AddForce wont move sphere
Here is the code I've been using for the roll a ball tutorial. The comment portion allows the ball to move around but the code in the fixedUpdate() with the AddForce does nothing. Im hoping someone can help me understand if the code is wrong or perhaps another way to test the AddForce as I am sure I will want to use it in future projects. I know that the info for the vector is being stored correctly because of the commented code, but for some reason when I put that info into the AddForce function the sphere does not move. Any help please
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
Debug.Log("W is Pressed");
if (Input.GetKeyUp(KeyCode.UpArrow))
Debug.Log("W is released");
/*
float horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Vector3 pos = new Vector3(horizontal, 0, vertical);
transform.Translate(pos);
*/
}
void fixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
rb.AddForce(movement * speed);
}
}
Comment