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 normalmaps · Feb 20, 2014 at 04:04 PM · raycastlinepointon

interesting raycast question

Hello people,

I have a (what I believe) interesting question about raycasts.

The behaviour I want:

I'm working on a project where you have the ability to teleport to a certain point in front of you (let's say 6 meters in front of you). Now what I want to avoid is that you teleport in another collider (or outside the map, but that one isn't that difficult).

if there is a better method to get this, please do enlighten me!

What I plan on trying:

I wanted to know if there is a way to call a point in your raycast. So not the hit point but somewhere between the hitpoint AND the one that casts.

so there are 2 things I want to know.

1: is this possible? 2: do you know a better way to do it?

sincerely, me

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 robertbu · Feb 20, 2014 at 04:38 PM

In order to solve this problem, you need to figure out some way to handle the fact that your character has some size. That is, say your Raycast failed at exactly 6 units, and you cast the ray from your character position, if you moved your character to that position, the character would be 1/2 inside the object that was hit.

So I'm going to define 'charRadius' as the distance from the pivot point of your character and the front of your character, and 'desiredDist' as the distance you would like to travel if you do not hit anything, and assume you want to move in the current forward direction of your character:

 var charRadius = 0.75;
 var desiredDist = 6.0; 
  
 function TeleportForward () {
     var hit : RaycastHit;
     if (Physics.Raycast(transform.position, transform.forward, hit, desiredDist+charRadius)) {
         var dir = (transform.position - hit.point).normalized;
         transform.position = hit.point + dir * charRadius;
     
     }
     else {
         transform.position += transform.forward * desiredDist;
     }
 }

The part of this code that directly answer your questions is these two lines:

     var dir = (transform.position - hit.point).normalized;
     transform.position = hit.point + dir * charRadius;

It calculates a vector from the hit point back towards the character. Then it set the position 'charRadius' along that vector. The result is that the front of your character should just be touching the surface the ray cast hit.

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 normalmaps · Feb 21, 2014 at 09:27 PM 0
Share

thanks alot robertbu! +1

avatar image
0

Answer by thornekey · Feb 20, 2014 at 04:08 PM

u could get the distance of the ray and divide by 2 and then add that amount to ur location? lol i dont even know if that will work.. its 3 am

Comment
Add comment · Show 2 · 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 normalmaps · Feb 20, 2014 at 04:11 PM 0
Share

hahaha, not what I was looking for, but thanks anyway

avatar image thornekey · Feb 20, 2014 at 04:13 PM 0
Share

soorry heheh

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

19 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

Related Questions

Find a point at a distance from point on a line passing through two points 1 Answer

How do you use a queuing mechanism in Unity? 1 Answer

Can Raycast get multiple points from the same collider? 1 Answer

Raycasting to get an exact point on an object. 2 Answers

Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 2 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