- Home /
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);
}
}
Answer by FLASHDENMARK · Oct 01, 2011 at 07:28 AM
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.
Where it says:
private var up = (Vector3.up);
you can just delete that line of code if you want it is not used.
PS. I am OrangeLightning
No use.....it doesn't even print the "afaf". I wonder what the issue is.... I'll work on it some tomorrow.
If it does not print "afaf" are you sure that you are using a CharacterController? or the gameObject is tagged "Jumppad"?
ehmm, you forget to actually move the CharacterController in Update ^^
add the line:
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
at the end of your Update function.
You also forgot to implement gravity, so you're not jumping. You're a little rocket...
Don't put character = GetComponent(CharacterController) in update. You're wasting processing power by getting the component every frame. Put it outside of a function and assign it in the inspector.
Answer by TehWut · Oct 01, 2011 at 05:53 PM
Thank you all so Much!!!!
I finally got it to work, I never would have had it not been for you guys :/
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!
I'm trying to do something similar using the default Character Controller+Character $$anonymous$$otor set up and I've got it mostly working, but here's my problem. I get a $$anonymous$$UCH bigger kick from my jump pad if I jump up when I hit it because my character motor also has upward velocity that it's applying. Likewise, if I jump down onto the jump pad, I get almost to boost at all. I'm new to coding and the $$anonymous$$otor script is strange and scary to me! How could I make it so my jump pad gives the same kick regardless of my character's current velocity?
It's not working
NullReferenceException: Object reference not set to an instance of an object jumper.Update () (at Assets/scripts/jumper.js:12)
if (controller.isGrounded)//line 12
I have a character motor (default fps controller prefab) would that cause the error? Am I supposed to check Jump or IsJumping?