Backsliding like a baws
Hella people !
Im't actually trying to tweak a bit the walking script of the tutorial for 2d games. Here's the thing : using UnityEngine; using System.Collections;
public class CharacterControllerScript : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (Mathf.Sign(move) * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
As you can see, I've replaced the common "move maxSpeed" by "Mathf.Sign(move) maxSpeed". The purpose is to prevent speed variations with a joystick, so the gameplay's still the same with a keyboard and a gamepad. Also I've added kind of a "startup" and "recovery" frames, using the "curve" fonction and modulating the maxSpeed during "start walking" and "return to idle" animations.
Now here's the problem :
When the char is going left and I instantly stop to press the left key, the char is sliding backward. I've tried to test this with a keyboard and the reaction is the same. I've tried to suppress the "recovery" frames and it worsen the problem ; turning the char into an unstoppable backslide masta'.
Edit: the problem comes from the fact that "mathf.Sign()" can never return the 0 value, since it is returning 1 as for 0 or positive numbers, so if I keep suing this function I need to find a way to return 0 when the player doesn't press anything.
So if someone has a solution to keep the "no variation in speed" gameplay style, without using a clunky "if", and keeping the "startup" and "recovery" frames : I'll thank him greatly. ^^
Answer by WhyldH · Jun 03, 2016 at 04:58 PM
resolve the problem with this : using UnityEngine; using System.Collections;
public class CharacterControllerScript : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
float side = 1;
anim.SetFloat ("Speed", Mathf.Abs (move));
if(facingRight == true)
side = 1;
else
side = -1;
if (move != 0)
rigidbody2D.velocity = new Vector2 (Mathf.Sign(move) * maxSpeed, rigidbody2D.velocity.y);
else
rigidbody2D.velocity = new Vector2 (side * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Is there anything better to do ? I mean, adding a variable isn't really optimized isn't it ?