I am trying to do the space shooter tutorial and the first scripted code that you have to write isn't correct in the tutorial since it is in a later version so there is the updated code in the space below. but when I input it into monodev as instructed it has issues. here is the code: using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Boundary boundary; void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody.velocity = movement * speed; rigidbody.position = new Vector3 ( Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax) ); rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt); } } can anyone explain what I am supposed to be fixing?
hi, please use [code ][/code ] tags when pasting code into the forums, helps with the readability. There is a sticky on them at the top of the scripting section. In unity 5 they removed the default quick accessors like "rigidbody", you now need to explicitly create them. Basically add Code (csharp): // class declaration of variable Rigidbody rigidbody; void Start() { // initialize variable rigidbody = GetComponent<Rigidbody>(); }
okay so that cleaned some of it up but now these two alerts are popping up in the unity console -Assets/Scripts/PlayerController.cs(15,19): warning CS0108: `PlayerController.rigidbody' hides inherited member `UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended -Assets/Scripts/PlayerController.cs(15,19): warning CS0414: The private field `PlayerController.rigidbody' is assigned but its value is never used here is the updated code Code (CSharp): using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Boundary boundary; Rigidbody rigidbody; void Start() { rigidbody = GetComponent<Rigidbody> (); } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); GetComponent<Rigidbody>().velocity = movement * speed; GetComponent<Rigidbody>().position = new Vector3 ( Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax) ); GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt); } }
once rigidbody is set in the Start function you don't need to (and shouldn't really for performance reasons) replace all the other rigidbody references... but anyway. Can you confirm which version of unity you are using?
I am using version 5.1.3.f1 I have replace rigidboy where it needs to be but now something is wrong with the velocity, position and rotation. Also how when in the game view mode in unity and play is pressed I could not figure out what keys to press in order to move the character object. If you know what the defaults are that would be awesome Code (CSharp): using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Boundary boundary; Rigidbody rigidbody; void Start() { rigidbody = GetComponent<Rigidbody> (); } void FixedUpdate () { float rigidbody; float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody().velocity = movement * speed; rigidbody().position = new Vector3 ( Mathf.Clamp (rigidbody().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidbody().position.z, boundary.zMin, boundary.zMax) ); rigidbody().rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody().velocity.x * -tilt); } }
rigidbody not rigidbody() "()" on the end denotes a function, you want the variable. horizontal and vertical axis are wasd and arrow keys I believe, look in the Input Manager within unity to check http://docs.unity3d.com/Manual/class-InputManager.html
I don't know if you are still getting this warning -Assets/Scripts/PlayerController.cs(15,19): warning CS0108: `PlayerController.rigidbody' hides inherited member `UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended if so you should replace Code (csharp): Rigidbody rigidbody; with Code (csharp): new Rigidbody rigidbody;
except you don't use "new" when dealing with components, you let the unityengine create them so they're handled correctly.
Yes and no. In case of instantiating you don't use new. However in the case of telling the compiler that the Rigidbody field you have added to your class is hiding the field named Rigidbody on UnityEngine.Component it is perfectly alright to do because it has nothing to do with instantiating.
Okay so I had gotten everything working and yes that took away the warning. Now I am trying to get the asteroid to tumble. the only problem is I am using the given code and I have tried code in their API but so far the only movement I've gotten is directional instead of rotational. Here is my code: Code (CSharp): using UnityEngine; using System.Collections; public class RandomRotator : MonoBehaviour { public float tumble; new Rigidbody rigidbody; void Start () { rigidbody.angularVelocity= Random.insideUnitSphere * tumble; } }
for the moment yes. later i will add in the movement I already have my destroy by contact code in place and i believe it is working. I just need it to rotate and since there is a glitch in the code it wont let me test in the play mode. this is the error i have been getting NullReferenceException: Object reference not set to an instance of an object RandomRotator.Start () (at Assets/Scripts/RandomRotator.cs:11)
I'm thinking you should use AddTorque instead of angularVelocity. Setting the angular velocity manually will overwrite the rotational effect of one astroid colliding with another. In therory you should only need to call AddTorque once if you have set angularDrag to zero.
I just tested your code. 1) Did you assign the rigidbody of your script? I cannot see it in your code and it does not show up in the editor 2) Did you uncheck "Use Gravity" on the astroid's rigidbody component?
Yes gravity is turned off. Assign rigidbody how so? Code (CSharp): using UnityEngine; using System.Collections; public class RandomRotator : MonoBehaviour { public float tumble; new Rigidbody rigidbody; void Start () { rigidbody.angularVelocity = Random.insideUnitSphere * tumble; } }
You can assign the value of your rigidbody field in multiple ways. If the RandomRotator script is attached to the same game object that has the Rigidbody you can do this in the Start method this.rigidbody = this.GetComponent<Rigidbody>(); You can also choose to make the rigidbody field public and then drag'n'drop the rigidbody into the field in the editor.
That first one worked. Awesome thanks. So I need to remember to GetComponent<Rigidbody>(); whenever calling that in the code. I'm sure I'll have more question in the near future being I am aiming to finish this game today.
Hello all! I am brand new to Unity (and C#) having only used it for a few weeks now, and am utilizing the tutorials. I've been following the thread because I had issues with Unity 5 handling scripting differently than in the tutorial, and have made the changes brought up in this discussion (thanks for the help). Before I ask a question, I'll give some context: I was previously following the Upgrade Guide for Unity 5 for this tutorial up until the scripting part (page 14) where it says to create a reference variable "rb" for the rigidbody. Out of curiosity, why did you do things differently to the Guide and how did you know to do what you did? Additionally, I'd like to understand how to use reference variables as used in the Upgrade Guide. (Again, brand new to Unity and C#) Thanks!