Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by soccer1mt · Sep 05, 2016 at 11:30 PM · collidersraycasthithitphysics.raycastcomparetag

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);
             }
         }
     }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges