Untitled

 avatar
unknown
plain_text
a month ago
2.7 kB
5
Indexable
private void Update()
{
    ...

    MyInput();
    SpeedControl();
    PersonalSpace();

    ...
}

private void FixedUpdate()
{
    MovePlayer();
}

private void MyInput()
{
    horizontalInput = Input.GetAxisRaw("Horizontal");
    verticalInput = Input.GetAxisRaw("Vertical");

    ...
}

private void MovePlayer()
{
    if (PersonalSpace())
    {
        // calculate move vector if theres a wall in the way
        Debug.Log("Attempting to stop player from moving toward wall");
        moveDirection = new Vector3(GetSlopeMoveDirection(rb.velocity, closeWall.normal).x * verticalInput, rb.velocity.y, GetSlopeMoveDirection(rb.velocity, closeWall.normal).z * horizontalInput);

    }
    else
    {
        // calculate movement direction
        moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    }

    // on slope
    if (OnSlope() && !exitingSlope)
    {
        rb.AddForce(GetSlopeMoveDirection(moveDirection, slopeHit.normal) * moveSpeed * 20f, ForceMode.Force);

        if (rb.velocity.y > 0)
        {
            rb.AddForce(-slopeHit.normal * 80f, ForceMode.Force);
        }
    }

    // on ground
    else if (grounded)
    {
        rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    }

    // in air
    else if (!grounded)
    {
        rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    }

    // turn gravity off while on slope
    rb.useGravity = !OnSlope();
    
    
}

private void SpeedControl()
{
    // limiting speed on slope
    if (OnSlope() && !exitingSlope)
    {
        if (rb.velocity.magnitude > moveSpeed)
        {
            rb.velocity = rb.velocity.normalized * moveSpeed;
        }
    }

    // limiting speed on ground or in air
    else
    {
        Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        // limit velocity if needed
        if (flatVel.magnitude > moveSpeed)
        {
            Vector3 limitedVel = flatVel.normalized * moveSpeed;
            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
        }
    }
}

public Vector3 GetSlopeMoveDirection(Vector3 direction, Vector3 plane)
{
    return Vector3.ProjectOnPlane(direction, plane).normalized;
}


(separate script)
public bool checkIfWall(RaycastHit wallHit)
{
    float angle = Vector3.Angle(wallHit.normal, Vector3.up);
    if(angle >= minWallAngle && angle <= maxWallAngle)
    {
        Debug.Log("Wall is climbable");
        return true;
    }
    return false;
}
Editor is loading...
Leave a Comment