Hi guys,
first off, I will post my script. It uses rigidbody2D.AddForce to move the character. I also have a GUI Element that shows me the current Speed. However, there are some problems. First off, the script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 100f;
public float maxMoveSpeed = 15f;
private float currentMoveSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.LeftArrow)) {
rigidbody2D.AddForce (Vector3.left * moveSpeed);
transform.localScale = new Vector3 (-2, 2, 2);
}
if (Input.GetKey (KeyCode.RightArrow)) {
rigidbody2D.AddForce(Vector3.right * moveSpeed);
transform.localScale = new Vector3 (2, 2, 2);
}
if (rigidbody2D.velocity.magnitude > maxMoveSpeed) {
rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxMoveSpeed;
}
currentMoveSpeed = rigidbody2D.velocity.magnitude;
}
void OnGUI() {
GUI.Label (new Rect (50, 25, 200, 30), "Current Move Speed:"+currentMoveSpeed.ToString ());
}
}
1.) My first problem is the movespeed and maxmovespeed. Generally this works, but it takes way too long for the character to reach the max speed. That’s why my moveSpeed is set so super high. However, when I just tap the key for 1 frame the movementSpeed goes higher than the maxMoveSpeed.
What can I do to get a quick acceleration to my desired speed without having this issue?
2.) Related to the first point, but also an individual problem: When I let go of the button, my character slides really, really far. If this is not completely related to one, what do i do do quickly decrease the momentum as to not have too much sliding?
Note that I have little C# experience and wrote all of this myself using the Coding API. So if there’s any general improvements to be made too, it would also be good to know. I’m still learning