Physics Scripting issues and help Thread

Hey guys, looking through the scripting threads i’ve noticed a lot of people having problems on how to script different physics applications. I myself have these problems lol So I decided to make a thread in hopes of it staying sticky for people to post their problems or issues.

I’ll start…

I have a box collider that will collide with a gameobject. I want to add a force to the gameobject in the direction of the box collider’s normal. What’s the most efficient and simplest way to do this??

will whis work?? if so, how do I use it??

bump

The normal is already a vector pointing away of the surface,to move away via the normal vector you just have to move the object with this vector.

An example would be:
rigidbody.Addforce(hit.normal * speed);

I dont know, this is my code but it doesn’t seem to be working, I don’t think it’s returning the “hit.normal”

var power : float = 10.0;
var hasHit : boolean = false;


function OnCollisionEnter (hit : Collision) {

	if(hit.gameObject.name == "racketcollider"){
		hasHit = true;
		
		}
}

function OnControllerColliderHit(hit : ControllerColliderHit) {
    rigidbody.AddForce(hit.normal * power);
}

You shouldn’t be putting code in On…whatever functions. You should use those to gather useful data, then perform the actual physics code in fixedupdate. The reason for this is that on…whatever can be called multiple times in one loop, for example it can be called 10 times if touching multiple objects.

For example:

function OnCollisionStay(collisionInfo : Collision)
{
	//loop through all contacts
	for (var contact : ContactPoint in collisionInfo.contacts)
	{
		//get gameobject
		var obj:GameObject = contact.otherCollider.gameObject;

		//walkable
		if (obj.layer == 10)
		{
			averageNormal += contact.normal;
			averagePoint += contact.point;
			collisionCount ++;
			grounded = true;
		}
    }
	
}

And then deal with it more in fixedupdate:

//deal with ground
if (grounded)
{
//calculate collisions
averageNormal /= collisionCount;
averagePoint /= collisionCount;
collisionCount = 0;

//calculate if we can jump
if (jump)
{
isJumping = true;
}
}

… then at the end of fixedupdate, reset grounded…

grounded = false;

Just a typical example of a custom running/jumping character. The information is gathered from the callbacks and then dealt with in fixedupdate for physics. It isn’t dealt with in Update() because update is grossly out of sync with the on…collision callback functions, and fixedupdate is in sync with them.

I understand your reasoning and thanks for pointing that out(i’ll be sure to remember that), but I’m not advanced enough in coding yet to know how to apply that to my application. Can someone delve a little further into this for me?? Maybe someone who’s encountered my similar situation?

bump