- Home /
How can i make my 2D character not toppling over
Hello i am making a 2D game where my character is running to the right side over the screen and when he starts running he is falling over and my character is moving to the right side over the screen on his stomach i've tried a lot like freeze the z rotation and when i do that i can't jump with him maybe i made a mistake in my script i dont know pls. help me
here is my script:
using UnityEngine; using System.Collections;
public class PlayerScript : MonoBehaviour { public float maxSpeed = 10f; public float jumpForce = 700f; public bool grounded = false; public Transform groundCheck; private float groundRadius = 0.2f; public LayerMask whatIsGround;
void Update ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
GetComponent<Rigidbody2D>().velocity = new Vector2 (1 * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
}
Hello ;)
I dont know how to keep my character run on his feet. Is there someone there know a way how to do it?
Freezint z rotation should work. Why wouldn't you be able to jump with freezed z rotation?
Bonus tip: Use proper interpunctuation in your sentences so they are easier to read.
I dont know what there is wrong, when i click on my character and go under the Constraints and set a x in the Freeze rotation z i dont tripple over, but i can't jump i don't know why. Thanks for the bonus tip.
Looks like the problem lies with the jumping code, then. If you post all the code related to jumping, perhaps someone can help you.
Your answer
Follow this Question
Related Questions
Rotate a cube using touch 0 Answers
Rotation control (C#) 1 Answer
Trying to Add jump to this code. 1 Answer
How to stop MoveRotation when rigidbody is facing a specific euler angle 2 Answers
Does rotation increase velocity? 0 Answers