I am looking for a way to attach my camera to the head of my character controller model. I can’t make it a child of the head object or the camera itself will shake when running. I’m new to this, does anyone know how to make a script to smoothly attach the camera to the head while it animates without view-bobbing?
Make the bobbing head and the camera siblings, both children of an empty object which you will use for any rotation of the head.
Yes, but I can not give you any solid ideas unless I know exactly how you are doing it.
For one, Parenting will not work out well you want something more along the lines of:
transform.position = PlayerPhysFeet.transform.position - FeetGroundC + offset;
transform.rotation = Quaternion.Slerp(transform.rotation, Turnto, Time.deltaTime * Turnspeed);
That way you have direct control over what axis gets transferred to the object. (This example is a bit overcomplex and is set up to compensate body position with IK movement.)
One really REALLY important question, because this WILL effect your base script for it. Will “UP” always be a static direction in your game/sim? Or do you intend to have your char walk on walls/ceilings/round planets with spherical gravity?
I am looking to make a basic first person game so the directions should stay static. I want to keep the camera in the same position ahead of the head so when my character model does different animations, the camera stays in line without any bobbing.
Assuming you can get the position of the head, you can use transform.position as is, as I did in the example script.
So whenever you call transform.position in a script you are essentially addressing the exact global coordinates of whatever object the script is on in the X Y and Z.
Those come out as a Vector3(X,Y,Z)
So, all you need to do is remove Y from that and replace it with whatever the cam is currently sitting at.
Prolly look somthing like new Vector3(headPosition.x,transform.position.y,headPosition.z);
EDIT: Keep in mind this will NOT rotate the object/cam. You wanna “look” into Transform.Lookat. If you use it right it will make the cam rotate to the head of your char. (If you want smooth movement or rotation look into slerp/lerp s.
If you want the camera to smoothly follow the player, it probably has to be a child to the player.transform. For some reason not even fixed update can’t follow objects without parent-sibling relation without adding some degrees of rugged, jerky, mismatched, vibrating effect to it.
https://forum.unity.com/attachments/bootlegorbitalrig-gif.747577/
Looks pretty smooth to me. That is using another object parented, but the actual camera is using that exact snippet of script I posted above to track the center object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// You need to create an empty, attach this script to it, then parent your camera to that empty. After that just drag your char object to Public Transform player
public class TestCamRotatanator5000 : MonoBehaviour
{
#region Fields
private Vector3 targetchords;
// | CAMERA WILL MATCH ROTATION OF ANY OBJECT YOUR STICK IN HERE|
// v v
public Transform player;
// ^ ^
// | |
public float Turnspeed = 2.0f;
public Quaternion Turnto;
[Header(header: "Offset")]
public Vector3 offset;
#endregion
private void Start()
{
targetchords = player.transform.position;
}
void Update()
{
Turnto = player.transform.rotation;
transform.position = player.transform.position + offset;
transform.rotation = Quaternion.Slerp(transform.rotation, Turnto, Time.deltaTime * Turnspeed);
}
}
EDIT1: Actually after double checking, it might be the Slerp that is making it smooth… Not sure though. I tosses that little demo scene out a while back and I don feel like rebuilding it. Regardless, If it isn’t smooth, then just Slerp/Lerp it and set the speed to something insanely high.
EDIT2: You might have some other underlying issues in your script if you are getting jerkiness for no apparent reason. Have you attempted to set up a ultra simplified version with just a plain oject and camera? Might also be worth trying to stick it in LateUpdate. It might be your animator fighting you (I had a bunch of issues with animators until I learned about late update.)