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
1
Question by squall0831 · Aug 01, 2012 at 05:59 PM · shootingraycastingraycollission

Casting ray forward from transform position

Hey everyone, I'm trying to cast a ray from my player object forward to see if a shot hits something. The problem is that I've only found a sample where the ray is casted from the camera's position. Here's the code:

 void Update () {
    if(Input.GetKey(KeyCode.Escape)){
      Screen.showCursor = true; 
    }
  
    if(Input.GetButtonDown("Fire1")){
      Screen.showCursor = false;
      Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
      RaycastHit hit;
  
      if(Physics.Raycast(ray, out hit)){
        Debug.Log(hit.transform.name); 
        Debug.DrawLine(transform.position, hit.transform.position, Color.red);
      }
    }
  }

But since I'm working on a top-down shooter, I would like to cast the ray from the players transform position (transform.x, transform.y, transform.z+1) into a forward direction. transform.z+1 is there because the ray should start at the gun's height. I hope some of you can help me with that.

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
8
Best Answer

Answer by strachpr01 · Aug 01, 2012 at 07:43 PM

ok try this one. first make an empty game object and position it where you want your gun then use this code. just reference the function when you want to perform the raycast. dont forget to set the raycast object in the inspector.

public GameObject raycastObject;

void CheckForHit(){

RaycastHit objectHit;

     Vector3 fwd = raycastObject.transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(raycastObject.transform.position, fwd * 50, Color.green);
     if (Physics.Raycast(raycastObject.transform.position, fwd, out objectHit, 50))
     {
 //do something if hit object ie
 if(objectHit.name=="Enemy"){
 Debug.Log("Close to enemy");
 }
 }
 }

this code extends a raycast for 50m so depending on how far you wish it to go you may need to change these figures both on the debug and the actual raycast

Comment
Add comment · Show 3 · 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 squall0831 · Aug 01, 2012 at 08:00 PM 2
Share

Thanks this one worked!

avatar image strachpr01 · Aug 01, 2012 at 08:04 PM 1
Share

also just make sure the raycast object is not inside the players collider or it will always register a hit of itself

avatar image niittymaa · Nov 04, 2013 at 08:35 PM 1
Share

Thanks mate, found this one to useful! Just edited it some:

 private void SetTargetEnemy() {
   RaycastHit objectHit;        
   // Shoot raycast
   if (Physics.Raycast(transform.position, transform.forward, out objectHit, 50)) {
     //Debug.Log("Raycast hitted to: " + objectHit.collider);
     targetEnemy = objectHit.collider.gameObject;
   }
 }

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

9 People are following this question.

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

Related Questions

raycast is pointing in two directions? 1 Answer

project a ray in a straight line 1 Answer

Fast object on collider not working 1 Answer

Trouble with Raycast shooting 1 Answer

How to detect certain object using raycast 2 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