- Home /
Make an Object Responding to Physics
Hi all, I have been using the code below to make my character jump in a curve from one point to another. Found it somewhere in this site.
function Update()
{
BezierTime = BezierTime + Time.deltaTime;
if (BezierTime >= 1)
{
BezierTime = 0;
}
CurveX = (((1-BezierTime)*(1-BezierTime)) * StartPointX) + (2 * BezierTime * (1 - BezierTime) * ControlPointX) + ((BezierTime * BezierTime) * EndPointX);
CurveY = (((1-BezierTime)*(1-BezierTime)) * StartPointY) + (2 * BezierTime * (1 - BezierTime) * ControlPointY) + ((BezierTime * BezierTime) * EndPointY);
transform.position = Vector3(CurveX, CurveY, 0);
}
The code works perfectly, but what I want to achieve is make the object tp bounce (responds to physics) when it hits something.
Any suggestion please?
Thanks very much. [sorry for my bad english.]
Irina Popova
Perhaps a better way to implement this would be to actually use physics, then? Give the object a bouncy physics material, and it should bounce off obstacles.
Calculating the required force to make it jump in a nice curve might require some knowledge in physics, however.
I have tried using physics before but I could achieve that desired curve. In my game, I have a character that jumps to wherever user touch.. The further it is, the more powerful force added to the rigidbody using the Vector2 $$anonymous$$agnitude calculations. It works well with short ranged jumps, but it goes crazy with far jumps..
@asafsitner: Tell me about it :(
@HeyEveryCoder: my game its almost similar with yours, so have you figured out any solutions?
I have also tried applying rigidboy velocity in the update function. But it doesnt work.
Anyone have had encounter this problem before?
Thanks
Irina Popova
Answer by Bunny83 · Jun 04, 2012 at 10:57 AM
Well you could try a mixed solution. Attach a Rigidbody to your object, but make it kinematic, so the physics are disabled. Move it along the bezier curve, but you need a way to interrrupt the movement (e.g. a boolean variable). Just check for collisions with OnCollisionEnter. If you collide with something, stop your bezier movement, turn physics back on (isKinematic = false) and apply a starting force maybe just in the old movement direction.
From now on your player will react to physics. You have to find out when your player is back on ground to switch back to your old movement, but that's roughly the procedure.
Your suggestion sounds solid, but when i tried, it seems that I have trouble applying the needed force. $$anonymous$$aybe its just my bad math.
I gave up... I have posted a job on elance.com. Hope someone could do this job for us..