- Home /
How do you make a characterController jump automatically when it reaches the edge of a platform?
I'm making a 2D game where several characterControllers (created as prefabs) move by themselves along platforms. I'm trying to make it so that they jump automatically when they reach the edge of a platform, but all of the jumping scripts that I've looked at so far don't seem to work. I have it so that the game recognizes when the character is touching the ground and begins moving them automatically, but that's it so far. Could someone just give me a basic idea of the lines that I will need to use to make this happen? Thanks for any help.
I'm thinking invisible objects at the edges? When you detect collision with one of them, you order a jump.
you can add colliders at the edges of your plattform. Raycast down from your player. If hit edge, jump automatically!
Answer by fafase · Jun 24, 2013 at 07:41 PM
2 solutions I can think of:
You could place a trigger plane at the end of each platform and when entering the plane, it jumps. Since you are in 2D unity:
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
bool onceJump = false;
void Start(){
controller = GetComponent<CharacterController>();
}
void Update() {
if(controller.isGrounded){
moveDirection = Vector3.zero;
moveDirection.x = 1;
moveDirection.x = speed;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider col){
if(col.transform.tag == "Player")moveDirection.y = jumpSpeed;
}
Other way is to use the isGrounded variable. As soone as your guy will start falling, bam it jumps.
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
bool onceJump = false;
void Start(){
controller = GetComponent<CharacterController>();
}
void Update() {
if (controller.isGrounded) {
moveDirection = Vector3.zero;
moveDirection.x = 1;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
onceJump = true;
}
else if (!controller.isGrounded && onceJump){
moveDirection.y = jumpSpeed;
onceJump = false;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Working and tested.
So if you are grounded, you walk, if you are not grounded, you jump but only once. If you are not grounded but you cannot jump, you are going down until you are grounded.
Thanks to those who responded. I might not have been clear in my question. I understand the idea behind when to make something jump (I've been going with the !isGrounded then jump method) to try and make it jump as soon as it starts falling, but I'm still pretty new to scripting and can't figure out how to simply make a CharacterController jump. I can do it with a rigidbody using rigidbody.AddForce(transform.up/right), but I'm confused at how to achieve a similar effect using a CharacterController. I think:
moveDirection.x = speed;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
is what I am looking for, but unfortunately I am getting errors when I put this script in. Would you be willing to break some of this down to explain what variables and commands I need for this?
Hmmm. I wonder if you are using a different version than I am, because I put the exact code that you listed and it's still giving me lots of errors. I should note that I am using Javascript.
The code above is C#. You would need to put this code in C# scripts to get them to compile in Unity.
As an afterthought, next time you ask something, include either the js or c# in the tags :)