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