Riding a second character controlled object

Hey unity people-

I have my hero, and a deer she rides. Both have a character controller. To ride the deer, I've said in script to attach the hero's center to a 'saddle' (an empty game object) I placed inside the deer, when I press the correct button and am in range of the animals side. She is able to get on perfectly in the right position, but when I press my 'run' button (makes the speed go from 7 to 20), the script seems to forget that I put

hero.position = saddle.position;

in the update function... The player is still riding, but she doesn't stay ON the saddle. The deer jerks and shakes forward and acts jittery when walking and more when running. It also makes the game crash after a while.

The thing is, sometimes when I load unity she is able to ride the deer PERFECTLY.

I think it is because their character controllers are colliding so it makes the deer jittery when moving.

The way I set up the script is to turn off the player's controll script and the deer's 'follow the player script', and enable a second script attached to the deer for an all new controlling script for riding.

I can't turn off the player's character controller because I instantly get hundreds of errors. Many things rely on the player current whereabouts- BUT, is there some way to turn off either's controller OR stop the deer from shaking indefinitely?

I have also tried to reposition the deer's controller but it doesn't seem to matter if the controller is no where near the hero or deer when riding- the deer always jitters.

here is the basic script for controlling the animal when riding-

function Update(){

   transform.rotation.z = 0;

var rider= GameObject.Find("Player").transform;

var saddle = GameObject.Find("saddle").transform;

var heros = GameObject.Find("Player");

rider.position = saddle.position;
rider.rotation = saddle.rotation;

var controller : CharacterController = GetComponent(CharacterController);

var forward = transform.TransformDirection(Vector3.forward);

var curSpeed = speed * Input.GetAxis ("Vertical");

transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);

controller.SimpleMove(forward* curSpeed);

 if  (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){
    animation.CrossFade("walk");

}else{
animation.CrossFade("idle");
speed = 7;

}
if (Input.GetButton ("Jump"))
if (controller.isGrounded) {
moveDirection.y=jumpSpeed;
animation.CrossFade("walk");

}if (Input.GetButton ("run")) {
speed = 20;

}if (speed >7){
animation.CrossFade("run");

}if (Input.GetButton("dismount")){
    hero.riding = false;
        heros.GetComponent(playercontrollscript).enabled =true;
GetComponent(cowboyscript).enabled = false;
GetComponent(followmescript).enabled = true;

}
}

Also, I do want the deer's x axis to rotate up or down when riding over the terrain instead of always being straight to the world- how do I allow it to rotate?

AND the deer cannot jump at all when the player is riding it. I also think it is because of the players controller. I tried to turn off gravity for both of them, but it doesn't work.

Any help is very much appreciated!

There are a lot of questions in there, and I can't answer all of them, but I think it would help you if instead of setting the players transform equal to that of the saddle each update, you just set saddle to be the parent of the players transform. That is,

var playertransform = GameObject.FindWithTag("Player").transform;
var saddle = GameObject.Find("Saddle").transform;
playertransform.parent = saddle;

This should execute during the mounting event. Then, during dismount, we set the players transform to have no parent:

playertransform.parent = null;

As far as the x rotation of the deer goes, what you want to look for is the normal vector of the ground you are standing on, which you want to be your "up" vector. As for how to execute that in code, I would start with looking at the code in the CharacterMotor.js script related to groundNormal. It uses `groundNormal = hit.normal;` to get the normal, and the AdjustGroundVelocityToNormal function to make sure we run across the ground.

private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 {
    var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity);
    return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
}

You could run the same calculations as that function and then set the rotation equal to looking along the velocity vector.

var forward = AdjustGroundVelocityToNormal (hVelocity, groundNormal);
transform.rotation = Quaternion.SetLookRotation(forward,groundNormal);

For the deer jumping, I got nothing, unless you want to completely restructure your approach so that you set the deer to be child of the rider, and change the riders controller attributes accordingly (speed, height from ground, etc). We wouldn't be controlling the deer directly anymore, but the player-deer combined object thing. This would make a lot of what I wrote previously fairly irrelevant, too.

You can use most of what Mickydtron said and I think that will all work, then to get jumping to work, I would guess it’s not working because the deer collides with the player controller. You can use Physics.IgnoreCollision(); in your deer script. Would be something to the extent of
Physics.IgnoreCollision(player.collider, collider);
Make sure that’s in your deers controller for when the player mounts it. Hope that helps!