Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting Vector3.Dot to work based on player direction and angle?

I’m using Unity3D and I’m trying to determine the direction that the player is moving only when they are on an angled surface. Currently the code below works as it should on angled surfaces but when I stand on a flat surface and press W or A, the message "Moving uphill" activates, and when I press D or S, the message "Moving downhill" activates. When I am on a flat surface that should not happen but I’m not sure where the issue is.

// Update()
if (controller.isGrounded && Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 2f))
{

    Vector3 move = new Vector3(move.x, 0, move.y).normalized;
    float surfaceDot = Vector3.Dot(move, hit.normal);

    if (surfaceDot == 0)
    {
        Debug.Log("On a flat surface");
    }
    else if (surfaceDot < 0)
    {
        Debug.Log("Moving uphill");
        // Reduce player speed
    }
    else
    {
        Debug.Log("Moving downhill");
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use Vector3.Angle to calculate if it’s an uphill or downhill.

// that angle returned by surfaceAngle for perfectly flat surface.
float flatAngle = 90f;

if (controller.isGrounded && Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 2f))
{

    Vector3 move = new Vector3(move.x, 0, move.y).normalized;
    float surfaceAngle = Vector3.Angle(move, hit.normal);

    // only activate if inclination is above 35°
    if (surfaceAngle - flatAngle < 35f)
    {
        Debug.Log("On a flat surface");
    }
    else if (surfaceAngle < 90)
    {
        Debug.Log("Moving uphill");
        // Reduce player speed
    }
    else
    {
        Debug.Log("Moving downhill");
    }
}

If the surface can tilt in all directions, then this might be more difficult.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading