- Home /
Issue on moving using CharacterController
Hi everyone.
I have a issue with my project, which is a 2D platform game where the player controls two characters in the same enviroment using the same controls (if one character moves, the other moves too).
I have two cubes on my scene. Each cube must move and maintain the distance between each other in the X axis. To do this, I modified a script that I find on the Unity forum. I attached the script on both cubes (one named Aubert and other named Jan). Basically, all that the script do is prevent that one cube moves while the other is blocked by some object. I use the character controller to manage the collisions.
My issue is when the player jump and one of the cubes collides with an object, they move in the X axis from different distances, they lose the sync. I think that is because the character controller (I already tried change the character controller settings, with no results).
Here comes the code. "JogadorAntigo" is the name of script that I attached on the cubes.
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false; private var player; private var collider2;
//debug private var debug; private var debug2;
function Start() { //on begin, each script points to the other if (gameObject.name == "Aubert") { player = gameObject.Find("Jan"); } if (gameObject.name == "Jan") { player = gameObject.Find("Aubert"); }
collider2 = player.GetComponent(CharacterController);
} function FixedUpdate() {
if (gameObject.name == "Aubert") { //debug debug = gameObject.Find("debug1"); debug2 = gameObject.Find("debug2");
debug.guiText.text = "Vetor Forcas Jan:" + player.GetComponent("jogadorAntigo").moveDirection;
debug2.guiText.text = "Vetor Forcas Aubert: " + moveDirection;
}
if (!grounded) { moveDirection.x = Input.GetAxis("Horizontal")*speed; } else { moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, 0.0); if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; } } moveDirection = transform.TransformDirection(moveDirection); if(collider2.collisionFlags == 5 || (collider2.collisionFlags & CollisionFlags.Sides)) { moveDirection.x = 0; player.GetComponent("jogadorAntigo").moveDirection.x = 0; }
// Apply gravity moveDirection.y -= gravity * Time.deltaTime;
// Move the controller grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Any way to sync the two objects (that I'll replace for models later) will be helpful.
I hope you guys forgive my poor english...
Answer by StephanK · Jun 23, 2010 at 11:41 PM
If you attach this script to both of your characters and one of them collides with an object it won't move in the direction of the collision, but the other one will. If you want to keep the characters at a fixed x-offset you could just set transform.position.x of one character to otherCharacter.transform.position.x - offset. but this may look odd if one character collides with something and the other just stops moving with no obstacle visible.