Hi guys!
So I just started using Unity and I have watched some tutorials. The problem that I am facing is player movement. Every tutorial I have stumbled across has some kind of acceleration along the x axis (I am making a 2D game). My goal is to go to top speed instantly and stop instantly too. This is how my current script looks like:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
float Speed = 7.5f;
float rot = 90f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float moving = Input.GetAxis ("Horizontal") + Speed;
float rotating = Input.GetAxis ("Horizontal") * rot;
if (Input.GetKeyDown ("a")) {
rigidbody2D.velocity = new Vector3 (-moving, 0, 0);
}
if (Input.GetKeyUp ("a")) {
rigidbody2D.velocity = new Vector3 (0, 0, 0);
}
if (Input.GetKeyDown ("d")) {
rigidbody2D.velocity = new Vector3 (moving, 0, 0);
}
if (Input.GetKeyUp ("d")) {
rigidbody2D.velocity = new Vector3 (0, 0, 0);
}
if (Input.GetKeyDown("a")) {
transform.Rotate (0, 0, 90);
}
if (Input.GetKeyUp("a")) {
transform.Rotate (0, 0, -90);
}
if (Input.GetKeyDown("d")) {
transform.Rotate (0, 0, -90);
}
if (Input.GetKeyUp("d")) {
transform.Rotate (0, 0, 90);
}
}
}
I know that this is not the best way to do it. Users will not be able to change key input and so on. Also, I noticed that if I press a and d fast after each other my sprite rotation will screw up (I has to be 90 when going right, 0 when standing still and -90 when moving left, nothig else).
So my question is how can I impove this without adding acceleration to topspeed and from topspeed to 0?