I have a script that orbits the player while still looking at him. I want to change the direction of movement depending where the camera is, so for example the player’s back is turned towards the camera and as the camera rotates 90 degrees to the left the A key now becomes his forward direction. I’ve got that part working but I feel like I’m doing it in the worst way possible and there should be a better way to do it, the way I’m doing it just won’t be smooth. Right now I’m getting the rotation values of where the camera is and using if statements to press the correct keys in between values.
Thanks!
PlayerScript
public float smooth = 0.5f;
public float playerSpeed = 5;
public float PlayergravitySpeed;
public float rotationSpeed;
public float turnSpeed;
public CamFollow camScript;
public Transform cam;
// Use this for initialization
void Start ()
{
//Reminder = MoveLeft should be not be working after 140
}
//If statemnts checking where the camera rotation is and depending on the values
//you walk a certain direction
void Update ()
{
//transform.forward = Camera.main.transform.forward;
//can only walk forwards between values 307-360, W walks forward
if(cam.eulerAngles.y <= 360)
{
if(cam.eulerAngles.y >= 307 && Input.GetKey(KeyCode.W) || Input.GetKeyDown(KeyCode.W))
{
moveForward = true;
}
else
{
moveForward = false;
}
}
//can only walk forwards between values 0-45, W walks forward
if(cam.eulerAngles.y <= 45)
{
if(cam.eulerAngles.y >= 0 && Input.GetKey(KeyCode.W) || Input.GetKeyDown(KeyCode.W))
{
moveForward = true;
}
else
{
moveForward = false;
}
}
//cannot move forward between values 307 and 45
if(cam.eulerAngles.y <= 307)
{
if(cam.eulerAngles.y >= 45)
{
moveForward = false;
}
}
//move left between 45-140, A key now becomes forward direction
if(cam.eulerAngles.y <= 140)
{
if(cam.eulerAngles.y >= 45 && Input.GetKey(KeyCode.A))
{
moveLeft = true;
}
else
{
moveLeft = false;
}
}
//Stop moving left if value left is less than 45 or greater than 140
if(cam.eulerAngles.y <= 45)
{
if(cam.eulerAngles.y >= 140)
{
moveLeft = false;
}
}
}//end of update
void FixedUpdate()
{
velocity = velocity + gravity_vector * playerSpeed * Time.deltaTime;
transform.position += velocity * playerSpeed * Time.deltaTime;
if (moveForward)
{
velocity = new Vector3 (0, 0, 5 * playerSpeed);
}
else
{
velocity = new Vector3 (0, transform.position.y, 0);
}
if(moveLeft)
{
velocity = new Vector3 (0, 0, 5 * playerSpeed);
}
}//end of FixedUpdate