- Home /
2D Sidescroller Issues Please help.
Hello. I am new to Unity and I am having trouble getting a way of controlling my character when making a 2D Sidescroller type game. The main trouble I have is with scripting all help and suggestions are appreciated. Once again I am a total novice In the programming/scripting world so please have some patience. Thank you. (I wouldn't be opposed to someone posting their controller in the responses and I can compare that to mine.)
Here is my controller so far. (hasn't been working.) using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float jumpHeight;
public float moveSpeed;
//use this for initialization
void Start () {
}
//update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> (). velocity.x,jumpHeight);
}
if (Input.GetKey (KeyCode.D)) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed,GetComponent<Rigidbody2D> ().velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed,GetComponent<Rigidbody2D> ().velocity.y);
}
}
}
Answer by Guppie1337 · Jun 16, 2015 at 12:18 PM
Unity has some very good examples of how to do this if you watch some of the tutorials they provide.
If you're not interested in following that tutorial, I'll give you a quick rundown of how things workout in the script.
//You want player movement in FixedUpdate.
void FixedUpdate ()
{
//Create variables to store your values.
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Your movement vector.
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
//Move the player.
GetComponent<Rigidbody2D>().velocity = movement;
}
What's happening here is Input.GetAxis changes values by the left, right, up, down arrow keys. You can use other keywords or even make your own through Edit -> Project Settings -> Input. They are basically Vector2 with coordinates of values from either -1 to 1. e.g (1, 0) = right, (-1, 0) = left, (0, 1) = up, (0, -1) = down.
When FixedUpdate goes through, if you are pressing any of those keys during that frame, it will return one of those values. Your player will then change his velocity for every frame it has a value.
A very common mistake is that people don't quite understand that when you change a rigidbody velocity, you are changing it for good unless you change it again somehow. The reason this code works to stop your character when you want is because when you aren't pressing anything, the value returned is (0, 0), thus bringing your player's velocity to a stop.
Your answer
Follow this Question
Related Questions
How do I stop the player from moving offscreen? 2 Answers
Changing player's moving direction 0 Answers
Please Help - 2D Collisions Without Unity's Physics 1 Answer
My 2D ball keep falling through ground 0 Answers
2D C# destroy a GameObject on collision 2 Answers