- Home /
Moving Player Relative to Camera?
How would be the most efficient way to have the player move relative to the camera's rotation? Here's what I have right now. Currently, my script uses Vector3, which I know works off of worldspace rather than local space, is there any way to continue using Vector3, or will I have to restart the script from the ground up?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public Rigidbody rigidBody;
public float moveSpeed, jumpForce;
private Vector2 moveInput;
private float horizontalMove;
private float verticalMove;
public float horizontalTurn;
public LayerMask ground;
public Transform footPoint;
public bool grounded;
public float inputTest;
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//movement
//moveInput.x = Input.GetAxis("Horizontal");
//moveInput.y = Input.GetAxis("Vertical");
horizontalMove = Input.GetAxis("Horizontal");
verticalMove = Input.GetAxis("Vertical");
moveInput.Normalize();
inputTest = Input.GetAxis("Horizontal");
rigidBody.velocity = new Vector3(horizontalMove * moveSpeed, rigidBody.velocity.y, verticalMove * moveSpeed);
//Sprint
if(Input.GetButtonDown("Fire1") && grounded)
{
moveSpeed = 4;
}
else
{
moveSpeed = 2;
}
//animator control
if ((rigidBody.velocity.z != 0)||(rigidBody.velocity.x !=0))
{
anim.SetFloat("Vertical",rigidBody.velocity.z);
anim.SetFloat("Horizontal",rigidBody.velocity.x);
}
anim.SetFloat("Speed", rigidBody.velocity.magnitude);
anim.SetBool("Grounded",grounded);
//Check for ground contact and jump
RaycastHit hit;
if(Physics.Raycast(footPoint.position, Vector3.down, out hit, .3f, ground))
{
grounded = true;
} else
{
grounded = false;
}
if(Input.GetButtonDown("Jump") && grounded)
{
rigidBody.velocity += new Vector3(0f, jumpForce, 0f);
}
}
}
You'll have to forgive the messy code, this is a first draft.
Answer by Llama_w_2Ls · Aug 06, 2020 at 07:40 AM
bean.transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0); //Locks the y rotation of the player to the camera's y rotation
transform.position = new Vector3 (bean.transform.position.x, bean.transform.position.y + 0.5f, bean.transform.position.z); //Moves the camera with the player
Well im still kinda confused about what youre trying to do. Im guessing that you want the player's rotation to match the camera's rotation? Or the player's position match the camera's position? Anyway, heres both. bean.transform is the transform of my player, and this code is attached to the main camera. It doesnt have to be but instead of writing transform.position etc.
you could write camera.transform.position
. Hope it helps
Your answer
Follow this Question
Related Questions
Camera problem 2 Answers
Camera and Player position 0 Answers
Player's movements doesn't match orbital camera 1 Answer
Don'tDetroyOnLoad | issue 1 Answer
Make a ridgid body move in the direction the camera is facing 0 Answers