- Home /
How can I make movement relative to camera only after becoming stationary?
void HandleMove()
{
if (canMove && pause.paused == false)
{
var speed = walkSpeed;
// detect input movement
var moveHorizontal = Input.GetAxis("Horizontal");
var moveVertical = Input.GetAxis("Vertical");
IsMoving = moveHorizontal != 0 || moveVertical != 0;
var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// forward is always direction of camera forward along x/z plane
if (useCameraMovement)
{
var camForward = Camera.main.transform.forward;
var camDir = new Vector3(camForward.x, 0.0f, camForward.z);
var angle = Mathf.Sign(camDir.x) * Vector3.Angle(camDir.normalized, Vector3.forward);
var quat = Quaternion.Euler(0f, angle, 0f) * movement;
movement = quat;
}
// rotate the character
var rot = movement * (speed / 10);
if (movement != Vector3.zero)
{
var newRotation = Quaternion.LookRotation(rot);
rb.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 33f);
}
// move the character
movement *= (speed / 10f);
var characterMovement = transform.position + movement;
if (attackTimer <= 0 || !IsGrounded)
{
rb.MovePosition(characterMovement);
}
}
}
I have a 3rd person camera that follows the player and that rotates in 45 degree increments, while looking down at the player. I force the movement relative to the camera, but I want this to take effect only after becoming stationary.
if i'm holding up the player moves up, away from the camera, but if he is moving up and the camera is rotated while he is moving the player rotates with the camera, and instead of staying the straight path up he is now moving on a straight path 45 degrees to the left or right of the path. i can't figure out how to make it so that the movement is only re-related to the camera after the player stops moving. i still want movement relative to the camera, just not to reset until after he is stationary. How can I improve this code to achieve this effect?
Your answer
Follow this Question
Related Questions
How can I make WASD controls relative to camera Position? 1 Answer
Need help to make the camara rotation follow the player 0 Answers
how can i rotate the camera around the object , which follows the object??? 0 Answers
camera not rotating with body 0 Answers
Player Movement Relative to Camera? 4 Answers