• 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
Question by DimitriUK · Apr 25, 2015 at 07:31 PM · c#physicsrigidbodyprogrammingcollison

How would I get collision at different speeds?

Hello all, I have been working on a huge commercial project and have come to a stop where I am not too familiarized in the mathematical side and conditional statements part of programming in C#.

What do I want?

  1. My helicopter can travel at speeds and with this speeds I would like a different affect on collision at each speed.

  2. My helicopter has been built using Rigidbody, mainly AddForce and AddTorque. I wouldn't even know how to get the speed value of the Rigidbody.

  3. I would like the helicopter to be able to collide with the terrain at different speeds.

I have gone ahead and made some pseudocode to display to show you all what I am trying to achieve. I imagine I have been stuck for over 7 hours now and I guess all the research I have been doing has not lead me to the correct solution.

 using UnityEngine;
 using System.Collections;
 
 public class CollisionTerrain : MonoBehaviour {
 
     public GameObject terrain;
     public ParticleSystem PlayP1;
     public ParticleSystem PlayP2;
     public ParticleSystem PlayP3;
     public GameObject WH;
 
     void OnCollisionEnter(Collision collision) {
         
         if(helicopter.velocity.magnitude > 10);  //If Helicopter is faster than a velocity of 10
         
         PlayP1.particleSystem.enableEmission = true;     //Helicopter will emit a black smoke and make a damage sound 
         PlayP1.AudioSource.Play(); // Ignore if wrong
         
          if(helicopter.velocity.magnitude > 25); //If Helicopter is faster than a velocity of 25
         
         PlayP2.particleSystem.enableEmission = true;    { Helicopter will emit a deeper black smoke and make a more intense damage sound}
         PlayP2.AudioSource.Play(); // Ignore if wrong
         
          if(helicopter.velocity.magnitude > 50); //If Helicopter is faster than a velocity of 25
         
         PlayP3.particleSystem.enableEmission = true;    { Helicopter will emit a deeper black smoke and make a more intense damage sound}
         PlayP3.AudioSource.Play(); // Ignore if wrong
         WH.Instantiate.WreckageObject. //Blown up Helicopter
         
             
         }
     }

alt text

alt text

collisiononspeed.jpg (121.7 kB)
helicopter.jpg (297.5 kB)
Comment

People who like this

0 Show 0
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
Best Answer

Answer by Addyarb · Apr 25, 2015 at 07:48 PM

First, lets get the speed of your object. Put this at the top of your script:

 public float velocity; //The velocity of our object.
 Vector3 previous; //The previous position of our object.

now, lets use the Update function to get the speed of our object. Like so:

     void Update () {
         velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
         previous = transform.position;
     }

next, lets check that velocity if we hit something.

     void OnTriggerEnter(Collider col){
         if (col.tag == "Terrain") {
             if(velocity > 0 && velocity < 10){
                 //SlowCrash();
             }
             else{
                 if(velocity >= 10 && velocity <= 50){
                 //MediumCrash():
                 }
             else{
                 if(velocity > 50){
                     //FastCrash();
                 }
             }
         } 
     }
 }





Comment
DimitriUK

People who like this

1 Show 2 · 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 DimitriUK · Apr 25, 2015 at 10:35 PM 0
Share

Thank you so much Addyarb!

I wasn't looking for OnTriggerEnter though, so I changed it to OnCollisionEnter, just in case anyone else sees this post and wanted it collision with terrain rather than the trigger area. :)

avatar image Addyarb · Apr 25, 2015 at 11:37 PM 1
Share

Of course! Glad you found a solution. Good luck with your game. :)

avatar image

Answer by siaran · Apr 25, 2015 at 07:50 PM

your code is really not that far off. you can call rigidbody.velocity to get a rigidbody's velocity (as a vector3) and you can get the magnitude for a scalar value.

the problem in your code snip is mostly that if your speed is > 50, all lines will be called (if something is larger than 50, it is also larger than 10, 25, and 30).

a simple way of solving this would be something like

 void OnCollisionEnter(Collision c){
  float speed = helicopter.rigidboy.velocity.magnitude;
 
  if(speed > 50) DoHighestEffect();
  else if (speed > 25) DoMediumEffect();
  else if (speed > 10) DoLowEffect();
  else DoMinimumEffect();
 
 }

You may want to have a single CollisionEffect(float speed) method instead that takes the speed as a parameter, depending on how you implement having different effects.

Comment
DimitriUK

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Tightening up turning radius with AddRelativeForce (or other options) 1 Answer

Vibrating GameObjects 0 Answers

How do I add a pulling force? 1 Answer

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer


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