Question by
Dthobbs · Apr 07, 2016 at 01:15 AM ·
animationjavascriptscripting problemanimator
BCE0020 error with animation
I am trying to get a 2D sprite to look like it is walking through the animator controller using a speed parameter. I feel like I'm getting really close except this error "BCE0020: An instance of type 'UnityEngine.Rigidbody2D' is required to access non static member 'velocity'." popped up. Could anyone take a look at my script and help me out? I'm rather new to unity.
#pragma strict
public var maxJumps = 0; // maximum numer of jumps
private var numJumps = 0; //number of current jumps
public var moveSpeed = 0;
private var facingRight = true; //initally sprite faces to the right
private var isWalking : boolean;
public var jumpheight = 0;
var animator : Animator;
function Start() {
animator = GetComponent("Animator");
}
function Update () {
var x;
var y;
if (Input.GetKeyDown("space") && CanJump()){
x = GetComponent(Rigidbody2D).velocity.x;
GetComponent(Rigidbody2D).velocity = new Vector2(0, jumpheight);
numJumps ++;
}
if (Input.GetKey (KeyCode.A))
{
y = GetComponent(Rigidbody2D).velocity.y;
GetComponent(Rigidbody2D).velocity = new Vector2(-moveSpeed, y);
animator.SetFloat("Speed", GetComponent(Rigidbody2D.velocity.x));
if (facingRight) {
Flip();
}
}
if (Input.GetKey (KeyCode.D)) {
y = GetComponent(Rigidbody2D).velocity.y;
GetComponent(Rigidbody2D).velocity = new Vector2(moveSpeed, y);
animator.SetFloat("Speed", GetComponent(Rigidbody2D.velocity.x));
if (!facingRight) {
Flip();
}
}
}
function OnCollisionEnter2D (coll : Collision2D)
{
if (coll.gameObject.CompareTag("Ground")) {
numJumps = 0;
}
}
function CanJump() {
return numJumps < maxJumps;
}
function Flip() {
var flipScale : Vector3;
var rigidbody : Rigidbody2D;
rigidbody = GetComponent(Rigidbody2D);
flipScale = rigidbody.transform.localScale;
flipScale.x *= -1; //flip horizontally
rigidbody.transform.localScale = flipScale;
facingRight = !facingRight; // now facing opposite direction
}
it says that animator.SetFloat("Speed", GetComponent(Rigidbody2D.velocity.x)); is tripping the error. please help!
Comment
Answer by JigneshKoradiya · Apr 07, 2016 at 05:36 AM
right now in your script you are using
" GetComponent(Rigidbody2D)."
to get rigidbody from the game object
but after unity 5 this scripting is changed so you have to use
"gameobject.GetComponent.()."