Tilting Head with Camera from the FirstPersonController Script FPS controller provided in (Unity 5)
Hello All, I am using the provided FPSController prefab provided with Unity 5 in the character assets. (I was making my own first person camera script but this provided more options)
I wanted the camera to effect the head bone of my character model. I want my character models head to tilt where I am moving my camera. What can I add to the script to do this?
Thank you for your time in advance!
If I parent the head to the "FirstPersonCharacter" camera, it will tilt etc but it obviously will not move on the proper axis, so the head will fly up and rotate etc.
I am guessing I need a script in the head bone that rotates on local axis to the bone, that follows the camera, any suggestions?
Thanks
Any help with this would be greatly appreciated! I am still learning and wanted to use the 1st person camera with a character model of $$anonymous$$e.
I created a separate script and put it in the head bone of the character.
The public variables headBone and cameraObject I dragged them from the Hierarchy into the inspector slots.
Here is my Script:
using UnityEngine;
using System.Collections;
//FirstPersonCharacter
//Character1_Head
public class HeadLook : $$anonymous$$onoBehaviour {
public Transform headBone;
public Camera cameraObject;
//var bizzyBone : Transform;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
headBone.rotation = cameraObject.localrotation;
}
void LateUpdate()
{
}
}
I get an error: "Severity Code Description Project File Line Error CS1061 'Camera' does not contain a definition for 'localrotation' and no extension method 'localrotation' accepting a first argument of type 'Camera' could be found (are you missing a using directive or an assembly reference?) 1st person camera.CSharp.Plugins D:\Unity Projects\1st person camera\Assets\Standard Assets\Characters\FirstPersonCharacter\Scripts\HeadLook.cs 22 "
Answer by CherryHotaling · Dec 10, 2015 at 02:11 AM
Ok Figured out how to Rotate the neck with the camera (The example below I had to have the X of the camera rotate the Z of the bone) Put this Script inside the head bone of your character:
using UnityEngine;
using System.Collections;
public class HeadTiltWCamera : MonoBehaviour
{
public Camera cameraRot;
private Vector3 objRot;
// Use this for initialization
void Start()
{
//Only take the x axis rotation
//Bone only uses Z for up and down, camera oddly is using the X for up and down
Vector3 tmp = cameraRot.transform.localEulerAngles;
tmp = cameraRot.transform.localEulerAngles;
tmp.x = 0f;
tmp.y = 0f;
tmp.z = -cameraRot.transform.localEulerAngles.x + 10.0f;
objRot = tmp;
transform.localEulerAngles = objRot;
}
// Update is called once per frame
void Update()
{
}
void LateUpdate()
{
//Only take the x axis rotation
//Bone only uses Z for up and down, camera oddly is using the X for up and down
Vector3 tmp = cameraRot.transform.localEulerAngles;
tmp = cameraRot.transform.localEulerAngles;
tmp.x = 0f;
tmp.y = 0f;
tmp.z = -cameraRot.transform.localEulerAngles.x + 10.0f;
objRot = tmp;
transform.localEulerAngles = objRot;
}
}
There is one problem, The camera will not move its position with the animation because OBVIOUSLY it is not parented to the head. The problem I have when I parent the camera to the head is the Minimum and Maximum X constraints do not work and the camera will rotate on weird angles. I suppose I should find a way to just make the neck do what the camera does with the FPS controller and have the camera move with the mouse movements. Or I could figure out why the camera is working oddly when parented to the head.
Well I figured I would post this for anyone that might be interested in this sort of thing.
I found out that the "$$anonymous$$ouseLook.cs" script from the FirstPersonController that comes with unity has a section in it that I changed to limit rotations. I basically bypassed the crazy math of the Quaternion at the bottom of the script. I changed around line 38s if statement for clampcertical rotation to this:
if (clampVerticalRotation)
{
character.localRotation = m_CharacterTargetRot;
//$$anonymous$$ake sure not to pass a certain angle
//Store the camera angle as Euler in tmp
Vector3 tmp = m_CameraTargetRot.eulerAngles;
//Debug.Log(tmp);
//Check if the x angle of the camera exceeds the $$anonymous$$ax or $$anonymous$$ and set it to max or $$anonymous$$ if it does
if (tmp.x > $$anonymous$$aximumX)
{
//Debug.Log("Greater than $$anonymous$$ax: " + tmp + " $$anonymous$$ax is: " + $$anonymous$$aximumX);
tmp.x = $$anonymous$$aximumX;
tmp.y = 90.0f;
tmp.z = 0.0f;
m_CameraTargetRot.eulerAngles = tmp;
}
if (tmp.x < $$anonymous$$inimumX)
{
//Debug.Log("Less than $$anonymous$$in: " + tmp + " $$anonymous$$in is: " + $$anonymous$$inimumX);
tmp.x = $$anonymous$$inimumX;
tmp.y = 90.0f;
tmp.z = 0.0f;
m_CameraTargetRot.eulerAngles = tmp;
}
camera.localRotation = m_CameraTargetRot;
//Original
//m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
}
Answer by Gatling-Hawk-youtube · May 27, 2018 at 05:28 PM
I once removed the bone from the model and placed it within the first person camera... that worked
Your answer
Follow this Question
Related Questions
Basic question about Unity functionality 0 Answers
camera scale 0 Answers
First Person Camera moves due to model 0 Answers
Adding components to prefab instead of player 0 Answers
Camera can't keep up with mouse when the FPS is low 0 Answers