charactercontroller.Move Jump not working!

why won’t this work? instead of pressing a button, on collison I want this enemy character controller to jump up…and be affected by gravity e.t.c . It should work, should’nt it? it sucessfully prints “afaf” once it collides… Also I am fairly new to coding.

   var jumpspeed = 35;
var up;
var controller;
var isJumping: boolean = false;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
up = (Vector3.up);
}

function OnControllerColliderHit(hit:ControllerColliderHit)
{
print("atleastwerehere");
if (hit.gameObject.tag == "Jumppad")
{
print("afaf");
controller.Move(up*jumpspeed);
}
}

You are heading in the right direction, but there are some things you could improve on.

Use a variable called moveDirection

Use that to jump

Something like this:

var jumpSpeed = 35;
private var up = (Vector3.up);
var controller;
var isJumping: boolean = false;
private var moveDirection : Vector3 = Vector3.zero; 

function Update()
    {
    var controller : CharacterController = GetComponent(CharacterController);
    }

function OnControllerColliderHit(hit:ControllerColliderHit)
    {
    print("atleastwerehere");
    if (hit.gameObject.tag == "Jumppad")
        {
        print("afaf");
        moveDirection.y = jumpSpeed;
    }
}

It should look something like that. It is much better and works really good.

  • Code is untested.

Thank you all so Much!!!

I finally got it to work, I never would have had it not been for you guys :confused:

Here is the final code: it may look funny but my Astar AI is pretty fickle about movements.

var jumpSpeed = 35;
var controller;
var gravity = 20.00;
var isJumping: boolean = false;
var Jump : boolean = false;
private var moveDirection : Vector3 = Vector3.zero; 

function Update()
    {
    var controller : CharacterController = GetComponent(CharacterController);
	if (controller.isGrounded)
	{
	isJumping = false;
	}
	if (Jump)
	{
	isJumping = true;
	}
	if (isJumping)
	{
	Jump = false;
	controller.Move(moveDirection * Time.deltaTime);
	}
	moveDirection.y -= gravity * Time.deltaTime;
}
function OnControllerColliderHit(hit:ControllerColliderHit)
    {
    print("atleastwerehere");
    if (hit.gameObject.tag == "Jumppad")
        {
        print("afaf");
		Jump = true;
        moveDirection.y = jumpSpeed;
    }
}

again thank you all!