- Home /
Movement/Jump help
Heya guys, I'm working on a runner style games and I have the base controls down. I only need to stop the player from being able to move during the jump.
This is what I've gotten so far but have had little luck. :(
void Update ()
{
bool grounded;
if (state != State.Hit)
{
// Amount to move
float amtToMove = Input.GetAxisRaw ("Horizontal") * PlayerSpeed * Time.deltaTime;
// Move the Player
transform.Translate (Vector3.right * amtToMove);
// Wrap
if (transform.position.x <= -6f)
transform.position = new Vector3 (-6f, transform.position.y, transform.position.z);
else if (transform.position.x >= 6f)
transform.position = new Vector3 (6f, transform.position.y, transform.position.z);
if (transform.position.z <= -0f)
transform.position = new Vector3 (transform.position.x, transform.position.y, -0f);
else if (transform.position.z >= 0f)
transform.position = new Vector3 (transform.position.x, transform.position.y, 0f);
}
if (Physics.Raycast (transform.position, -transform.up, 2))
{
grounded = true;
//reset our jump count since we hit the ground //0 for double jump
jumpCount = 1;
Instantiate (DustPrefab,spawnA.position, Quaternion.identity);
}
else
{
grounded = false;
float amtToMove = Input.GetAxisRaw ("Horizontal") * 0.1f * Time.deltaTime;
}
if (Input.GetKeyDown ("space") && (grounded || jumpCount < 1))
{
state = State.Hit;
rigidbody.AddForce (0, 10, 0);
rigidbody.AddRelativeForce (transform.up * jumpForce, ForceMode.Impulse);
Instantiate (JumpPrefab, transform.position, Quaternion.identity);
state = State.Playing;
}
}
Or even dull down the movement.. It's looks like the players hits an invisible wall mid air.
Can't you do something like:
// $$anonymous$$ove the Player
if (grounded)
transform.Translate (Vector3.right * amtTo$$anonymous$$ove);
Answer by ryba · Jan 17, 2013 at 05:23 PM
Simple add CharacterController component to your player game object, and use Move method to deal with moveing, just like in this example: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Your answer
Follow this Question
Related Questions
Slowing player speed when in air 1 Answer
Jumping while moving 1 Answer
C# let a character jump 2 Answers
Jump Further When Key Is Held Down 1 Answer
Mecanim tutorial issue with jump 2 Answers