Question by
Rockyeahh · Jul 07, 2016 at 02:27 PM ·
movement2d-platformer
My player no longer moves
It used to move but now it doesn't and all I've done is correct the jumping script, so now it jumps but it does not move anymore. I'll post the script. The player is supposed to move ethier left with A or right with D and jump with the space bar. It worked two days ago bt today it does not. I was originally going to work on finding a way to stop the character moving after letting go of the button but I doubt I'll deal with that now this has happend.
This is also my first game.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public bool jump = false;
public float jumpForce = 10.0f;
private bool grounded = false;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D hit)
{
grounded = true;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.A))
{
rb2d.velocity = new Vector2(rb2d.velocity.x, speed * -1);
if (Input.GetKeyUp(KeyCode.A))
{
rb2d.velocity = new Vector2(0, 0);
}
if (Input.GetKeyDown(KeyCode.D))
{
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
if (Input.GetKeyUp(KeyCode.D))
{
rb2d.velocity = new Vector2(0, 0);
}
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed);
}
}
}
void Update ()
{
if (Input.GetButtonDown("Jump"))
{
rb2d.AddForce(Vector2.up * jumpForce);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Charaecter Flying Away 0 Answers
increase speed of a character costantly 1 Answer
Why is my character movement jerky/stuttry/jettry? 2 Answers
Activating a Script Causes Another to Activate 0 Answers
2D Platformer, Icy material? 0 Answers