- Home /
Player model not turning with the camera, always faces forward.
Probably a stupid question but, as you can see in the picture my player model is not turning itself to align towards where the camera is pointed (I am running towards the wall in the picture). Basically when I turn the camera the player turns with it, that all works fine. But the model always stays facing forward.
We need either the code or the hierarchy to understand.
Hey, this is the script and the hierarchy. Basically there is an empty object that is a child of the player that the $$anonymous$$ainCamera follows.
Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : $$anonymous$$onoBehaviour
{
public float Camera$$anonymous$$oveSpeed = 100.0f;
public GameObject CameraFollowObj;
Vector3 FollowPOS;
public float clampAngle = 65.0f;
public float inputSensitivity = 120.0f;
private GameObject CameraObj;
private GameObject PlayerObj;
private float camDistanceXToPlayer;
private float camDistanceYToPlayer;
private float camDistanceZToPlayer;
private float mouseX;
private float mouseY;
private float finalInputX;
private float finalInputZ;
private float smoothX;
private float smoothY;
private float rotY = 0.0f;
private float rotX = 0.0f;
// Use this for initialization
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update ()
{
mouseX = Input.GetAxis ("$$anonymous$$ouse X");
mouseY = Input.GetAxis ("$$anonymous$$ouse Y");
finalInputX = mouseX;
finalInputZ = mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = $$anonymous$$athf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (-rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
void LateUpdate ()
{
CameraUpdater ();
}
void CameraUpdater()
{
// set the target object to follow
Transform target = CameraFollowObj.transform;
//move towards the game object that is the target
float step = Camera$$anonymous$$oveSpeed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, target.position, step);
}
}
So you turn the camera using the mouse and the player turns when the camera turns? Do you have a script that turns the player object? It looks like you have a reference to PlayerObj but never use it.
Answer by MasterChameleonGames · Mar 09, 2019 at 09:35 PM
Figured it out, you're rotating the CameraFollow GameObject which is a child of Player, and the model is another child, so only CameraFollow is rotated.
Change transform.rotation = localRotation;
To transform.parent.rotation = localRotation;
That will rotate the parent which will rotate the models along with it.
Of course, this is assuming the code you gave is working as intended.
Doing that it gave me this error. And the camera stopped working.
NullReferenceException: Object reference not set to an instance of an object
Did you change anything else?
Also, try reverting back to what you had before and see if CameraFollow is rotating. You'll have to look at the inspector and see if the values change since it is an empty object.
And this script is on the CameraFollow GameObject, the child of Player, right?
Your answer
Follow this Question
Related Questions
Using a Pre Rendered character as the player 0 Answers
How to make a camera that moves with the player? 1 Answer
Free Avatar Locations Anyone? 0 Answers
Character Controller Help. 1 Answer
Parenting a camera to different objects with input? 1 Answer