Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Luke 4 · Apr 26, 2011 at 12:00 AM · raycastdirectionraycasthitsphere

RayCast to all directions

Hi,

i am having issue with raycasting. I would need object to do a raycast to all directions from its position and then check the rayhit vector so i could invert the vector for the object so it moves away from the rayhit. is this somehow possible ? Something like checksphere, but getting also the data from the hit, mostly the vector between the object and the rayhit.

I did some solution with raycast into 5 directions combined with checksphere, but its too heavy i guess, its almost 500 lines and i dont think its clean solution, when i am sure there is better way.

Thanks for any advise.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Henrique Vilela · May 04, 2011 at 01:37 AM

You can use Physics.OverlapSphere to get all the objects that are on the desired radius, than Transform.TransformPoint on the enemy position to get the direction vector.

Comment
Add comment · 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
0

Answer by zmar0519 · Apr 26, 2011 at 12:30 AM

look here for a raycasting version of a collider, not sure if it's exactly what your looking for, but it wouldn't be to hard to modify it to have hit raycast hit vars. Good luke, Luke!!! :)

Also, the code is listed below:

#pragma strict

var layerMask : LayerMask; //make sure we aren't in this layer var skinWidth : float = 0.1; //probably doesn't need to be changed private var minimumExtent : float; private var partialExtent : float; private var sqrMinimumExtent : float; private var previousPosition : Vector3; private var myRigidbody : Rigidbody; //initialize values function Awake() { myRigidbody = rigidbody; previousPosition = myRigidbody.position; minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z); partialExtent = minimumExtent*(1.0 - skinWidth); sqrMinimumExtent = minimumExtent*minimumExtent; }

function FixedUpdate() { //have we moved more than our minimum extent? var movementThisStep : Vector3 = myRigidbody.position - previousPosition; var movementSqrMagnitude : float = movementThisStep.sqrMagnitude; if (movementSqrMagnitude > sqrMinimumExtent) { var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude); var hitInfo : RaycastHit; //check for obstructions we might have missed if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value)) myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent; } previousPosition = myRigidbody.position; }

Comment
Add comment · 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
0

Answer by zmar0519 · May 04, 2011 at 12:43 AM

You could use Vector3.distance to calculate if something came close, not sure how to tell which direction it's coming from, though.

Comment
Add comment · 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
0

Answer by Richard J. Hansen · May 04, 2011 at 03:25 AM

Could you you just use a collision sphere and then just the direction to/away from the contact point?

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Magic Leap Raycasting - Direction 2 Answers

Raycast not hitting instantiated object when timescale is 0 1 Answer

Trying to play other objects' sound and animations from Raycast hit 1 Answer

How to get the number of objects that are on a Raycast Path? 1 Answer

RaycastHit2D.collider is null - why? 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