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;
}
}
}
}