- Home /
Getting character to face direction of camera
Hi, I have made a character controller script which makes the character move and also face the direction that it moves in, however I want to add a camera look script which makes the camera follow the player and will allow me to change the camera rotation with the mouse, and then the new forward of the character is the forward of the camera. I've tried lots of methods but I just can't get it right so that once the forward of the character is changed it moves in accordance to it's new forward vector. Here is my character controller script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Private variables
private float _currentSpeed;
private float _walkSpeed = 3.5f;
private float _runSpeed = 5.0f;
private float _rotateSpeed = 6.6f;
private float _vvSpeed = 0;
private float _jumpHeight = 2f;
private float _doubleJumpHeight = 2.2f;
private int _jumpNum = 0;
private float _grav = 7.0f;
//Public Variables
public Vector3 movementVector = Vector3.zero;
public CharacterController myController;
void Start () {
myController = transform.GetComponent<CharacterController>();
}
void Update () {
if(Input.GetKey(KeyCode.LeftShift)){
_currentSpeed = _runSpeed;
}
if(Input.GetKeyUp(KeyCode.LeftShift) || !Input.GetKey(KeyCode.LeftShift)){
_currentSpeed = _walkSpeed;
}
movementVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if(movementVector != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementVector), Time.deltaTime * _rotateSpeed);
if(myController.isGrounded){
_jumpNum = 0;
movementVector.y = 0;
if(Input.GetKeyDown(KeyCode.Space)){
_jumpNum = 1;
_vvSpeed = _jumpHeight;
}
}else{
if(Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1){
_vvSpeed = _doubleJumpHeight;
_jumpNum = 0;
}
}
_vvSpeed -= _grav * Time.deltaTime;
movementVector.y = _vvSpeed;
myController.Move(movementVector * _currentSpeed * Time.deltaTime);
}
//Accessed by BouncePad script
public void Bounce(float amount){
_vvSpeed = amount;
}
}
I would just like to be pointed in the right direction. Thanks
Answer by Happy-Zomby · Sep 21, 2016 at 04:12 PM
Hi, I'm not sure I understand fully what you are looking for but I'm guessing what you want to do is make the camera a child object of your character then it will automatically follow your character around - and you can edit its position relative to the parent (your character) when using the mouse. If you look for camera as child you should find a lot of threads on this topic. Hope that helps,
When I attach the camera to the player, when I move left or right the camera will rotate to the same direction and it will look like I'm going forward when I'm holding the left arrow key. What I want is for the camera to follow the player but not rotate unless the mouse is used to rotate it. Then once the camera is rotated 90 degrees for example, the new forward of the character will be the same as the camera. And from then on the same rule applies where the character can move left and right without rotating the camera. It is very difficult to explain but it is basically the same concept as $$anonymous$$inecraft but in 3rd person.
You need to rotate your camera around the character see here: http://answers.unity3d.com/questions/600577/camera-rotation-around-player-while-following.html
then for your new foward... you can add the vector 3 of the camera to the one of your player to create a projection point and then have your character rotate towards that projection point. hope that helps,
Ok that makes a lot of sense, I think this might be what I need to do. Thanks
Answer by UNDERHILL · Sep 21, 2016 at 04:11 PM
https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
If the camera is on the player create an empty child on camera which extends in front of the player and target that with Transform.LookAt or use a negative multiplier on the fed vector ( whatever * -1.0f )
When I attach the camera to the player and make a child on the camera then make the camera look at the child using Transform.LookAt the camera looks down and shakes all over. Is this what you meant or have I done something wrong?
There are a few ways to do this. I imagine that you have a character with a floating 'third person' camera above and behind them, yes?
Can you please be very very specific about your setup and what you are trying to accomplish? A screenshot would help a LOT here!
Here is a screenshot of the player (the capsule) and the position of the camera behind it. Like I mentioned it is the same idea as $$anonymous$$inecraft that I'm trying to achieve but in a 3rd person type of view.
Your answer
Follow this Question
Related Questions
Launching a Character controller using vectors 0 Answers
Object not moving in forward direction 1 Answer
Camera moves into Object instead of following 1 Answer
nullifying targetObject after a Smooth LookAt Transition 2 Answers
Is there a way to make a Character Controller stop instead of move over obstacles?? 1 Answer