- Home /
Roll a ball - ball not moving
Hi - I'm a beginner to Unity3D and I started with the Roll a Ball game. I find that I'm not able to move the ball in Play mode even though I've set everything up correctly. This is how the ball is set up:
Also, this is my PlayerController code: using System.Collections; using System.Collections.Generic; using UnityEngine;
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);
}
}
I'm not sure what I'm doing wrong here. Could I be missing something in my initial installation which would prevent me from running the program?
Thanks in advance!
Are you sure, that you have clicked into the Game window, or the Game window is active after hitting the play button?
Also, you could check the moveHorizontal and moveVertical values if they change. Write this in the FixedUpdate at the end:
Debug.Log("Horizontal: " + moveHorizontal + " // Vertical: " + moveVertical);
If you can see the values changing, it could be the fault of the RigidBody.
Are you sure that you are applying enough force to the ball? You could increase the Speed value and see if it is moving or not. If not, set the initial position of the ball to higher on the Y coordinate. $$anonymous$$ake sure it falls down, so it is not stuck in the ground.
Thanks guys! I moved up the position of the object to a higher position along the Y-axis, and the ball falls down. Can you tell me what I would need to do to move the ball in the Horizontal plane (XZ-plane), ins$$anonymous$$d of along the Y-axis?
Answer by RisingDead_xTR · Jan 16, 2017 at 02:00 PM
The script seems to be alright. There are only two possible problems you might be facing. One the rigidbody component is not acquired by the script from the gameobject. If so check it by using the script below (paste it in Fixed Update). If the rigidbody is acquired but the gameobject not moving then go to Edit (from Toolbar) > Project Settings > Input and verify and correct them according to your needs.
if (rb = null)
{
Debug.Log("Failed to get rigidbody component from gameobject.");
}
else
Debug.Log("Rigidbody component from gameobject is aquired.");