- Home /
Issues with my 2D movement script & rigidbody2D
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 ());
}
}
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?
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 :)
Answer by robertbu · Jul 17, 2014 at 03:38 PM
For #2, select your object in the Hierarchy, then in the inspector, increase the 'linear drag'. Note may also have to further increase 'moveSpeed' to compensate.
You can also do a pseudo-drag this way (executed in FixedUpdate()):
rigidbody.velocity = rigidbody.velocity * 0.95f;
It behaves a bit differently than the 'drag' setting, and adjust the 0.95f to fit your situation.
You need to change Update() to FixedUpdate() in your code. The frame rate of Update() varies based on load, and based on the device, so you ship will have varying behaviors if you use Update(). Note that you do not want to read single key input in Update() like GetKeyDown(). Since FixedUpdate() and Update() run at different frame rates, you can lose input if you use GetKeyDown() in FixedUpdate(). But continuous input like GetKey() is generally okay.
Your 'moveSpeed' is misnamed. It would be better named 'moveForce' since it is only indirectly related to speed. How much force you need to apply to get a perceived movement is a factor of the mass of the object and the scale of your scene. So if you need it to come up to speed faster, just make the number bigger.
when I just tap the key for 1 frame the movementSpeed goes higher than the maxMoveSpeed
I see nothing in the code that would account for this. Any chance you have some sort of collision going on? Put an OnCollisionEnter2D() callback on your object to check.
Note for future questions, on Unity Answers, we like to see single, specific questions. Multiple questions are harder to answer and you end up with situations like this one where I hand some but not all the answers. Often multi-question posts are rejected or closed.
Oh, sorry, I didn't know about the multiple/individual question thing. I will keep that in $$anonymous$$d for the future.
Your answer worked perfectly fine. I probably just thought the movespeed went higher because i immediately "flung" the character in one direction and the sliding was faster than the actual walking.
Thanks for clearing up the issue with the Update/FixedUpdate too, i did not know about this.