Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Apexuni · Mar 26, 2010 at 08:41 PM · damagefalling

fall damage issue?

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 ); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by e-bonneville · Mar 26, 2010 at 10:49 PM

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Apexuni · Mar 27, 2010 at 07:17 AM 0
Share

hey i try this script but nothing happen..... i add more information please see my question again Thanks

avatar image Apexuni · Mar 29, 2010 at 05:45 AM 0
Share

as you say in your script that subtracting health that's what i add this send massage here like gameObject.Send$$anonymous$$essage ("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver ); but its not working

avatar image Apexuni · Apr 02, 2010 at 06:05 PM 0
Share

hey i slove this problem. can i call this fuction only one time

means. one fall one damage & then if player fall again then second damage.

avatar image
1

Answer by KvanteTore · Mar 26, 2010 at 10:00 PM

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
 }

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Apexuni · Mar 27, 2010 at 07:17 AM 0
Share

hey i try this script but nothing happen..... i add more information please see my question again Thanks

avatar image KvanteTore · Mar 27, 2010 at 08:02 AM 0
Share

You need to actually do something with the damage, like subtract it from your health.

avatar image KvanteTore · Mar 27, 2010 at 08:03 AM 0
Share

However, I would think that the EnhancedFPSWalker elbon95 linked to is more appropriate for your needs.

avatar image Apexuni · Mar 27, 2010 at 09:11 AM 0
Share

i upload my whole movement script can you tell me whats wrong in this script check my question.

avatar image Apexuni · Mar 28, 2010 at 08:49 PM 0
Share

but the problem is when i changed relspeed for e.g. 20 then this script is not working

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

FallDamage being given continously instead of just once 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges