My script for moving the player is not working. I have looked for red lines but everything looks good, however, when I play the game, the player is instantly stuck moving towards positive X. Here is the script:
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject pBody;
public GameObject pGunOrigin;
public static float speed;
Vector3 gOffset;
void Start () {
gOffset = pGunOrigin.transform.position - transform.position;
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
}
void FixedUpdate () {
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Rigidbody>().velocity = new Vector3(speed, GetComponent<Rigidbody>().velocity.y, GetComponent<Rigidbody>().velocity.z);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
GetComponent<Rigidbody>().velocity = new Vector3(-speed, GetComponent<Rigidbody>().velocity.y, GetComponent<Rigidbody>().velocity.z);
}
if (Input.GetKey(KeyCode.UpArrow))
{
GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x, GetComponent<Rigidbody>().velocity.y, speed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x, GetComponent<Rigidbody>().velocity.y, -speed);
}
if (Input.GetKey(KeyCode.DownArrow) == false && Input.GetKey(KeyCode.UpArrow) == false && Input.GetKey(KeyCode.RightArrow) == false && Input.GetKey(KeyCode.LeftArrow) == false)
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
}
pGunOrigin.transform.position = pBody.transform.position + gOffset;
}
}
What’s wrong?