Why is my Vector3 turning into junk data?

I’m trying to check slopes before applying movement.

I’ve got a Vector3 that tells my player where to move.

I’m wanting to shoot out a ray from that point downward, take that hit position, shoot another ray from the player, take that position. Then compare the two positions.

But for when I shoot the rays I’m getting odd values.

    void ApplyMovement()
    {
        var layerMask1 = 1 << 0;
        var myLayerMask = layerMask1;
        RaycastHit hit;
        if (Physics.Raycast(movementDirection + transform.position, Vector3.down, out hit, 3f, myLayerMask))
        {
            movementHitPosition = hit.transform.position;
        }

        if (Physics.Raycast(transform.position, Vector3.down, out hit, 3f, myLayerMask))
        {
            playerHitPosition = hit.transform.position;
        }

        //GET THE SLOPE
        slopeRun = Vector3.Distance(movementDirection + transform.position, transform.position);
        slopeRise = playerHitPosition.y + movementHitPosition.y;
        if (slope <= slopeLimit)
        {
            //movementDirection = new Vector3(hit.point.x, hit.point.y + (capsuleHieght * 0.5f), hit.point.z);
            if (airControlToggle) myRigidbody.transform.Translate(movementDirection * Time.fixedDeltaTime * movementSpeed, Space.Self);
            return;
        } else {
            if (airControlToggle) myRigidbody.transform.Translate(movementDirection * Time.fixedDeltaTime * movementSpeed, Space.Self);
        }
    }

Does anyone see what I’m doing wrong?

Are you certain you are sending the rays in the right direction? For example, should movementDirection + transform.position be something like transform.position + transform.TransformDirection(Vector3.forward * movementDirection)?

Also, try simplifying the scenario to find the problem.

For example, place your player at the origin. Then place a slope to their right and the ray should not hit it, then try the slope to the left and then behind with same expected results. Then when the slope is placed ahead of them , it should detect the hit. If you are starting from (0, 0, 0) it should simplify the maths when you are stepping things through in the debugger.

1 Like

Thanks.

I actually think I need to scrap this entire approach.

I’m realizing that even if it was working exactly like I want, the player could walk sideways up a slope.

I just have no idea what I’m doing here when it comes to checking slopes.

Anyone have a link to a good tutorial?

Can you not just use the built in physics engine?

What exactly are you trying to do - for example, a skiing game, roll-a-ball or something else?

I’m making an fps controller that uses a collider, but the movement is done through transform.

This way I can easily add force to the player, but at the same time have the snappiness of a character controller.

Also, I think I have my solution.

I’m going to use a spherecast in my ground check, but position it so that if the ground is more than 45 degrees it fails the check.

And while the player is not grounded, the physics material is set to zero friction so they don’t cling to walls and will slide down slopes greater than 45 degrees.