Hey everyone,
My problem: I have a script (Not my own) which tries to access a 3D Rigidbody but my problem is that my game is 2D so i am unable to switch to a 3D rigidbody without causing problems. So i was wondering if a kind soul could help me fix this so it stops trying to access a 3D rigidbody.
P.S i tried changing each “rigidbody…” to “rigidbody2D…” but it was unsuccessful.
#pragma strict
var cSpeed:float = 10.0;
var sFactor:float = 10.0;
//Two variables to hold our scores
static var playerScore:int = 0;
static var enemyScore:int = 0;
function Start ()
{
rigidbody.AddForce(5,0,0);
}
function Update ()
{
var cvel = rigidbody.velocity;
var tvel = cvel.normalized * cSpeed;
rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);
//Check the right bounds
if(transform.position.x > 8.7)
{
playerScore++;
transform.position.x = 0;
transform.position.y = 0;
}
//Check the left bounds
if(transform.position.x < -6.79)
{
enemyScore++;
transform.position.x = 0;
transform.position.y = 0;
}
}
I think your idea was basically right. the rigidbody would be a vector2 instead of a vector3, so you would need to change that on line 18. Line 11 should have only the first 2 arguments.
To add to what @fire7side said, you’ve got the right idea. Make everything Vector2’s and ensure you’re using rigidbody2D instead of rigidbody.
Further more, ensure that there is in fact a Rigidbody2D component attached to your ball object, in the inspector. Or even inside the script, say in the Start() function:
if (!rigidbody2D) {
gameObject.AddComponent(Rigidbody2D);
}
//Since it's a member variable, rigidbody2D will now
//automatically be mapped to the newly added component,
//without having to assign it
Hello,
you would need to change that on line 11. Line 11 should have only the first 2 arguments or vector2 and direction,like this.
rigidbody2D.AddForce(Vector2.up);
change line 16.
var cvel = rigidbody2D.velocity;
and also change Line 18, vector2.Lerp instead of a vector3.Lerp and ensure you’re using rigidbody2D instead of rigidbody,attachment of rigidbody component should be rigidbody2D.