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
1
Question by HashGrove · Feb 21, 2015 at 05:05 PM · rotationraycasttransformparentchild

Raycasting from child weird behaviour

Sorry if this has appeared before but I couldn't find anything. I want to raycast from a child object towards the direction it's facing. It's rotation is changed by it's parent object accordingly but I think something is wrong. Here is the code:

 RaycastHit hit2;
         if (tip != null) {
             if (Physics.Raycast (tip.position,tip.TransformDirection(1, 0, 0) , out hit2)) {
                 Debug.DrawLine(tip.position, hit2.point);
             }
         }

And here is the part with the rotation change:

 mouseOnY = Camera.main.ScreenToWorldPoint (Input.mousePosition).y - transform.position.y;
         foreach (Transform t in upperBody) {
             t.localRotation = Quaternion.Euler (new Vector3 (t.rotation.x, t.rotation.y, mouseOnY));
         }

I uploaded a video because the problem cannot be seen in an image.

link text

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 HashGrove · Feb 21, 2015 at 07:03 PM

Sorry for wasting your time. It seem I have been rotating the parent and child at the same type so the rotations where going funky.

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 andrew-lukasik · Feb 21, 2015 at 07:32 PM 0
Share

It's ok, but I think there is more here. Imo it's just a chance that this code works as you wanted it to. Don't get me wrong - I'm amazed why it even works :) because it really shouldn't

(Give me few moments and I will write you proper code for this)

avatar image andrew-lukasik · Feb 21, 2015 at 07:47 PM 0
Share

Ok. I added proper code as a new answer. Check it out. I think that you should use. Your code is imo too messy right now and will give you more errors in the future. If you need any explanation or how to make it better, just ask.

avatar image
0

Answer by andrew-lukasik · Feb 21, 2015 at 07:44 PM

Use this code for rotation:

         Vector3 mousePositionScreen = Input.mousePosition;
         mousePositionScreen.z = Mathf.Abs( Camera.main.transform.position.z );//<without setting Z (default is 0f) this Camera.main.ScreenToWorldPoint() will give the same result as Camera.main.transform.position, srsly
         Vector3 mousePositionWorld = Camera.main.ScreenToWorldPoint( mousePositionScreen );
         float axisRotaionTowardMouse = Quaternion.LookRotation( mousePositionWorld-upperBody.position ).eulerAngles.x;
         upperBody.rotation = Quaternion.Euler( new Vector3( 0f , 0f , -axisRotaionTowardMouse ) );

To achieve better accuracy (meaning spear being more accurate in targeting mouse position) you can add similar rotation procedure to your spear Transform too:

 spearTransform.rotation = Quaternion.Euler( new Vector3( 0f , 0f , -Quaternion.LookRotation( mousePositionWorld-spearTransform.position ).eulerAngles.x ) );
 
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

21 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 avatar image avatar image

Related Questions

Camera follow ball along cylinder 1 Answer

Changing child's rotation changes parent's position 0 Answers

Separate child rotation 1 Answer

Make a simple tree 1 Answer

Having trouble with raycast rigidbody object childing for tractor beam 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