- Home /
Question by
CosmicCrafter707 · Apr 30, 2020 at 02:48 AM ·
cameraplayer movement
PLayer movement relative to camera
a firend and i are trying to make our first game in unity, and upon making the camera a child of the player object and allowing it to rotate around the player, we realized that the player object moves in relation to the world rather than the camera. if you know how to fix this, thank you, your help is appreciated
our code: public class PlayerMovement : MonoBehaviour { public float speed = 18; float jumpForce = 8.0f; public Rigidbody rig; bool jumping = false;
void Start()
{
rig = GetComponent<Rigidbody>();
}
void Update()
{
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
rig.MovePosition(transform.position + movement);
if (Input.GetButtonDown("Jump"))
{
rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
jumping = true;
}
Comment