• 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 Cherno · Apr 07, 2013 at 05:28 PM · raycastoffset

Changing Raycast origin upwards (offset)

Hello, I have an annoying problem and I seached everywhre but couldn't find the solution.

I need t ochange the origin point of a raycast, I defined localOffset for it and I did exactly like all solutions that are supposed to work, but it is just not working:

All it does is change the direction of the ray, the origin however still stays at the origin of the game object (player character in this case, which is far too low). So now the ray goes forward and up a bit.

I want to cast from about middle height (0.5).

 var localOffset = transform.position + transform.up * 0.5;
 var hitCollision : RaycastHit;
 var CollisionCheckRange : int = 1.1;
 
 if (Physics.Raycast (localOffset, transform.forward, hitCollision, CollisionCheckRange))
Comment
Add comment · Show 5
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 Fattie · Apr 08, 2013 at 06:32 PM 0
Share

I will tell you a GREAT SOLUTION - what you usually see vid games

$$anonymous$$ake a marker object .. ie just an empty game object. Name it "HappyStartPosition". Drop it inside your cowboy. (or whatever the character is)

Just $$anonymous$$OVE IT AROUND in the editor, place it where you want the ray to begin from.

This is a beautiful solution. You see this constantly in video games.

$$anonymous$$ake your ray start simply from that point, from HappyStartPosition.position. Forget local space, etc.

For instance i was just looking at a little chatacter, he had like ten! of these. For example "bullet begins from here" "glowing light drops from this point" "attach sword right here" and so on. You get it?

It's great - and I hope it solves your woe completely

avatar image Cherno · Apr 08, 2013 at 06:49 PM 0
Share

Indeed a great idea. I just make it a child of the player object. Now I just have to find out how to access the variables it puts out in my main movement script, a whole new can of worms opened so to speak :) But I have to learn it anways for my elevator script, so thanks for the suggestion!

I will flag your answer as a solution once I have it all working correctly, it may take a few days but I have this question page bookmarked.

avatar image AlucardJay · Apr 08, 2013 at 07:02 PM 1
Share

Wow, I cannot believe you acknowledged that you were told 30 $$anonymous$$utes ago, and I just told you on your other question 5 $$anonymous$$utes ago. You won't be posting another comment as an answer again, will you... no? good.

  • Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

  • Read : http://answers.unity3d.com/page/newuser.html

avatar image Dracorat · Apr 08, 2013 at 07:03 PM 0
Share

This is also what I'm doing - I have guns on a ship and each has an Empty at the correct position that I use for starting various position-based checks at the right location. I made sure each faces "forward" in the direction I expect too in order to $$anonymous$$imize the amount of walking up and down the object tree as needed. In short, Fattie is spot on.

avatar image Fattie · Apr 09, 2013 at 06:02 AM 0
Share

to access variables, check out the tutorials on unityGE$$anonymous$$S.com it's very easy and it is the core of unity program$$anonymous$$g!

1 Reply

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

Answer by Benproductions1 · Apr 08, 2013 at 01:17 AM

Hello,

You should look up how Vectors work and the difference between global and local space. The origin of a Raycast is in global space, your localOffset is also in global space, therefore you have to convert your localOffset to global space, when you use it as global space. To do this, I suggest you use the Transform function called TransformPoint It transforms a point from local space to global space. Therefore:

 if (Physics.Raycast(transform.TransformPoint(localOffset), transform.forward/*... etc*/)) {

Hope this helps, Benproductions1

Comment
Add comment · Show 3 · 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 Cherno · Apr 08, 2013 at 06:26 PM 0
Share

Thanks for the suggestion, unfortunately it does not help, it still just changes the direction of the ray but the origin still remains at the game object's origin (i.e. the bottom).

Another solution which is infact like my code looks at the moment is presented here:

http://answers.unity3d.com/questions/224511/how-can-i-offset-a-raycast-along-the-transforms-lo.html

But that user had the same problem as me so he had to resort to a workaround.

avatar image Benproductions1 · Apr 08, 2013 at 10:37 PM 0
Share

@Cherno It does work, your just not using it right

avatar image Cherno · Apr 09, 2013 at 05:57 AM 0
Share

Heh you are right I forgot to change my Debug ray as well! Thanks!

Edit: On further testing it breaks down as soon as I turn my player object, the point the ray is pointing at is then 1 unit to the right (left turn) or left (right turn).

Edit2: Now the original solution from the other linked thread works, I don't know what to think anymore, maybe I just forgot to change my DeBug rays and hence was under a false assumption. Anyway here's the working line of code again. Don't forget that a DeBug ray also needs the localOffset ins$$anonymous$$d of a transform.position :)

 if  (Physics.Raycast (localOffset, transform.forward, hitCollision, CollisionCheckRange))

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Offset of texture with Raycast 1 Answer

How to get an offset of a RaycastHit? 0 Answers

Raycast refuses to cast down 1 Answer

raycast object instance offset? 1 Answer

Multiple Raycasts 3 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