- Home /
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?
- My helicopter can travel at speeds and with this speeds I would like a different affect on collision at each speed. 
- 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. 
- 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
         
             
         }
     }


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();
                 }
             }
         } 
     }
 }
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. :)
Of course! Glad you found a solution. Good luck with your game. :)
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.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                