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 sjefke31 · Mar 05, 2012 at 03:01 PM · gameobjectraycastrotate

Rotate object without rotating RayCast

Is it possible to rotate an object that has RayCast collision attached to it WITHOUT rotating the RayCast itself on that specific object?

When I run my game, this is how it looks like:

alt text

  • Red dot = Player, looking UP

  • Blue stripe = RayCast in front

  • Orange stripe = RayCast to the right

  • Green stripe = RayCast to the left

  • Yellow stripe = RayCast in the back

So, whenever I press (for example) Arrow Right it should rotate the OBJECT 90 degrees but the RayCast should not rotate. Like this:

alt text

  • Red dot = Player, looking to the RIGHT

  • Stripes remain the same

I can't find anything on the internet that helps me with not rotating RayCast upon object rotation. ANYONE?

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
0
Best Answer

Answer by Anne_Marije · Mar 07, 2012 at 12:15 PM

The raycast isn't actually rotated with the object. The vector.forwards you are using to cast it is rotated with the object.

You could initialize your own vectors at start and use those instead.

For example in C#

 Vector3 North;
 Vector3 South;
 Vector3 West;
 Vector3 East;
 
 void Start() //assuming player starts facing north
 {
 North = Vector3.forward;
 South = new Vector(0,0,-1)
 West = new Vector(-1,0,0)
 East = Vector.right;
 }
 checkColisionFront(North); //kind of weird methode name to be honest as this won't be the front of your gameobject anymore after rotation.

The vectors may need some tweaking, I haven't tried it. Hopefully this helps you.

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 sjefke31 · Mar 07, 2012 at 01:10 PM 0
Share

I get your point, and I applied it to my current solution. Works like a charm, thank you. Now my next issue is to rotate the object itself via the eulerAngles. Thanks again!

avatar image
1

Answer by Owen-Reynolds · Mar 05, 2012 at 03:39 PM

The direction of the raycast is entirely under your control. There's a slot where you say where it starts and what direction it goes. Fill them in with whatever you want.

If you use transform.forward then you are telling Unity to go forward based on your rotation. That's what the transform in front specially does. Likewise, if you raycast from something childed to you, you're telling Unity that the start of the raycast should rotate with you (that's what childing is for.) All you have to do is figure out what means actual North or East... . Turns out that's just Vector3.forward or Vector3.right.

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 KruegerT · Mar 05, 2012 at 03:41 PM

I don't know whether i get your question right, but you could probably just use the global direction vectors? E.g. Vector3.right, will always point in the same direction.

In your picture the player's forward-vector after the rotation is the same as the world's right-vector.

Greetings, Thomas

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 sjefke31 · Mar 06, 2012 at 08:47 AM

@Owen Reynolds, I am using the following code, this rotates my RayCast upon gameobject rotation.

 checkCollisionFront = transform.TransformDirection(Vector3.forward);
 
 if (Physics.Raycast(transform.position + new Vector3(0.15f, 0.1f, 0), checkCollisionFront, out hit, distance))
         {
             //Do Something Here
         }

Anything I should do differently?

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 Owen-Reynolds · Mar 07, 2012 at 12:41 AM 1
Share

transform.TransformDirection specifically tells Unity you want to take NORTH (which is what Vector3.forward is) and twist it to the way you are facing. You don't have to twist it like that if you actually want north.

avatar image sjefke31 · Mar 07, 2012 at 08:52 AM 0
Share

Thank you for your answer, but actually I don't really understand what you mean. With the method you just explained, how exactly can I make the RayCast always point a given direction? Even when the object where it's attached to rotates to a different direction.

So again, I have 4 RayCasts: Every RayCast points to a different direction. The RayCast is attached to an object, if the object rotates it shouldn't rotate the RayCast with it.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastAll isn't finding all possible collisions 1 Answer

Problem with the direction of the raycast when rotating 1 Answer

See what object I hit in raycast 0 Answers

Camera switch between child back to main camera issue 2 Answers

How do I ask if RaycastHit returns null? 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