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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Is rigidbody going forward?

Discussion in 'Scripting' started by Diskomuz, Feb 26, 2021.

  1. Diskomuz

    Diskomuz

    Joined:
    Jun 29, 2017
    Posts:
    7
    Hello,
    how i check, if rigidbody goes forward? (IF) in script C#...Rigidbody moves in 3D with all directions. Rigidbody isnt connected to camera.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    You can take the dot product (with
    Vector3.Dot()
    ) of whatever you consider "forward" (perhaps
    transform.forward
    ?) and the
    .velocity
    vector out of the Rigidbody.

    If it is positive, you are going forward, otherwise backwards.

    This is the nitty gritty on dot products:

    https://en.wikipedia.org/wiki/Dot_product
     
    SpiffingGoldfish and Diskomuz like this.
  3. Diskomuz

    Diskomuz

    Joined:
    Jun 29, 2017
    Posts:
    7
    Thanks for the reply, it works perfectly!!!
     
    Joe-Censored likes this.