Question by
dsharma_ce · Oct 14, 2016 at 07:42 AM ·
roll a ball
Roll a Ball game: Ball is not moving and my code is correct. Please help me on this.
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);
}
}
Comment
Answer by Thiagow · Oct 14, 2016 at 08:24 AM
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);
}