Raycasthit is sometimes returning gameobject name when I use hit.gameobject.comparetag.
I have weird raycasthit behavior. Sometimes, it compares the tag correctly and the function executes as expected. Other times, Debug.Log(hit.collider.tag) returns the name of the game object instead of the tag and then the code does not execute since the comparison in the if statement is not satisfied. It appears that it might have to do with the proximity of the object to the raycast but I can't figure it out. Has anyone seen this behavior before? Any suggestions would be very helpful. Been bashing my head against a wall for hours. Tyty
The code I'm talking about is in the CastAtPlayer function at the bottom. (Also, as a side note, my explosion force doesn't actually do anything to the instantiated object so if anyone has any ideas about that, that would also be much appreciated!)
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class TimeDestroy : MonoBehaviour
{
// This script is attached to the dropped bombs gameobjects and controls the explosions for the dropped bombs.
public GameObject explosion; // blow up thangs (particle system)
public GameObject CubeExplosion;// replacing cubes with crumbling cubes
public GameObject whitesmoke; // this is the explosion "Cloud" game object
public GameObject Flag;
public GameObject explodedplayer; //prefab of player that is instantiated to be exploded out of the arena
private Movement movement;
private FlagMove flagMove;
private Rigidbody PlayerRB;
private UnityEngine.NavMeshAgent Navtracker;
private Bomb_Pickup BombCountScript;
private PowerPick PickUpTracker;
private GameObject PlayerInstance; // instance of player
private float timeLeft = 4.5f;
// Use this for initialization
void Start()
{
PlayerInstance = PlayerInstanceGetter();
flagMove = Flag.GetComponent<FlagMove>();
movement = PlayerInstance.GetComponent<Movement>();
PlayerRB = PlayerInstance.GetComponent<Rigidbody>();
Navtracker = PlayerInstance.GetComponent<UnityEngine.NavMeshAgent>();
BombCountScript = PlayerInstance.GetComponent<Bomb_Pickup>();
PickUpTracker = PlayerInstance.GetComponent<PowerPick>();
}
// This is a 4 second time to have bombs blow up.
void FixedUpdate ()
{
//sets time on instantiated object(dropped bomb) which explodes after 4 seconds
timeLeft -= Time.deltaTime;
if ( timeLeft <= 0)
{
CastAtPlayer();
Casting(Vector3.forward);
Casting(Vector3.back);
Casting(Vector3.left);
Casting(Vector3.right);
Instantiate(explosion, transform.position, transform.rotation); // graphic for explosion
BombIncrementor();
Destroy(this.gameObject, 0f); //removes instantiated bomb
}
}
void OnTriggerStay(Collider other) // If another instantiated cloud hits a bomb, it will cause those bombs to blow up.
{
if(other.gameObject.CompareTag("Cloud"))
{
timeLeft = timeLeft - 4.5f;
}
}
void CastAtPlayer() // Casts ray from bomb to player of length of explosion. If hits, then the player dies
{
RaycastHit hit;
Vector3 direction = -transform.position + PlayerInstance.transform.position;
direction.y = .55f;
Ray landingRay = new Ray(transform.position, direction);
if (Physics.Raycast(landingRay, out hit, (.2f + (PickUpTracker.PowerUpLevel * .2f))))
{
Debug.Log(hit.collider.tag);
if (hit.collider.CompareTag("player1") || hit.collider.CompareTag("player2"))
{
Debug.Log(hit.collider.tag + " is dead!");
GameObject deadplayer = hit.collider.gameObject;
Debug.Log(deadplayer);
GameObject explodingplayer = Instantiate(explodedplayer, deadplayer.transform.position, Quaternion.identity) as GameObject;
deadplayer.SetActive(false);
Rigidbody rb = explodingplayer.GetComponent<Rigidbody>();
rb.AddExplosionForce(100f, transform.position, 0f, 100f);
Destroy(this.gameObject);
Debug.Log(transform.position);
}
}
}
Your answer
Follow this Question
Related Questions
Raycast is hitting a collider but ignores it right after 0 Answers
Find a collision point from inside the object. 0 Answers
cant iterate through after boxcast or spherecast problem need help please 1 Answer
Trigger and Rotation Issue 0 Answers
Touching anywhere on screen the Bow is releasing the arrow 1 Answer