Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Samir 1 · Jan 19, 2011 at 08:30 PM · gameobjectraycastshoothit

raycast from an empty gameobject?

basically i have a gun, infront of the gun there is an empty game object, i have attatched this script to it. basically i want the raycast line to shoot in the z axis(Vector3.forward) from this empty gameobject and when it hits another object with tag as "enemy" then destory it(for now). i know roughly how this is done but for some reason it isnt working. there is no errors just raycast doesnt work. and yes i have called this function in the update so that isnt the problem. and i have a variable for "public RaycastHit hit;"

here is my code so far:

public void Shoot()
{
    if (Input.GetKey("f"))
    {
        if (Physics.Raycast(transform.position, Vector3.forward, out hit))
        {
            if (hit.collider.gameObject.tag == "Enemy")
            {
                Debug.Log("It Hurts");
                Debug.DrawLine(transform.position, hit.point);
               /* GameObject enemy = GameObject.FindWithTag("Enemy");
                EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
                enemyHealth.maxHealth = -10;*/
            }
        }
    }
    //else
}

}

i have extra code in there but i just commented it out for now i really just want the raycast to shoot forward from the position of which the gameobject that it is attatched to. thanks plz reply is u know how this is done im sure im not missing out that much. :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Steffen Franz · Jan 19, 2011 at 09:58 PM

does your object, the Enemy in this example, have a collider attached?

Physics.Raycast casts against Colliders (from the Script Reference "Returns bool - True when the ray intersects any collider, otherwise false."). If the objects you are trying to "hit" does not have a Collider (e.g. BoxCollider) attached it won't detect it.

also: for the commented out code, don't do .FindWithTag("Enemy"). You already have the object you want to manipulate from hit.collider.gameObject. This might cause your problem for your code not working. If you have more than one "Enemy" tagged GameObject in the scene, Unity will grab one of the many "Enemy" tagged objects. This could be the one not being hit by the Raycast.

Comment
Add comment · Show 2 · Share
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
avatar image Samir 1 · Jan 19, 2011 at 10:49 PM 0
Share

thanks man, i also, finally found something else, that made it work. this topic can be set to answered :)

i had to change:

         if (Physics.Raycast(transform.position, Vector3.forward, out hit))
         {}

to ...transform.position, transform.TransfomrDirection(Vector3.forward)..."

this made it work :D

avatar image Coreyf716 · Nov 25, 2012 at 07:26 PM 0
Share

@Steffen 1 $$anonymous$$y player is an empty gameObject. It has a character controller, therefore, it can't have a collider. How can The player detect hits by raycast?

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

1 Person is following this question.

avatar image

Related Questions

How to detect if a raycast ray stop hitting an object 1 Answer

shooting and hit script JS 1 Answer

Unity Raycast Script, Help! 1 Answer

raycast to determine pivot 1 Answer

How do I make it know what it's hitting? 3 Answers


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