unity3d - Error on converting UnityScript to C# -
i trying convert unityscript code d# , getting following error:
expression denotes method group,
variable
,value
ortype
expected on getcomponent
void update () { float xvel = getcomponent().rigidbody2d().velocity.x; if( xvel < 18 && xvel > -18 && xvel !=0){ if(xvel > 0){ getcomponent.rigidbody2d().velocity.x=20; }else{ getcomponent.rigidbody2d().velocity.x = -20; } } }
your issue with: getcomponent().rigidbody2d()
not how use getcomponent, error seeing because getcomponent
requires parameter or specified type. js , c# getcomponent work little different. mean do:
void update () { float xvel = getcomponent<rigidbody2d>().velocity.x; if( xvel < 18 && xvel > -18 && xvel !=0){ if(xvel > 0){ getcomponent<rigidbody2d>().velocity.x = 20; }else{ getcomponent<rigidbody2d>().velocity.x = -20; } } }
also in c#, don't think can modify velocity directly, due property wrappers around it. instead have manually update velocity new vector2
. if want set x
value, pass in existing y
value.
i write this:
private rigidbody2d _rigidbody; void start() { _rigidbody = getcomponent<rigidbody2d>(); } void update () { float xvel = _rigidbody.velocity.x; if( xvel < 18 && xvel > -18 && xvel !=0){ if(xvel > 0){ _rigidbody.velocity = new vector2(20, _rigidbody.velocity.y); }else{ _rigidbody.velocity = new vector2(-20, _rigidbody.velocity.y); } } }
though i'd change magic 18
in variable can't make guess here represents!
Comments
Post a Comment