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 danizufrique · Apr 12, 2021 at 09:56 PM · rotationcollisionphysicsunityeditorarrow

Keep rotation when changing parent in Unity 3D

Hello, I'm trying to implement arrows. Every frame the arrow changes its rotation depending on its velocity, but when it hits something and I try to attach the arrow to the object rotation goes wrong. I'm sure that the problem occurs when changing parent and I tried everything without result. This is the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Arrow : MonoBehaviour
 {
     private Rigidbody rb;
     private bool hit;
     private Quaternion startingRot;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         hit = false;
         startingRot = transform.rotation;
     }
 
 
     void FixedUpdate()
     {
         if (!hit && rb.velocity.magnitude>0)
         {
             transform.localRotation = Quaternion.LookRotation(rb.velocity);
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         hit = true;
         rb.constraints = RigidbodyConstraints.FreezeRotation;
         transform.SetParent(other.transform, true);
         rb.isKinematic = true;
         rb.velocity = Vector3.zero;
     }
 
 }

Comment
Add comment · Show 8
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 HellsHand · Apr 12, 2021 at 11:49 PM 0
Share

Have you tried saving the arrows rotation right before parenting and then setting it back immediately after?

avatar image danizufrique HellsHand · Apr 13, 2021 at 02:52 PM 0
Share

Yes, but it makes no difference:

 private void OnTriggerEnter(Collider other)
     {
         startingRot = transform.rotation;
         hit = true;
         transform.SetParent(other.transform, true);
         rb.isKinematic = true;
         rb.velocity = Vector3.zero;
         transform.rotation = startingRot;
     }

avatar image HellsHand danizufrique · Apr 13, 2021 at 03:34 PM 0
Share

You could try using a raycast off of the arrow and at a certain distance grab the arrows rotation and then set it back after parenting. Something like:

     RaycastHit hit;
     if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, distanceToObject, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide))
     {
         startingRot = transform.rotation;
     }

Then you could play around a bit with distanceToObject to make it more precise. If that doesn't work could you post an image of what the arrow is actually doing? I'm kind of assu$$anonymous$$g it angles the arrow but if not an image might help.

Show more comments
avatar image AndBje · Apr 13, 2021 at 03:39 PM 0
Share

Maybe I misunderstand your problem since the code works for me. However, first I had an unwanted effect since I forgot to set the collider to be a trigger and then the object rotated weirdly and moved away. As a trigger, it is nicely parented and stops rotating.

avatar image danizufrique AndBje · Apr 13, 2021 at 05:42 PM 0
Share

Here is a clip of the problem: link text

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by AndBje · Apr 13, 2021 at 09:07 PM

This was an interesting problem (the video-clip was a very good demonstration). It seems like you can't re-parent a rigid body without getting some weird behavior. I tried several things, like disable the rigid body and re-parent first in the next frame, and so on, but it still didn't work.

The only solution that works for me is to remove the rigid body before re-parenting.

         Destroy(rb);
         transform.SetParent(other.transform, true);

If you don't like the idea of removing behaviors in run-time you could have a second copy of the arrow, without the rigid body and simply replace the original arrow with the copy.

Good hunting!

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 HellsHand · Apr 13, 2021 at 10:29 PM 0
Share

Quite interesting indeed. Noticed in the video that the collider is doing exactly what is expected but the actual mesh is rotating so the transform.rotation doesn't actually change anything since it's separated from it's collider. I tested this with a simple drop test and got the exact same result.

avatar image danizufrique · Apr 14, 2021 at 12:03 AM 0
Share

Still not working with this code:

 private void OnTriggerEnter(Collider other)
         {
             hitted = true;
             //rb.constraints = RigidbodyConstraints.FreezeRotation;
             //rb.isKinematic = true;
             //rb.velocity = Vector3.zero;
             Destroy(rb);
             transform.SetParent(other.transform, true);
         }

The result is: Clip I tried uncommenting those lines and I get the same result no matter what settings I tweek. Just in case, this is the code for the bow that affects the arrow: PasteBin

No matter what, thanks a lot for all the help!!

avatar image HellsHand danizufrique · Apr 14, 2021 at 12:42 AM 0
Share

Nah it's not your code, I'm still messing around and getting nowhere, even something like this:

 public class Arrow : MonoBehaviour
 {
     private Rigidbody rb;
     RaycastHit rayHit;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         
         if (Physics.Raycast(transform.position, Vector3.down, out rayHit, 1f, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide))
         {
             Destroy(rb);
             StartCoroutine(ParentArrow());
         }
     }
 
     IEnumerator ParentArrow()
     {
         yield return new WaitForSeconds(1);
         transform.SetParent(rayHit.transform);
     }
 }

will produce the same behavior. The arrow will sit in the correct position right up until it's parented. A clear view of what's going on

alt text alt text

As you can see the collider is still where it belongs but the Mesh has moved for some strange reason and all I'm doing is dropping the object, no other code.

On a side note, I have no idea how to make my images clickable links lol. Got it =)

screenshot-2021-04-13-204335.png (267.9 kB)
screenshot-2021-04-13-204319.png (280.1 kB)

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

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

Related Questions

How to Prevent Rolling Cube from Climbing Up Walls 0 Answers

CONTROL RIGIDBODY MOVEMENT 2 Answers

Hockey Game collider objects going through objects 0 Answers

Rigidbody doesn't rotate after collision 3 Answers

How to prevent a thrown object from rotating but not effect the physics of its collision? 0 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