- Home /
controller.Move not working correctly
Hi, I am having a problem with moving a gameobject with a collider attached. I am using the collider.Move function: when I press "space" to jump, my character quits moving forward shortly after leaving the ground. If I increase my jump height, the character will move for part of the jump, but not all of it. The character always moves on the downward part of the jump and the first part of the upward part of the jump. Soon after leaving the ground, all movement on the x axis stops. I have done some logging, and the velocity.x remains a constant 1.5 this whole time. My character consists of a gameobject with a character controller, script, and animation attached. It also has a armature system attached as a child. I have completely disabled all animation, with no better results. I will attach my script as a reference.
#pragma strict
var playerMoveSpeed : float = 1.5;
var jumpVelocity : float = 6.2;
var gravity : float = 20.0;
var moveDirection : int = 0; //reference for changing the direction the knight is facing
var controller : CharacterController;
private var velocity : Vector3;
function Start ()
{
controller = GetComponent (CharacterController);
}
function Update ()
{
if (velocity.x > 0) //sets move direction
{
moveDirection = 0;
}
if (velocity.x < 0) //left
{
moveDirection = 1;
}
if (moveDirection == 0) //changes knight direction based on move direction, right
{
transform.localScale.z = 1;
transform.eulerAngles = Vector3(0,180,0);
}
if (moveDirection == 1) //left
{
transform.localScale.z = -1;
transform.eulerAngles = Vector3(0,0,0);
}
velocity.x = -1.5; //Input.GetAxis("Horizontal") * playerMoveSpeed; I hardcoded the movement in to make sure I don't have an input issue.
if (controller.isGrounded) //allows jumping while on ground.
{
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jumpVelocity;
}
}
velocity.y -= gravity * Time.deltaTime; //apply gravity
controller.Move(velocity * Time.deltaTime); //actually does the moving on character controller
print (velocity.x);
print (velocity.y);
print (Time.deltaTime);
}
Wow, I think I figured out how to fix this, although I really don't understand what is CAUSING the problem.
I had another gameobject as a child of my main character, and the child had a box collider attached. Enabling the trigger on the box collider solved the problem. Why this is, I have no idea, and if anyone could shed some light on that, I would much appreciate it. Thanks!
Well, you can make sure the two objects don't collide with each other, by putting them on different layers, then disabling collision between those layers. I actually have had a few problems like this, where objects would suddenly decide to accelerate into the air, never to be seen again, because of colliders interfering with each other!
Ok, If I come across a case where setting the collider to a trigger doesn't work, I will try the layer trick.
Um, noob question here, but how do I mark this question as answered?