Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 JayFitz91 · Jul 20, 2015 at 05:40 PM · raycasttransformposition

Trouble with raycasting

I would like to fire a raycast from the tip of my gun forward 10 units, but for some reason the end of my raycast is fixed on a particular location rather than pointing forward and I'm not sure why, the image below should show my issue, the if statement always displays "hitting", here is the code I am using:

 void Update
 {        
     Vector3 fwd = gunPos.transform.TransformDirection(Vector3.forward);
     Debug.DrawLine(gunPos.transform.position, fwd, Color.green);
 
     if (Physics.Raycast(gunPos.transform.position, fwd, 10))
     {
         Debug.Log("hitting");
     }
     else
         Debug.Log("Not Hitting");
         
 }

EDIT

This is is what I'm using now, the end of the ray is no longer fixed but the ray does not rotate with the player and continuously fires forward, I've added another photo to better illustrate, the ray works now but when I move my character the Z position of my gunPos stays in the same spot when i need it to rotate with the player.

 void Update()
 {
     RaycastHit hit;
     Ray myRay = new Ray(gunPos.transform.position, Vector3.forward);
     Debug.DrawRay(gunPos.transform.position, Vector3.forward * 10);
 
     if (Physics.Raycast(myRay, out hit, 10))
     {
         Debug.Log("hitting");
     }
     else
         Debug.Log("NOT hitting");
         
 }

alt text

capture.png (202.5 kB)
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
0

Answer by ivanbozovic · Jul 20, 2015 at 05:44 PM

Hi, this line is the problem:

 Vector3 fwd = gunPos.transform.TransformDirection(Vector3.forward);

Try using gunPos.transform.forward instead of Vector3.forward

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 JayFitz91 · Jul 20, 2015 at 05:46 PM 0
Share

That didn't work I'm afraid :/

avatar image Positive7 · Jul 20, 2015 at 06:21 PM 0
Share

attach an empty child GameObject at the end of the gun, make sure Z Axis is forward

avatar image JayFitz91 · Jul 20, 2015 at 07:04 PM 0
Share

Thats how I have it set up currently, but the z axis seems to be in world space when I need it to be in local

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

23 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

Related Questions

JS Dynamic Height Camera vibrating =( 1 Answer

Getting transform position of last parent 2 Answers

NullReferenceException when using raycast 1 Answer

Negative Positions Breaks Raycasts 1 Answer

Help with moving instantiated object's transform 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