Ok I know this is probably a simple solution to this. And I tried to solved myself… But I need some help before my head explodes, or I have a stroke. I more of a graphics guy…
I working on very simple collisons.
Basically I’m using the “First Person Controller” for the player
I have added a simple cube(From Unity). I added Rigidbody to the cube.
Basically I am trying to get it when so that when the player collides with the box. A message would display in the debug log.
I have written this script (javascript) and added it to the cube.
function OnCollisionEnter(collision:Collision){
if (collision.gameObject.tag == "Player")
{
print ("yay it works);
}
}
I get no errors, but I also get no display in the console/debug log. So basically nothing is happening. Is their something wrong with the code? Can I get some help please?
“The Controller does not react to forces on its own and it does not automatically push Rigidbodies away.”
Some code i found somewhere some time ago!
//CharacterCollider.js
// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
//print("START");
var a_layer = hit.collider.gameObject.layer;
if (a_layer != 8 )
return;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
var p = (pushPower-body.mass);
print(p);
if (p < 0 ) pr=0;
// Apply the push
body.velocity = pushDir * p;
}