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 gamerant · Sep 05, 2013 at 03:24 PM · javascriptraycast

How to fix a small raycast issue?

Hi there. I just started working with raycast and now encountered an interesting problem.

I have this code snipet so that when a ray hits a collider the object that emitted the ray would move in a certain direction.You can find the snipet below.

This worked fine in my test scene.However when I put this in the actual script that is attached to my game object the ray is drawn above the game object,rather than coming from the game object it self.Which results in the raycast completely missing it's target.

I tried using Vector3() to position the ray in the correct position,but this didn't work as the ray seems to remain in the same position no matter what and not move with the object that is emitting it.

Just wondering is there something I am missing or is there something I could do so that the ray will always be drawn from the game object instead of above it?

All help is appreciated :D

 var forward = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;    
 
 Debug.DrawRay(transform.position,-forward*15, Color.green);
  
 if((Physics.Raycast(transform.position,-forward,hit,15))&&(onPlatform==true))
     {
         Debug.Log("Hit");
         if(hit.collider.gameObject.tag == "MainPlatform")
         {
             MovePlatform();
         }
     } 

If something is unclear ask away :D

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

2 Replies

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

Answer by clunk47 · Sep 05, 2013 at 04:01 PM

Just use -transform.forward and place your raycast statement in Update(). Also, no need for so many parentheses.

 #pragma strict
 
 var hit : RaycastHit;
 var onPlatform : boolean;
 
 function Update()
 {
     if(Physics.Raycast(transform.position, -transform.forward, hit, 15) && onPlatform)
     {
         Debug.Log("Hit");
         if(hit.collider.gameObject.tag == "MainPlatform")
         {
             MovePlatform();
         }
     } 
 }
 
 function MovePlatform()
 {
     //Code
 }
Comment
Add comment · Show 1 · 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 gamerant · Sep 05, 2013 at 06:31 PM 1
Share

That's great thank you. I had var forward = transform.TransformDirection(Vector3.forward); before the rest of the code and since that was removed it seems to work now. And I know there is no need but if I have to place more than once condition inside the if()` then I prefer to use more parentheses to just separate out each once more clearly.

avatar image
0

Answer by DDP · Sep 05, 2013 at 03:43 PM

What about this?

 var forward = transform.TransformDirection(transform.forward);
Comment
Add comment · Show 1 · 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 clunk47 · Sep 05, 2013 at 03:55 PM 1
Share

Or just

 var forward = transform.forward
 

But you don't even need to assign a variable, since you can just use

 transform.forward

If you wanted to assign a variable and use TransformDirection, you'd use like:

 var forward = transform.TransformDirection(Vector3.forward);

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

17 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Gun Script Help 2 Answers

Trouble with raycasting 0 Answers

The name 'Raycast' does not denote a valid type ('not found') 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