Hello I have just started using unity and was trying to complete the tutorial to make a game where the player moves a ball. I ran into a problem while writing the script and can’t get the ball to move when I press play.
here is the script
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void start()
{
rb = GetComponent();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
I would appreciate the help, thanks!
You should make the two floats properties, and move Input.GetAxis() into Update().
FixedUpdate() should only be used for physics, and nothing else. Use Update() and LateUpdate() for testing things such as input.
Also Start() should be uppercase.
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
float moveHorizontal, moveVertical;
Vector3 movement;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
}
void FixedUpdate()
{
rb.AddForce(movement * speed);
}
}
Thanks you for your help! has the way unity uses scripts changed? because in the video it had no trouble reading the script, also I’m very new to programming in general and was wondering if there were any up to date tutorials that help teach unity/c#? Thanks a bunch!
As you get further into unity, you will see that it is best to put it into the Update instead of the FixedUpdate. But what the main issue is, is that you have a lowercase start(), when it should be an uppercased Start(). So you probably have a null reference to the Rigidbody.
2 Likes
Heres a link for programming in C#. Its the whole language.
but look on the Unity go to Tutorials - Scripting - And watch most. The most valuable ones in programming are ,
if else statements
voids , Voids make different (Functions) So say I tell this void number “A” it moves a ball 1 every second. I make another void that dose something but needs the ball to move. You call the void and it reads void “A” then goes on with the code.
var, floats, bools, ints, strings
void Update, Start, and the other ones Unity has
These are what I use most of the time when programming! GL