Player sometimes don't stop moving when Key is release, and is properly installed in the fixedUpdate()
I'm trying to make a character move when a key is pressed and stopped when the key is release, the script works, but sometimes (i.e. not always), for some reason, the speed do not return to 0 and the character continue to move after the key is release, call me a perfectionist, but i don't want players do not stop when they need to, it need to be reliable.
Here is the code:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class CharacterPlus : NetworkBehaviour {
private Rigidbody2D rb2d;
void Start () {
if (isLocalPlayer) {
rb2d = this.GetComponent<Rigidbody2D>();
}
}
void Update()
{
Debug.Log ("Normal Velocity: "+rb2d.velocity+" Inertia Debug: "+rb2d.inertia);
}
// Update is called once per frame
void FixedUpdate () {
//input_x = Input.GetAxisRaw ("Horizontal");
//input_y = Input.GetAxisRaw ("Vertical");
if (isLocalPlayer) {
if (Input.GetKeyDown (KeyCode.D) || Input.GetKeyDown (KeyCode.RightArrow)) {
Move (18f, 0.0f);
}
else if (Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.LeftArrow)) {
Move (-18f, 0.0f);
}
if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.UpArrow)) {
Move (0.0f, 18f);
}
else if (Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.DownArrow)) {
Move (0.0f, -18f);
}
if (
Input.GetKeyUp (KeyCode.D) || Input.GetKeyUp (KeyCode.RightArrow) ||
Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.LeftArrow) ||
Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.UpArrow) ||
Input.GetKeyUp (KeyCode.S) || Input.GetKeyUp (KeyCode.DownArrow)
) {
Stop ();
}
}
}
void Move(float h,float v){
Vector3 movement = new Vector3 (h,v, 0f);
rb2d.AddForce (movement*70);
}
void Stop() {
rb2d.velocity = Vector3.zero;
rb2d.inertia = 0f;
}
}
Any question, comment, idea or request for clarification would be much apreciated too.
Thanks in advance
indeed, another questions like this one already exist, but or they aren't responded, or aren't using the same formula than me, also, none of them have the problem of reliability of the stop, them can't stop at all.
So my problem is reliability to stop when needed, not actual stop, hence, i publish a new question for it.
¡¡¡Cheers up!!!
Answer by Obsdark · Jan 04, 2017 at 01:38 AM
Fixed
i just use:
if (!Input.anyKey) {
Stop ();
}
instead of:
if (
Input.GetKeyUp (KeyCode.D) || Input.GetKeyUp (KeyCode.RightArrow) ||
Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.LeftArrow) ||
Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.UpArrow) ||
Input.GetKeyUp (KeyCode.S) || Input.GetKeyUp (KeyCode.DownArrow)
) {
Stop ();
}
Thanks in advance for that
Your answer
Follow this Question
Related Questions
Move Player along normal of floor? 1 Answer
How to tidy up my code for Unity 5 Basic Platformer 1 Answer
Problem with force movement when calling from another scripts 0 Answers
2d Rigidbody how to check if Player is moving towards the mouse 0 Answers
How do I prevent a rigidbody2D from falling through the ground? 0 Answers