when my player falls in a certain hight then i want to apply some damage but i don't know how to calculate hight & then how to apply damage e.g. if the player falls some height then apply some damage to the player like that
for the damage i using FPS damage script just decrease the GUI width
thnaks
i make two game object one is my character mesh & attach box collider & Health script to this game object & for the movement i make one more gameobject & i attach movement script to this game object (movement script is my own script)
thats what i don't where attach below script & hw to work this script
var speed =50.0;
var raydist = 1.0;
var rayoffset = 1.0;
static var maxspeed = 5.0;
var airxspeedfactor = 2;
var jumpforce = 1000;
static var grounded = 1;
var damage = 10.0;
function FixedUpdate ()
{
transform.position.z = 0;
//print (rigidbody.velocity.x);
if (Physics.Raycast ((transform.position+ Vector3 (rayoffset, rayoffset, rayoffset)), Vector3 (0, -1, 0), raydist))
{
//print ("Grounded");
grounded = 1;
transform.rigidbody.AddForce((Input.GetAxis("Horizontal") * speed), 0, 0);
rigidbody.velocity.x = Mathf.Clamp(rigidbody.velocity.x, -maxspeed, maxspeed);
//rigidbody.drag=1.75;
// if (Input.GetButtonDown("Vertical"))
// {
// transform.rigidbody.AddForce(0, jumpforce, 0);
}
else
{
//print ("Clear");
grounded = 0;
transform.rigidbody.AddForce((Input.GetAxis("Horizontal") * speed / airxspeedfactor), 0, 0);
rigidbody.velocity.x = Mathf.Clamp(rigidbody.velocity.x, -maxspeed, maxspeed);
//rigidbody.drag = 0;
}
}
function OnCollisionEnter(collision : Collision) {
// Only give damage if speed is greater than 10
var relSpeed : float = collision.relativeVelocity.magnitude;
if (relSpeed > 10.0) {
print ("ok1");
//The amount of damage increases linearly with speed from
//v = 10 (giving zero damage) to v = 50 (giving 1 damage)
SendMessage ("ApplyDamage", damage,
SendMessageOptions.DontRequireReceiver );
}
}
EDIT:
As it says 'enhanced' character controller, replace your standard controller with this script. Make a new javascript file. Then, copy the enhanced walker code into it. Save it. Then, select your first person controller. Remove the standard walker script. Next, drag your new script onto it, thereby replacing the old walker script with the new one you just downloaded. That should do the trick. However, you're not finished yet. Find this script:
function FallingDamageAlert (fallDistance : float) {
Debug.Log ("Ouch! Fell " + fallDistance + " units!");
//Put your falling script here, and if you want, get rid of the Debug.Log line.
}
It's near the bottom. That's where you can put what happens if you fall, such as subtracting health, shaking, blurring, or otherwise discoloring the GUI, etc. Good luck!
Oh, and don't forget to fix the settings, like checking the falling damage option and such. You really should just use the standerd FPS prefab for this, as it makes things a lot simpler. You shouldn't need much else, unless you want to replace the 'Graphics' component with your own personal mesh. All it is an object that can be affected by physics, so it's not a big deal if you replace it. For clarity, though, keep the name to 'Graphics' no matter what mesh you have, in case a unknown script references it from somewhere and can't find it. Although that's very unlikely, it's also nice to keep your game streamlined and as simple as possible.
I believe this is what you're looking for. Check it out!
The easiest thing would probably be if you instead of using the height the player has fallen, used the velocity of the player when he collides with the ground. You can get the relative velocity of a collision in the `OnCollisionEnter` method
function OnCollisionEnter(collision : Collision) {
// Only give damage if speed is greater than 10
var relSpeed : float = collision.relativeVelocity.magnitude;
if (relSpeed > 10.0) {
//The amount of damage increases linearly with speed from
//v = 10 (giving zero damage) to v = 50 (giving 1 damage)
var damage : float = Mathf.Clamp((relSpeed - 10.0)/50.0, 0.0, 1.0);
//give the damage somehow
}
}