Search Unity

How can I rotate one vector 3 relative to another one?

Discussion in 'Scripting' started by From-Soy-Sauce, Aug 12, 2016.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hello, I am working on a 3D platformer that I want to be able to feature puzzles that involve changing the direction of gravity. I am getting to the point where I need to be able to determine the slope of the ground so that I can have proper slope movement behavior.

    Normally I would figure this out using something like, shooting a ray in the direction of Vector3.down and using rayDN.Hit.normal to see how slopped the ground is.

    However in this scenerio, the player's perspective of "down" might not be the same as vector3.down. How can I reorient the value given by "rayDN.Hit.normal" so that it is rotated based on how the character's perspective of "down"?


    An additional note, I have tried to do:

    floor_slope=transform.rotation*rayDN.Hit.normal;

    The goal is that (0,1,0) should always be returned if the player is standing on ground that is perpendicular to her her transform.up.

    floor_slope=transform.rotation*rayDN.Hit.normal;

    But that does not gives a value that is different for different gravity directions.
     
    Last edited: Aug 12, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    If you want to invert the direction of a vector you can multiply it by -1.

    Example:
    Code (csharp):
    1. Vector3 gravityDirection = hit.normal * -1.0f;
     
  3. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Thank you for replying, but that really has nothing to do with what I am asking about.

    As you can see from the screen shots, the gravity changing itself works fine. She will fall at the wall where the gravity is set to go.

    But I need to be able to figure out what the slope of the ground is that she is standing on relative to the direction that the gravity is pulling her.

    In this way, if she is standing on flat ground hit.normal.y translated should be 1, and if the ground is, say at a 45 degree angle, hit.normal.y translated should be .5. And this should be true regardless of the direction she is being gravatated toward.

    Just so that you can understand why I need this. Let's just say that if the ground does happen to be at an angle 45 degrees or more, that she shouldn't stand on it, but then go into a slope clinging mode.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this is what transform.down is for.


    https://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html ?
     
    jimroberts likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    I see thank you. This is what I was looking for.