- Home /
How to make an empty child object not rotate with parent?
I am making a third person game and I have an issue with the camera. The way the camera works is by following an empty game object that is a child of the player. I put the empty game object to the right of the player to make room for the cross hair and so that the cross hair can be in the center of the screen. The problem is that when the player is facing the camera they flip to the left of the cross hair.
Answer by Cornelis-de-Jager · Jan 15, 2019 at 01:01 AM
Easiest way is to not have it as a child. Rather write a follow script. Position the camera in the position you want then start the game.
public class CameraFollow : MonoBehaviour {
Vector3 offset;
Transform player;
void Start () {
offset = transform.postion - player.position;
}
void LateUpdate () {
transform.position = Vector3.lerp (transform.position, player.position + offset, 1f);
}
}
Thank you for your help. sorry I forgot to include my camera follow script. I don't think your script will work for the type of game I'm making because I also use clamp and a camera collision script. Here is my script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class camerafollow : $$anonymous$$onoBehaviour {
public float Camera$$anonymous$$oveSpeed = 120.0f;
public GameObject CameraFollowObj;
Vector3 FollowPOS;
public float clampAngle = 80.0f;
public float inputSensitivity = 150.0f;
public GameObject CameraObj;
public GameObject PlayerObj;
public float camDistanceXToPlayer;
public float camDistanceYToPlayer;
public float camDistanceZToPlayer;
public float mouseX;
public float mouseY;
public float finalInputX;
public float finalInputZ;
public float smoothX;
public 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;
}
// Update is called once per frame
void Update () {
// We setup the rotation of the sticks here
float inputX = Input.GetAxis ("RightStickHorizontal");
float inputZ = Input.GetAxis ("RightStickVertical");
mouseX = Input.GetAxis ("$$anonymous$$ouse X");
mouseY = Input.GetAxis ("$$anonymous$$ouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputZ + 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);
}
}
Is there a way to keep the player always on the left side of the screen. even when you rotate the camera?