- Home /
Camera based movement 3rd person game,Movement like in 3rd person games (camera based)
So I want to implement camera based movement in this function, but no tutorials really help me. I want to move like in all 3rd person games, if I look somewhere and press W character will move in that direction. Thanks in advance, if you will help me.
private void Movement()
{
float verticalAxis = Input.GetAxis("Vertical");//varies between 1 and -1
float horizontalAxis = Input.GetAxis("Horizontal");
//create a new vector
Vector3 updatedVector = new Vector3(horizontalAxis, 0.0f, verticalAxis);//new position that has been changed through axis
transform.LookAt(updatedVector + transform.position);//facing towards vector
animator.SetBool("Is_Running", true);//setting animator boolean
transform.Translate(updatedVector * speed * Time.deltaTime, Space.World);//actual movement
if(verticalAxis == 0 && horizontalAxis == 0)//if player doesn't move
{
animator.SetBool("Is_Running", false);
}
}
}
is there something like Space.World but for camera relative movement? like Space.Camera.
Answer by GanemVisk · Dec 14, 2019 at 07:30 PM
You can get world based camera directions from its transform and multiply it by your input, getting world based input directions.
Camera camera = Camera.main;
Vector3 cameraForward = Vector3.Scale(camera.forward, new Vector3(1, 0, 1)).normalized; // forward direction in 2d
Vector3 updatedVector = verticalAxis * cameraForward + horizontalAxis * camera.right; // right is already in the plane
As a tip, you could use a CharacterControler to handle movement, it will automatically handle collisions and slopes.
@GanemVisk Guess you meant camera.transform.forward, because camera.forward(or right) doesn't work.
Your answer
Follow this Question
Related Questions
Making jumping independent of normalized movement? 1 Answer
Mouse click movement script issue. 2 Answers
Smooth touch for map 0 Answers