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 TheBombix · Jun 04, 2012 at 03:17 PM · rotationraycastlookat

[HELP]Object Look At Raycast position

Hi guys, I'm here another time with another problem that i can't solve Ok here is the code

 var hit : RaycastHit;
 var direction = CameraTarget.transform.TransformDirection(Vector3.forward);

 if(CameraTarget){
     var rotationY = Quaternion.EulerRotation(Vector3(0,Physics.Raycast(CameraTarget.transform.position, direction, hit),0));
     var rotationX = Quaternion.EulerRotation(Vector3(Physics.Raycast(CameraTarget.transform.position, direction, hit),0,0));    

     Misc_a.transform.localRotation = rotationY;
     Misc_b.transform.localRotation = rotationX;
 }
 

Ok now i'll explain. I have a 3D model of a minigun Platform(like the one on the blackhawk helicopter), now the model if made with 4 parts: 1. The base of the minigun 2. The Misc_a object 3. The Misc_b object 4. The Misc_c object The base doesn't move or rotate, the Misc_a must rotate along its own Y axis looking at the Raycast Point (that has the origin from an object above the Main Camera that cast a ray), the misc_b must rotate along its own X axis looking at the Raycast Point, but in my script there are 2 errors related at the Vector3 in the Quaternion.EulerRotation. Can anyone tell me how to do what i've writed above?

Thanks Bye

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
0

Answer by Berenger · Jun 04, 2012 at 03:23 PM

Physics.Raycast returns a boolean, not a number. If you want the point the ray hits, it's hit.point.

For the rotation, you can calculate the vector gun -> point, set the y = 0 for Misc_a and x = 0 for Misc_b and affect it to the respective transform.forward.

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 GutoThomas · Jun 04, 2012 at 03:29 PM

Maybe:

 var hit : RaycastHit;
 var direction = CameraTarget.transform.TransformDirection(Vector3.forward);
 
 if(Physics.Raycast(CameraTarget.transform.position, direction, hit){    
 
     Misc_a.rotation = Quaternion.LookRotation(hit.point);
     Misc_a.eulerAngles = Vector3(0, Misc_a.eulerAngles.y, 0); // reset the rotation in x and z axis
 
     Misc_b.rotation = Quaternion.LookRotation(hit.point);
     Misc_b.eulerAngles = Vector3(Misc_b.eulerAngles.x, 0, 0); // reset the rotation in y and z axis
 }

Firstly, you need to launch the raycast to store the position where it collided with something. Then, you'll be able to play with this collision point as you want.

Comment
Add comment · Show 10 · 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 Berenger · Jun 04, 2012 at 03:38 PM 0
Share

(I assumed your answer was a comment to José's answer.)

Giacomo : it gives me 2 errors about $$anonymous$$isc_a.rotation = Quaternion.LookRotation(Vector3(hit.point)); $$anonymous$$isc_b.rotation = Quaternion.LookRotation(Vector3(hit.point));

it tells: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(UnityEngine.Vector3)'.

avatar image TheBombix · Jun 04, 2012 at 03:39 PM 0
Share

It gives me 2 errors: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(UnityEngine.Vector3)'.

the lines are: $$anonymous$$isc_a.rotation = Quaternion.LookRotation(Vector3(hit.point)); $$anonymous$$isc_b.rotation = Quaternion.LookRotation(Vector3(hit.point));

maybe the Vector3 must have 3 value, not only one?

avatar image Berenger · Jun 04, 2012 at 03:43 PM 0
Share

hit.point already is a Vector3.

avatar image GutoThomas · Jun 04, 2012 at 03:43 PM 0
Share

Sorry, it's obviously not needed to call the constructor, since hit.point is already a Vector3. $$anonymous$$y bad.

just:

$$anonymous$$isc_a.rotation = Quaternion.LookRotation(hit.point);

avatar image TheBombix · Jun 04, 2012 at 03:51 PM 0
Share

ehm it's still not working, damn the $$anonymous$$igun $$anonymous$$isc_b doesn't follow the $$anonymous$$isc_a movement and these 2 parts point an empty space in the game scene, i don't know why. Any suggestion? And thanks for scripts and comments.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

SphereCast help 1 Answer

Make character look at mouse position, not world position 3 Answers

LookAt To Only Rotate on Y Axis 2 Answers

Top down look rotation snaps when looking left, right, and back 1 Answer

Rotate object to lookAt point and be perpendicular to the ground 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