dimanche 28 juin 2020

Multiple bools in if statement being ignored

I have been trying to figure this out for over a week. I have the player set up with no rigid body because that would not work with the mechanics of the game. The player (a rectangular prism) has two empty child objects one on each end to detect collisions using raycasts. But every time i use the raycast in the player movement script the player just ignore the collisions and rolls right through the wall. The raycasts are working fine. When i use on bool in the if statement the player stops when he hits the wall. But when i use two bools in the if statement the player just rolls right through it...

Here is the raycast script i have on an empty game object on the left side of the player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionDetectionLeft : MonoBehaviour
{
    public static bool hit_L_Right;
    public static bool hit_L_Left;
    public static bool hit_L_Forward;
    public static bool hit_L_Backward;


    private void FixedUpdate()
    {
        hit_L_Right = Physics.Raycast(transform.position, Vector3.right, 0.67f);
        Debug.DrawRay(transform.position, Vector3.right * 0.67f, Color.green);
        if (hit_L_Right)
            Debug.Log("Hit Right");

        hit_L_Left = Physics.Raycast(transform.position, Vector3.left, 0.67f);
        Debug.DrawRay(transform.position, Vector3.left * 0.67f, Color.green);
        if (hit_L_Left)
            Debug.Log("Hit Left");

        hit_L_Forward = Physics.Raycast(transform.position, Vector3.forward, 0.67f);
        Debug.DrawRay(transform.position, Vector3.forward * 0.67f, Color.green);
        if (hit_L_Forward)
            Debug.Log("Hit Forward");

        hit_L_Backward = Physics.Raycast(transform.position, Vector3.back, 0.67f);
        Debug.DrawRay(transform.position, Vector3.back * 0.67f, Color.green);
        if (hit_L_Backward)
            Debug.Log("Hit Back");
    }
}

Here is the collision script on the right child:

using System.Collections.Generic;
using UnityEngine;

public class CollisionDetectionRight : MonoBehaviour
{
    public static bool hitRight;
    public static bool hitLeft;
    public static bool hitForward;
    public static bool hitBackward;


    private void FixedUpdate()
    {
        hitRight = Physics.Raycast(transform.position, Vector3.right, 0.67f);
        Debug.DrawRay(transform.position, Vector3.right * 0.67f, Color.green);
        if (hitRight)
            Debug.Log("Hit Right");

        hitLeft = Physics.Raycast(transform.position, Vector3.left, 0.67f);
        Debug.DrawRay(transform.position, Vector3.left * 0.67f, Color.green);
        if (hitLeft)
            Debug.Log("Hit Left");

        hitForward = Physics.Raycast(transform.position, Vector3.forward, 0.67f);
        Debug.DrawRay(transform.position, Vector3.forward * 0.67f, Color.green);
        if (hitForward)
            Debug.Log("Hit Forward");

        hitBackward = Physics.Raycast(transform.position, Vector3.back, 0.67f);
        Debug.DrawRay(transform.position, Vector3.back * 0.67f, Color.green);
        if (hitBackward)
            Debug.Log("Hit Back");
    }
}

And here is the player movement script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using UnityEngine;

public class Rectangle : MonoBehaviour
{
    public float rotationPeriod = 0.3f;
    Vector3 scale;

    bool isRolling = false;
    float directionX = 0;
    float directionZ = 0;

    float startAngleRad = 0;
    Vector3 startPos;
    float rotationTime = 0;
    float radius = 1;
    Quaternion fromRotation;
    Quaternion toRotation;

    private void Start()
    {
        scale = transform.lossyScale;
    }

