- Home /
Question by
junkrar · Mar 23, 2013 at 09:18 PM ·
physics2d-platformer
Add Physics to Character Movement
I'm not new to programming, but I'm new to Unity. I'm making a basic 2D platformer with movement defined as follows:
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
float rotation = Input.GetAxis("Horizontal");
if(controller.isGrounded)
{
moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
//running code
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
{ running = true; }
else
{ running = false; }
moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
//jump code
if(Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpHeight;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
This is currently alright, but it has some flaws. For example, the jumping continues even if I hit a ceiling (so it sticks to the ceiling until the jump is complete) rather than stopping right away. So I was wondering, how can I use Unity's physics to improve this character's design?
Comment
Your answer
Follow this Question
Related Questions
PlatformerController: is inertia (Mario physics) possible ? 4 Answers
2D Physics game using sprites 0 Answers
2D moving platforms, physics issues 1 Answer
2D 360 degress platformer example needed 0 Answers
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer