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 carlqwe · May 31, 2015 at 07:57 PM · raycast

Raycast is not working

Hello i have a script that controlls my enemy, i have so it follows me (futher down the script). I also have an raycast that checks if the Player is close to it, but i cant position the raycast right and it always changes please help! (Script is atatched to the enemy and the enemy rotates towards the player/target)

Code:

 function Update ()
 {
     //Makes the enemy move
     if(Input.GetKeyDown(KeyCode.Y))
     {
         move = true;
     }
     
     //Stop in front of player
     var hit : RaycastHit;
     var ray : Ray = new Ray(transform.position, Vector2.right);
     
     if (Physics.Raycast (ray, hit, raylength))
     {
         Debug.DrawLine (transform.position, hit.point, Color.red);
         if (hit.collider.gameObject.CompareTag("Player"))
         {
             move = false;
         }
     }
 }
Comment
Add comment · Show 2
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 LessThanEpic · May 31, 2015 at 08:11 PM 1
Share

Have you tried doing a draw line on your ray to see if the line his your player? Also, if you're just doing a distance check then a raycast is probably an unnecessarily expensive way to do it.

avatar image carlqwe · Jun 01, 2015 at 11:50 AM 0
Share

I have a debug.log so I see my ray and it is positioned wrong and is rotated wrong how so I fix that?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by karl_jones · Jun 01, 2015 at 11:58 AM

Instead of Vector2.right try transform.right. This will take into account the orientation of your object so it always does right from the object and not the world.

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 carlqwe · Jun 01, 2015 at 03:25 PM 0
Share

Thank you but how do i position it a little highter up? becouse its in the right rotation but its to far down so it doesn't collide with my player

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

22 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

Related Questions

Unity Character Controller messing up. 0 Answers

Follow mouse until mouseclick 0 Answers

hit.gameObject.AddComponent not working 1 Answer

Instantiate multiple objects and have them raycast 1 Answer

What does it mean if my RaycastHit.texturecoord changes dramatically on either side of adjacent triangle edges? 0 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