    private void Update()
    {
        float x = 0;
        float y = 0;
        

        if (Input.GetKeyDown(KeyCode.D) && (!BoxCollision.right || !BoxCollision.left))         // not working  
        {
            y = Input.GetAxisRaw("Horizontal");
        }

        else if (Input.GetKeyDown(KeyCode.A) && (!CollisionDetectionRight.hitLeft || !CollisionDetectionLeft.hit_L_Left))       // not working  
        {
            y = Input.GetAxisRaw("Horizontal"); 
        }

        if (x == 0)
        {
            
            if (Input.GetKeyDown(KeyCode.W) && !CollisionDetectionRight.hitForward)
            {
                x = Input.GetAxisRaw("Vertical");
            }

            else if (Input.GetKeyDown(KeyCode.S) && !CollisionDetectionRight.hitBackward)
            {
                x = Input.GetAxisRaw("Vertical");
            }
        }

        if ((x != 0 || y != 0) && !isRolling)
        {
            directionX = -y;
            directionZ = x;
            startPos = transform.position;
            fromRotation = transform.rotation;
            transform.Rotate(directionZ * 90, 0, directionX * 90, Space.World);
            toRotation = transform.rotation;
            transform.rotation = fromRotation;
            setRadius();
            rotationTime = 0;
            isRolling = true;
        }
    }

    private void FixedUpdate()
    {
        if (isRolling)
        {
            rotationTime += Time.fixedDeltaTime;
            float ratio = Mathf.Lerp(0, 1, rotationTime / rotationPeriod);

            float thetaRad = Mathf.Lerp(0, Mathf.PI / 2f, ratio);
            float distanceX = -directionX * radius * (Mathf.Cos(startAngleRad) - Mathf.Cos(startAngleRad + thetaRad));
            float distanceY = radius * (Mathf.Sin(startAngleRad + thetaRad) - Mathf.Sin(startAngleRad));
            float distanceZ = directionZ * radius * (Mathf.Cos(startAngleRad) - Mathf.Cos(startAngleRad + thetaRad));
            transform.position = new Vector3(startPos.x + distanceX, startPos.y + distanceY, startPos.z + distanceZ);

            transform.rotation = Quaternion.Lerp(fromRotation, toRotation, ratio); 

            if(ratio == 1)
            {
                isRolling = false;
                directionX = 0;
                directionZ = 0;
                rotationTime = 0; 
            }
        }
    }

    void setRadius()
    {
        Vector3 dirVec = new Vector3(0, 0, 0);
        Vector3 nomVec = Vector3.up;

        if (directionX != 0)
        {
            dirVec = Vector3.right;
        }

        else if (directionZ != 0)
        {
            dirVec = Vector3.forward;
        }

        if (Mathf.Abs(Vector3.Dot(transform.right, dirVec)) > 0.99)
        {
            if (Mathf.Abs(Vector3.Dot(transform.up, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.x / 2f, 2f) + Mathf.Pow(scale.y / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.y, scale.x);
            }

            else if (Mathf.Abs(Vector3.Dot(transform.forward, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.x / 2f, 2f) + Mathf.Pow(scale.z / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.z, scale.x);
            }
        }

        else if (Mathf.Abs(Vector3.Dot(transform.up, dirVec)) > 0.99)
        {
            if (Mathf.Abs(Vector3.Dot(transform.right, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.y / 2f, 2f) + Mathf.Pow(scale.x / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.x, scale.y);
            }

            else if (Mathf.Abs(Vector3.Dot(transform.forward, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.y / 2f, 2f) + Mathf.Pow(scale.z / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.z, scale.y);
            }
        }

        else if (Mathf.Abs(Vector3.Dot(transform.forward, dirVec)) > 0.99)
        {
            if (Mathf.Abs(Vector3.Dot(transform.right, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.z / 2f, 2f) + Mathf.Pow(scale.x / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.x, scale.z);
            }

            if (Mathf.Abs(Vector3.Dot(transform.up, nomVec)) > 0.99)
            {
                radius = Mathf.Sqrt(Mathf.Pow(scale.z / 2f, 2f) + Mathf.Pow(scale.y / 2f, 2f));
                startAngleRad = Mathf.Atan2(scale.y, scale.z);
            }
        }
    }
}

If anyone can figure this out please let me know. This is doing my head in.

Aucun commentaire:

Enregistrer un commentaire