- Home /
World position question
I want the camera to sit in front of my head bone, and I can't attach it to the head bone because I only want the position of the head bone, not the rotation. I have tried this:
function LateUpdate() {
camera.position = Head.position;
//The camera seems to be set to the head bones localPosition
}
But that doesn't seem to make the position the same. It sets it to the initial position but doesn't follow if I change the rotation of the head bone.
If you take the transform of the head bone, you could set the transform.position and be fine. I would do it that way and redefine the camera's transform rotation to take a persistent leveled value. $$anonymous$$aybe this requires you to set the camera in an empty child?
localPosition just make it stay at the bottom of the map, where the Head position would stay at its initial position.
If I try to use this code:
function LateUpdate() {
Debug.Log(Head.position.ToString());
camera.position = Head.position;
}
The bone doens't change position if I change the rotation of the parent bones?
Your code doesn't affect bones in any way, just the camera.
Answer by incorrect · Jan 11, 2015 at 12:05 PM
using UnityEngine;
using System.Collections;
public class AttachCamera : MonoBehaviour {
public Transform target;
void Update () {
transform.position = target.position;
}
}
Just assign the target object in inspector and camera with that script will follow the object.
Btw, localPosition, localRotation are values relative to paren object, so they are useful when you want to adjust those position and rotation of an object relatively to it's parent. In your case you should use global world position.
I don't think I can use Update since the bone is included in an animation, so the animation will override the movement. That is exactly what I have been doing since the start.
This script has nothing to do with your animation. Ok, I'll check it.
Well, you know, in my case it worked like I've expected. I just assigned right body part from hierarchy of character and camera's position always followed it through animation.
Or am I missing something? Please, can you show me your character's hierarchy and describe what actually you want to make. Not how you think it can be done by setting position and all that coding, just tell how you want your camera move. Do you want it to follow characters head and be faced the same direction? Do you want it just to move and shake as the head does, but be faced some other direction? Or you have something else in $$anonymous$$d?
The problem might be that the camera is a child of the character. I just want it to stand still in front of the head.
Oh, c'mon, just don't make it a child of a character. :)
Or I can improve my script so it will work even though the camera is a child of your character. Just tell me, where in hierarchy you want it to be. Should be just a charater's root's child?