using UnityEngine;
using System.Collections;
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 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement*speed);
}
}
Probably your reference of rigidbody is wrong…
Whit [SerializeField] or public variable, you can drag and drop the reference.
And in your code, you dont have a variable for speed.
try use thisone:
[SerializeField] private float speed;
[SerializeField] private Rigidbody rb;
void start()
{
if(rb == null)
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}