- Home /
First Person Camera not being child of the character/ Set child's rotation to global rotation
Hey there,
I'm relatively new to Unity and I am trying to create a FPS with physics similar to Mario Galaxy where you can hop from planetoid to planetoid and such things. I am now facing a problem with my camera when changing the planet. In my scene, every planetoid has a trigger around them and when a rigidbody touches it, it is attracted by the planetoid. This is true for projectiles as well as the player. Now my problem is, when I am changing the planet as player, the player's gets rotated so that his feet land on the ground first. My camera is a child of the player and when the player gets into a new orbit, the camera's x-rotation stays the same, so the camera is "jumping" from one rotation to another. This really affects the orientation and because of that I want the camera to still look into the same direction when changing the orbit.
I already tried to unchild the camera from the player but then I am not able to make the camera's rotation be the player's rotation.
Has anyone of you a solution for this? I would prefer the camera to stay a child of the player but maybe anyone of you has a better solution for this.
Thanks in advance :)
you could try unchilding the camera, moving the player, then rechilding
Sorry, i was busy for a while and could not reply to your suggestion. I tried to unchild the camera in game, but the problem with this is, that the "jump" in rotation is only delayed until i rechild the camera. $$anonymous$$aybe i did it wrong, i can't even imagine how i could do it. do you have an example or something like that?
Answer by fsdjfsdkjfsdk · Jul 28, 2015 at 10:36 PM
You could try lerping the cameras rotation to smoothly transition from one rotation to another using the Quaternion.Lerp function. I know this is not the full answer, but it will help with the "jumping" from one rotation to another. Here is a tutorial on lerping, if you are new to unity. The tutorial explains how to do it with position, but its the same basic idea for rotation as well.
Sorry, i was busy for a while and could not reply. I'm sure i could erase the jumping with the Lerp function but i wanted the jump not to happen in the first place. I'm not completely sure now because i had no time to try different solutions the last couple days but it would be optimal if the camera's rotation can only be manipulated by the player. but the camera should always follow my player object and be on the same position relatively to the player.
Answer by XDSoftworks · Jan 17, 2017 at 11:57 PM
@lurxy try this. it's untested in this form but should work.
using UnityEngine;
using System;
using System.Collections;
/*
--------------------------------------------------------------------------------------------
this is a bare bones version of my cam rig in my game, it is only the core
functions for following and rotation, it is very easy to extend into nearly anything.
--------------------------------------------------------------------------------------------
*/
public class camrigsimple : MonoBehaviour {
public Transform target; // set this or it won't follow anything and will spam your console until you do so
[SerializeField]
private float smoothTime = 3; // how fast the camera responds to movement, higher = slower
[SerializeField]
private float rotationSmooth = 2; // how fast the camera responds to rotation, higher = slower
[SerializeField]
private float distance = 3;
[SerializeField]
private float height = 1.3f;
private Vector3 velocity = Vector3.zero;
void Update(){ // use FixedUpdate if following a rigidbody
if (target){
Vector3 targetPosition = target.TransformPoint(new Vector3(0, height, -distance));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, rotationSmooth * Time.deltaTime);
}else {Debug.Log("no target!");}
}
}