- Home /
Error jumping and moving
I'm having trouble with the drive system with perssonagens script in C #. I use the following code:
private Vector3 moveDirection = Vector3.zero; private bool grounded = false;
public float speed = 10.0f; public float jumpSpeed = 8.0f; public float gravity = 20f; public int countJump = 0; public int maxJumps = 2;
void OnCollisionEnter(){ countJump = 0; }
void FixedUpdate () { if(!grounded){ moveDirection.x = Input.GetAxis("Horizontal")*speed; moveDirection.z = Input.GetAxis("Vertical")*speed; }else{ moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); countJump = 0;
} if(countJump < maxJumps){ if(Input.GetButtonDown("Jump")){ moveDirection.y = jumpSpeed; countJump ++; } } moveDirection = transform.TransformDirection(moveDirection);
moveDirection.y -= gravity * Time.deltaTime;
CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if (Input.GetKey (KeyCode.LeftShift)){ speed = 23.0f;
}else{ speed = 10.0f; } } }
some sample images:
1: http://imageshack.us/photo/my-images/585/90786173.png/
2: http://imageshack.us/photo/my-images/854/84060635.png/
I tried both ways but did not get a good result. 1> Slow Motion, High Jump. 2> Move fast, Jump Low; What should I do?
Sorry my bad english!!
Answer by Goliath520 · Nov 30, 2012 at 08:54 AM
Unfortunatly I don't have a solution for you. I have been looking over it for a little bit and don't know what is wrong. I did however clean up the code so it is a little easier to read.
private Vector3 moveDirection = Vector3.zero;
private bool grounded = false;
public float speed = 10.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20f;
public int countJump = 0;
public int maxJumps = 2;
void OnCollisionEnter()
{
countJump = 0;
}
void FixedUpdate ()
{
if(!grounded)
{
moveDirection.x = Input.GetAxis("Horizontal")*speed;
moveDirection.z = Input.GetAxis("Vertical")*speed;
}
else
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
countJump = 0;
}
if(countJump < maxJumps)
{
if(Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpSpeed; countJump ++;
}
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection.y -= gravity * Time.deltaTime;
CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if (Input.GetKey (KeyCode.LeftShift))
{
speed = 23.0f;
}else
{
speed = 10.0f;
}
}
}
The other thing that isn't a big deal but you can use to avoid extra work is using && in an if statement to avoid unnecessary nesting. I am refering to the jump portion of the code where you used:
if(countJump < maxJumps)
{
if(Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpSpeed; countJump ++;
}
}
Instead it might be easier to use:
if(Input.GetButtonDown("Jump") && countJump < maxJumps)
{
moveDirection.y = jumpSpeed; countJump ++;
}
Hopefully someone can answer this for you. If I figure it out I will comeback and respond.
Your answer
Follow this Question
Related Questions
Player can jump on non ground items 1 Answer
How to only Jump on Ground Items 1 Answer