- Home /
My sphere is not moving in the unity?
I am a beginner to the unity. I am making a rolling sphere game. In my game after all the code written in c# script, my directional light is moving from up to down instead of sphere. Object is not moving at all. Can anyone help me how can i fix this error? My code is shown below :- using System.Collections; using System.Collections.Generic; using UnityEngine;
public class New_script : MonoBehaviour {
public float speed;
public 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);
}
}
Answer by Bodhid · Sep 11, 2017 at 08:28 AM
Your code seems correct. Maybe your float speed
is not enough force to actually move your rigidbody, see if it works if you make this value higher. You could also use Update instead of FixedUpdate and change the force into movement * speed * Time.DeltaTime
, although in this case the speed value should probably also be higher.