Jorge Criado ros
Jorge Criado ros

Reputation: 11

Physics.Raycast Returning false Unity

i been trying to make just a simple Raycast but im getting false all the time, both of my object has MeshColliders and Box Colliders.

I'm trying to make a ray that if the player or the enemy see the other one shoot

players and enemy has these components: Transform ,Box Collider,Mesh Collider,Rigidbody, and Script(ShootOnSightPlayer),Line Renderer,Nav Mesh Agent.

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

public class ShootOnSightPlayer : MonoBehaviour
{
    public float damage =10f;
    public float range = 100f;
    [SerializeField]
    public GameObject target;
    [SerializeField]
    public bool hasShoot = false;
    public RaycastHit hit;

    public Rigidbody bullet;
    private bool checkIfTurn = false;
    public Transform cannon;

    private Rigidbody clone;
    
    // Bit shift the index of the layer (8) to get a bit mask
    int layerMask = 1 << 8;

    void Start(){
        layerMask = ~layerMask;
         Debug.Log( " Raycast has hit from " + gameObject.transform.tag);
    }
    void FixedUpdate()
    {

        if(gameObject.GetComponent<EnemyController>()){
            checkIfTurn = gameObject.GetComponent<EnemyController>().isTurn;
        }else if(gameObject.GetComponent<PlayerController>()){
            checkIfTurn = gameObject.GetComponent<PlayerController>().isTurn;
        }   



        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        
        Debug.Log(Physics.Raycast( cannon.position,  target.transform.position, out hit));
        if (Physics.Raycast( cannon.position,   target.transform.position, out hit) ){

                 Debug.DrawLine( cannon.position, target.transform.position, Color.white, 2.5f);

                
             Debug.Log("Hit tag " +  hit.collider.tag + " Target Tag " + target.tag + " Has Shoot " + hasShoot + " is my turn " + checkIfTurn);

            if( hit.collider.tag == target.tag  && !hasShoot && checkIfTurn ) {

            // Instantiate the projectile at the position and rotation of this transform
            
            clone = Instantiate(bullet, cannon.position, gameObject.transform.rotation);
           
            clone.transform.SetParent(gameObject.transform);
           

            hasShoot = true;
               
            }
        }
        
    }
}


Upvotes: 1

Views: 305

Answers (1)

KiynL
KiynL

Reputation: 4266

The reason for this problem is that the second raycast parameter asks you for the direction axis, not the location of the second target.

Physics.Raycast(fromPosition, direction, out var hit);

There are two solutions to the problem:

Calculate Direction Axis

First, subtract the target from the starting point of the beam to obtain the direction axis, by subtracting the two axes, it gives you its full-length direction, also if you want the size to always be constant at 1. You can normalize it, which is not necessary here.

Physics.Raycast( cannon.position, target.transform.position-cannon.position, out hit)
var direction = (target.transform.position - cannon.position).normalized;

Using Linecast

You can use Physics.Linecast as an alternative, which is actually a target-oriented raycast.

Physics.Linecast(cannon.position, target.transform.position, out hit);

Upvotes: 1

Related Questions