input.getaxis not working
Hello - i've got a Problem: i wrote some Code to get a Little ball rolling, but it really doesnt do anything - would be nice if someone could help. heres the Code: public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(1.0f, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
I have the same problem, please help us. I have also tested it with rb.transform.Translate (movement); and it does not work.
Answer by DFT-Games · May 29, 2017 at 10:52 PM
Hi @knorke3 ,
There are 2 problems in your script:
The Start is written start (lowercase) so I guess you have plenty of Null Reference Error
You forgot to use moveHorizontal
Here the working code (set the speed at 15f as default as it seems about right in the context):
public class PlayerController : MonoBehaviour {
public float speed = 15f; // let's set a decent speed as default
private Rigidbody rb;
void Start() // Upper case because in C# casing counts!
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// Let's assign both x and z
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
I hope this helps ;)
Your answer
Follow this Question
Related Questions
Bullet instantiates but doesn't move forward? 1 Answer
Adding force to a rigidbody opposite to the direction of a rotating weapon 0 Answers
Save a script created mesh as mesh 1 Answer
Acceleration only increasing when I press a key 0 Answers
How to make UI Button serves as buttons for Input.getAxis()? 0 Answers