- Home /
Character face camera direction when this pass a rotation limit
I want to make a character face to camera direction when this pass for example 30 degress on y axis and play and animation for turn right or left, i dont know how to make that, camera has a simple mouse rotator on it and its not parented with player prefab. Also if is possible that when character is walking it face the camera direction. Something like battlefield 4, Video for reference: https://youtu.be/LCUzWujaiLA?t=30 Thanks in advance, greetings. Here is the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerScript1 : MonoBehaviour {
[System.Serializable]
public class Animators
{
public Animator Player;
public Animator FirstPersonPlayer;
}
public Animators animators;
[System.Serializable]
public class PlayerMovement
{
public MovementSway movementSway;
public float DampTime = 0.15f;
public float WALK_SPEED = .5f;
public float NORMAL_SPEED = .8f;
public float Walk_Amount;
public float Run_Amount;
public float Rotation_Walk_Amount;
public float Rotation_Run_Amount;
public float Last_Rotation;
public float Last_Amount;
public float MaxLean;
public float BodyMaxLean;
public float CameraMaxLean;
public float WeaponMaxLean;
public float LeanDamptime;
public Transform LeanRotator;
public Transform BodyRotator;
public Transform WeaponLeanRotator;
public Transform CameraLeanRotator;
public Quaternion InitialRotation;
public Quaternion BodyInitialRotation;
public Quaternion CameraInitialRotation;
public Quaternion WeaponInitialRotation;
}
public PlayerMovement playerMovement;
[System.Serializable]
public class Strings
{
public string MoveX = "Walk";
public string MoveZ = "Strafe";
public string Aim = "Mouse0";
public string Walk = "Walking";
public string Crouch = "Crouch";
}
public Strings strings;
[System.Serializable]
public class Booleans
{
public bool isAiming = false;
public bool isCrouch = false;
public bool PressingRC = false;
public bool PressingLS = false;
public bool PressingLCtrl = false;
public bool LeanRight = false;
public bool LeanLeft = false;
}
public Booleans booleans;
public bool Reloading = false;
void Start ()
{
playerMovement.InitialRotation = transform.localRotation;
}
void Update()
{
Crouching();
Walk();
Aiming();
Walking();
Move();
Leaning ();
Lean ();
}
void Leaning()
{
if (Reloading==false){
if (Input.GetKeyDown(KeyCode.Q) && !Reloading)
{
booleans.LeanLeft = !booleans.LeanLeft;
booleans.LeanRight = false;
}
if (Input.GetKeyDown(KeyCode.E) && !Reloading)
{
booleans.LeanRight = !booleans.LeanRight;
booleans.LeanLeft = false;
}
}
else if (Reloading==true){
booleans.LeanLeft = false;
booleans.LeanRight = false;
}
}
void Lean()
{
if (booleans.LeanLeft == true && !Reloading)
{
animators.Player.SetFloat("Lean", -1f, playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat("Lean", -1f, playerMovement.DampTime * 4, Time.deltaTime);
playerMovement.LeanRotator.localRotation = Quaternion.Slerp (playerMovement.LeanRotator.localRotation, playerMovement.InitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.BodyRotator.localRotation = Quaternion.Slerp (playerMovement.BodyRotator.localRotation, playerMovement.BodyInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.WeaponLeanRotator.localRotation = Quaternion.Slerp (playerMovement.WeaponLeanRotator.localRotation, playerMovement.WeaponInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.CameraLeanRotator.localRotation = Quaternion.Slerp (playerMovement.CameraLeanRotator.localRotation, playerMovement.CameraInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.InitialRotation = Quaternion.Euler (0, 0, playerMovement.MaxLean);
playerMovement.BodyInitialRotation = Quaternion.Euler (0, 0, playerMovement.BodyMaxLean);
playerMovement.WeaponInitialRotation = Quaternion.Euler (0, 0, playerMovement.WeaponMaxLean);
playerMovement.CameraInitialRotation = Quaternion.Euler (0, 0, playerMovement.CameraMaxLean);
animators.Player.SetBool("leaning", true);
animators.FirstPersonPlayer.SetBool("leaning", true);
}
if (booleans.LeanLeft == false && booleans.LeanRight == false)
{
animators.Player.SetFloat("Lean", 0f, playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat("Lean", 0f, playerMovement.DampTime * 4, Time.deltaTime);
playerMovement.LeanRotator.localRotation = Quaternion.Slerp (playerMovement.LeanRotator.localRotation, playerMovement.InitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.BodyRotator.localRotation = Quaternion.Slerp (playerMovement.BodyRotator.localRotation, playerMovement.BodyInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.WeaponLeanRotator.localRotation = Quaternion.Slerp (playerMovement.WeaponLeanRotator.localRotation, playerMovement.WeaponInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.CameraLeanRotator.localRotation = Quaternion.Slerp (playerMovement.CameraLeanRotator.localRotation, playerMovement.CameraInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.InitialRotation = Quaternion.Euler (0, 0, 0);
playerMovement.BodyInitialRotation = Quaternion.Euler (0, 0, 0);
playerMovement.WeaponInitialRotation = Quaternion.Euler (0, 0, 0);
playerMovement.CameraInitialRotation = Quaternion.Euler (0, 0, 0);
animators.Player.SetBool("leaning", false);
animators.FirstPersonPlayer.SetBool("leaning", false);
}
if (booleans.LeanRight == true && !Reloading)
{
animators.Player.SetFloat("Lean", 1f, playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat("Lean", 1f, playerMovement.DampTime * 4, Time.deltaTime);
playerMovement.LeanRotator.localRotation = Quaternion.Slerp (playerMovement.LeanRotator.localRotation, playerMovement.InitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.BodyRotator.localRotation = Quaternion.Slerp (playerMovement.BodyRotator.localRotation, playerMovement.BodyInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.WeaponLeanRotator.localRotation = Quaternion.Slerp (playerMovement.WeaponLeanRotator.localRotation, playerMovement.WeaponInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.CameraLeanRotator.localRotation = Quaternion.Slerp (playerMovement.CameraLeanRotator.localRotation, playerMovement.CameraInitialRotation, playerMovement.LeanDamptime * Time.deltaTime);
playerMovement.InitialRotation = Quaternion.Euler (0, 0, -playerMovement.MaxLean);
playerMovement.BodyInitialRotation = Quaternion.Euler (0, 0, -playerMovement.BodyMaxLean);
playerMovement.WeaponInitialRotation = Quaternion.Euler (0, 0, -playerMovement.WeaponMaxLean);
playerMovement.CameraInitialRotation = Quaternion.Euler (0, 0, -playerMovement.CameraMaxLean);
animators.Player.SetBool("leaning", false);
animators.FirstPersonPlayer.SetBool("leaning", false);
}
}
void Walk()
{
if (Input.GetKeyDown (KeyCode.LeftShift)) {
booleans.isAiming = !booleans.isAiming;
}
}
void Crouching()
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
booleans.PressingLCtrl = true;
playerMovement.movementSway.Amount = playerMovement.Walk_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Rotation_Walk_Amount;
}
if
(Input.GetKeyUp(KeyCode.LeftControl))
{
booleans.PressingLCtrl = false;
playerMovement.movementSway.Amount = playerMovement.Last_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Last_Rotation;
}
animators.Player.SetFloat(strings.Crouch, Input.GetAxis(strings.Crouch),playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.Crouch, Input.GetAxis(strings.Crouch),playerMovement.DampTime * 4, Time.deltaTime);
}
void Aiming()
{
animators.Player.SetFloat(strings.Aim, Input.GetAxis(strings.Aim), playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.Aim, Input.GetAxis(strings.Aim), playerMovement.DampTime * 4, Time.deltaTime);
}
void Walking()
{
if (booleans.isAiming == true)
{
if (booleans.PressingLCtrl == true) {
playerMovement.movementSway.Amount = playerMovement.Walk_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Rotation_Walk_Amount;
}
else {
animators.Player.SetBool(strings.Walk, true);
animators.FirstPersonPlayer.SetBool(strings.Walk, true);
playerMovement.movementSway.Amount = playerMovement.Walk_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Rotation_Walk_Amount;
}
}
if (booleans.isAiming == false)
{
if (booleans.PressingLCtrl == true) {
playerMovement.movementSway.Amount = playerMovement.Walk_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Rotation_Walk_Amount;
} else {
animators.Player.SetBool(strings.Walk, false);
animators.FirstPersonPlayer.SetBool(strings.Walk, false);
booleans.PressingRC = false;
playerMovement.movementSway.Amount = playerMovement.Run_Amount;
playerMovement.movementSway.RotationAmount = playerMovement.Rotation_Run_Amount;
}
}
}
void Move ()
{
if (animators.Player.GetBool (strings.Walk))
{
animators.Player.SetFloat(strings.MoveZ, Mathf. Clamp(Input.GetAxis(strings.MoveZ),-playerMovement.WALK_SPEED,playerMovement.WALK_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.Player.SetFloat(strings.MoveX, Mathf. Clamp(Input.GetAxis(strings.MoveX),-playerMovement.WALK_SPEED,playerMovement.WALK_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.MoveZ, Mathf. Clamp(Input.GetAxis(strings.MoveZ),-playerMovement.WALK_SPEED,playerMovement.WALK_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.MoveX, Mathf. Clamp(Input.GetAxis(strings.MoveX),-playerMovement.WALK_SPEED,playerMovement.WALK_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
playerMovement.Last_Amount = playerMovement.Walk_Amount;
playerMovement.Last_Rotation = playerMovement.Rotation_Walk_Amount;
}
else
{
animators.Player.SetFloat(strings.MoveZ, Mathf. Clamp(Input.GetAxis(strings.MoveZ),-playerMovement.NORMAL_SPEED,playerMovement.NORMAL_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.Player.SetFloat(strings.MoveX, Mathf. Clamp(Input.GetAxis(strings.MoveX),-playerMovement.NORMAL_SPEED,playerMovement.NORMAL_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.MoveZ, Mathf. Clamp(Input.GetAxis(strings.MoveZ),-playerMovement.NORMAL_SPEED,playerMovement.NORMAL_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
animators.FirstPersonPlayer.SetFloat(strings.MoveX, Mathf. Clamp(Input.GetAxis(strings.MoveX),-playerMovement.NORMAL_SPEED,playerMovement.NORMAL_SPEED),playerMovement.DampTime * 4, Time.deltaTime);
playerMovement.Last_Amount = playerMovement.Run_Amount;
playerMovement.Last_Rotation = playerMovement.Rotation_Run_Amount;
}
}
}
Your answer
Follow this Question
Related Questions
How would I switch between 2 animation states connected to 1 state under input? 0 Answers
How to handle multiple animation variants 1 Answer
Issue with mecanim playing an animation using setbool 1 Answer
Moving a character depending on where it is facing. 1 Answer
Unity 5 Broken Animations? 3 Answers