Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Vector3 orthogonal to plane

Discussion in 'Scripting' started by midu77, Apr 7, 2021.

  1. midu77

    midu77

    Joined:
    Sep 25, 2018
    Posts:
    4
    Mathemagicians to the rescue!

    A sphere collides with a plane. Now I need to correct collision.relativeVelocity.magnitude to the proportion that is orthogonal to the plane.

    I know Vector3 has some static methods that can calculate such things, but I have no idea what to use and how exactly.
     
  2. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    planeNormal * Vector3.Dot(planeNormal, velocity)
    will give you the velocity towards that plane
     
  3. midu77

    midu77

    Joined:
    Sep 25, 2018
    Posts:
    4
    Thank you for the ultrafast reply! How would you change the following code? I do need a float in the end. Sorry, I'm oviously a noob.
    Your help is appreciated a lot!

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class AkRtpcAndAudio : MonoBehaviour
    5. {
    6.     private float Force;
    7.  
    8.     private void OnCollisionEnter(Collision collision)
    9.     {
    10.         Force = collision.relativeVelocity.magnitude;
    11.         Debug.Log(Force);
    12.     }
    13. }
    14.  
     
  4. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    mhh i think OnCollisionEnter happens after collision so you dont even have the right values anymore. could be wrong, its just out to the top of my head and i cant take a look at it right now so maybe someone else can help continue here?