FP MouseLook and Animating
I want to have my player be able to move his head and arms up and down with the mouse and the body rotate left and right with the mouse with animations (such as walk, idle, strafe, back, etc..). The problem is that when I try and do any of these things they just won't rotate. My theory is that the animation is preventing the mouse look script from rotating the objects it needs to rotate, but I need to animate the arms and stuff. (especially the arms when aiming)
(Model was made with Fuse and the animations were done in Mixamo)
MouseLook Script
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class FPSCamScript : NetworkBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX;
public float sensitivityY;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Start()
{
sensitivityX = PlayerPrefs.GetFloat("sensitivity");
sensitivityY = PlayerPrefs.GetFloat("sensitivity");
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetMouseButtonDown(1))
{
sensitivityX = PlayerPrefs.GetFloat("adssens");
sensitivityY = PlayerPrefs.GetFloat("adssens");
}
else if (Input.GetMouseButtonUp(1))
{
sensitivityX = PlayerPrefs.GetFloat("sensitivity");
sensitivityY = PlayerPrefs.GetFloat("sensitivity");
}
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(0, rotationX, 0);
transform.GetChild(9).GetChild(2).GetChild(0).GetChild(0).GetChild(2).transform.GetChild(0).transform.localEulerAngles = new Vector3(-rotationY, 0, 0); //RArm
transform.GetChild(9).GetChild(2).GetChild(0).GetChild(0).GetChild(0).transform.GetChild(0).transform.localEulerAngles = new Vector3(-rotationY, 0, 0); //LArm
transform.GetChild(9).GetChild(2).GetChild(0).GetChild(0).GetChild(1).GetChild(0).transform.localEulerAngles = new Vector3(-rotationY, 0, 0); //Head & Camera
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
Player Hierarchy Object Containing Script
unity-2018-09-03-16-38-15.png
(12.0 kB)
unity-2018-09-03-16-38-31.png
(7.0 kB)
Comment
Answer by Wesley21spelde · Jan 13, 2021 at 11:53 AM
Maybe this works
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}