I have this script to move the player (and their gun) and I can’t figure out why it’s not working. The debug.log(“blah”) things show me that the if statements are activating but the player doesn’t move at all. Here is the script:
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject pBody;
public GameObject pGunOrigin;
public static float speed;
Vector3 gOffset;
private bool right;
private bool left;
private bool forward;
private bool backward;
public Rigidbody rbody;
void Start () {
gOffset = pGunOrigin.transform.position - transform.position;
rbody = GetComponent<Rigidbody>();
}
void FixedUpdate () {
right = Input.GetKey(KeyCode.RightArrow);
left = Input.GetKey(KeyCode.LeftArrow);
forward = Input.GetKey(KeyCode.UpArrow);
backward = Input.GetKey(KeyCode.DownArrow);
if (right == true && left == false)
{
Debug.Log("Right");
rbody.AddForce(new Vector3(speed, 0, 0), ForceMode.VelocityChange);
}
if (left == true && right == false)
{
Debug.Log("Left");
rbody.AddForce(new Vector3(-speed, 0, 0), ForceMode.VelocityChange);
}
if (forward == true && backward == false)
{
Debug.Log("Forward");
rbody.AddForce(new Vector3(0, 0, speed), ForceMode.VelocityChange);
}
if (backward == true && forward == false)
{
Debug.Log("Backward");
rbody.AddForce(new Vector3(0, 0, -speed), ForceMode.VelocityChange);
}
pGunOrigin.transform.position = pBody.transform.position + gOffset;
}
}