Question by
Jonneixx · Dec 14, 2017 at 07:44 AM ·
scripting problemnot working
Trying to follow tutorial -2D Character Controllers- but it's outdated. Help?,I've been trying to follow the tutorial named - 2D Character Controllers - but I guess it doesn't work anymore
Using this code, but it doesn't work on my Unity version, it seems.
using UnityEngine; using System.Collections;
public class HeroAnimationScript : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
private Vector2 velocity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis("Horizontal");
velocity = new Vector2(move * maxSpeed, GetComponent<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;
}
} ,using UnityEngine; using System.Collections;
public class HeroAnimationScript : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
private Vector2 velocity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis("Horizontal");
velocity = new Vector2(move * maxSpeed, GetComponent<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;
}
}
Comment
P.S. This is the error message I recieve:
Assets/Scripts/HeroAnimationScript.cs(20,78): error CS1061: Type rigidbody2D' does not contain a definition for
velocity' and no extension method velocity' of type
rigidbody2D' could be found. Are you missing an assembly reference?
The "y" in velocity = new Vector2(move * maxSpeed, GetComponent().velocity.y); seems to be the only thing highlighted in red, by the way.
hope that's enough...
Your answer
![](https://koobas.hobune.stream/wayback/20220612144549im_/https://answers.unity.com/themes/thub/images/avi.jpg)