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 convictcartel · Apr 12, 2014 at 04:29 AM · raycasthover

Raycast Hover

I have a ufo that hovers but I want to use raycast so the player can hide from it.. sadly I dont know that much can someone help me make it so that it only follows within the ray cast?

 var verticalDistance : float; // distance above player to hover
 
 var maxMovementSpeed : float; // units per second
 
 var target : Transform; // transform to hover over
 
  // JavaScript example.
 function Update () {
 // Bit shift the index of the layer (8) to get a bit mask
 var layerMask = 1 << 11;
 // This would cast rays only against colliders in layer 8.
 // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
 layerMask = ~layerMask;
  
 var hit : RaycastHit;
 // Does the ray intersect any objects excluding the player layer
 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.down), hit, Mathf.Infinity, layerMask)) {
 Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.down) * hit.distance, Color.yellow);
 print ("Did Hit");
 } else {
 Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.downd) *1000, Color.white);
 print ("Did not Hit");
 }
 }
 
 function OnTriggerStay() {
 
  var movement = target.position - transform.position;
 
  movement.y -= verticalDistance;
 
  // Move either at movement speed or directly to desired position, whichever's smaller. Prevents jittering hopefully.
 
  transform.position += Vector3.Min( movement * Time.deltaTime, movement.normalized * maxMovementSpeed * Time.deltaTime );
 
 }
Comment
Add comment · Show 1
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 getyour411 · Apr 12, 2014 at 04:41 AM 0
Share

I would not do this in Update()...

 // Bit shift the index of the layer (8) to get a bit mask
 var layer$$anonymous$$ask = 1 << 11;
 // This would cast rays only against colliders in layer 8.
 // But ins$$anonymous$$d we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
 layer$$anonymous$$ask = ~layer$$anonymous$$ask;

as it seems needlessly complicated/repeated. Ins$$anonymous$$d, why not do this (add around line 3)

 var layer$$anonymous$$ask : Layer$$anonymous$$ask;

which will expose a simple dropdown button in the Editor that you can use to check/uncheck the layers the Raycast interacts with.

I think what you want the UFO to do is the code in the OnTrigger, so move that into the if(Raycast) block.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

rotating hovercar to be at the same angle as the terrain/object below it 1 Answer

Make camera hover at 50 over hilly terrain 2 Answers

Jittery Movement 3 Answers

Aligning player to surface 2 Answers

How to detect UI Elements with Raycast 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