I am porting a game design from Torque to Unity. I am amazed on how fast I can do it except for scripting the interaction I need for damage. I would like to have a Car receive damage by the speed it hits any collider in the provided it exceeds a specific speed value. Example: Car moving at 10 mph will get damage of .1... have the value be ignored unless it exceeds a value of 1. I modified the FPSPlayer.js so that it will work with the Car.js in the RaceDemo. The issue I have now is how to get the damage to register by impact. Any help would be great. I am still evaling the game development environment... but I plan to buy it once the time is up. Any help would be very helpful to my game.
You want to override function OnCollisionEnter. http://unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html The collision structure has a property called relativeVelocity exactly for this purpose. You can read that, get the magnitude from it and calculate the damage from that.
Attached is my updated code... but the issue I have right now is any collision triggers the damage if it is a Blockcollider. I will look into the relativeVelocity and see if I can get it to work. Thanks,
Code (csharp): function OnCollisionEnter() { var hit : BoxCollider; var direction = transform.TransformDirection(Vector3.forward); you are declare a hit variable to an undefined value, then you are trying to use it. Can't do that. Take a look at the first sample on the link i posted above. It shows you how to use the OnCollisionEnter function using relativeVelocity to determine if a sound should played.