Simple follow script

Hi, bit of a n00b to Unity, I'm trying to get get a character model to ride on top of a horse. Rather than parent him in the hierarchy I want him to follow the horse using a script, as he will dismount it on occasion. I thought I'd worked out a really simple way of doing this using the following:

var target : Transform;
var SaddleHight = 0.08;

function Update(){

transform.position = Vector3(target.position.x, target.position.y - SaddleHight, target.position.z);
transform.rotation = target.transform.rotation;

}

... where the target is a particular joint on the horse. Unfortunately, although this works, when the horse starts to move or turn quickly I notice that the character attached starts to gitter. Is there a better way to go about this?

Thanks!

Yes. Try:

var rider:GameObject;
var horse:GameObject;

function Update(){
    if (Input.GetButtonDown("Ride")){
        rider.transform.parent = horse.transform;
        rider.transform.localPosition = Vector3.zero;
        rider.transform.localRotation = Quaternion.identity;
    }

    if (Input.GetButtonDown("Dismount")){
        rider.transform.parent = null;
    }
}

Then set up Ride and Dismount keys in the Input Manager. There's no good reason not to use the parenting functions... it's the easiest and simplest way to do this. If necessary, put an empty object on the horse where you want the rider to sit, and parent it there (so you don't have to play around with the right offsets on your localPosition). But use parenting!

Assuming that your horse is being moved in Update() the problem might be that you are trying to move both rider and horse at the same time. Try moving the code that updates the rider when he is mounted into LateUpdate(). That way, each cycle the horse will first move to new position and then the rider will move to match after.

As Lambda Red says, it is undefined in which order a Unity-event is called on components. I do however not agree that LateUpdate() is your answer.

Why not? Update() +LateUpdate() can ONLY solve event ordering problems for pairs of objects, but NOT if you have 3 or more components that are dependent on each others.

what then? You have to make a system yourself. Here are some possible approaches:

1) Update the chain of dependent components at once. In your case just update the rider WHENEVER the horse has been updated. Yes, the horse can just call the rider explicitly. Con: You can get multiple update calls on your rider, if it depends on other objects e.g. the camera.

2) Explicitly control the event ordering yourself Only have a single game object with a component that implements the unity events e.g. Update() and lets this event central be THE ONLY ONE that distributes unity events to other components. The other components just register themselves on THE ONLY ONE with an event priority value. To check that some in the team do not make components that implement unity events, you can use reflection to check that only THE ONLY ONE declares unity events. Run this check when the game starts.

in the Unity docs LateUpdate is suggest, but this created also jittering with my camera following an object. Using FixedUpdate solved the problem. Otherwise I would also go for the parenting method.