Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Danao · Mar 16, 2017 at 12:44 PM · scaleparentchildontriggerenter2ddetach

How can I reattach the player transform after using DetachChildren() to flip the parent?

I want my player to "ride' the moving object (which has a definite front and back) so I'm making him a child of the object with OnCollisionEnter2D. Before flipping the object, I am using DetachChildren() but need to attach him again once the object is flipped (since OnCollisionExit2D will not be called until the player leaves the object). Any suggestions would be greatly appreciated!

Comment
Add comment · Show 1
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 justDeek · Mar 17, 2017 at 11:16 PM 0
Share

I have only an assumptions, that's why I write a comment ins$$anonymous$$d of an answer, but try it with FixedUpdate() or LateUpdate() ins$$anonymous$$d of Update(). That's at least what helped me sometimes when an Object had a janky movement to it, since Update() is frame-dependent it can lead to such problems.

2 Replies

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

Answer by Danao · Mar 20, 2017 at 02:32 AM

The solution was much more simple and elegant than I was originally trying to make it. All I needed to do was add a bool to check if "colliding" was true during the change of direction, if so, then use SetParent to reattach the player:

using UnityEngine;

     public class:  MovingObject : MonoBehaviour {
     
         public float speed;
         public float distance;
         private float xStartPosition;
         private Transform PlayerAttach;
         private bool colliding = false;
     
         void Start()
         {
             xStartPosition = transform.position.x;
         }
     
         void FixedUpdate()
         {
                 if ((speed < 0 && transform.position.x < xStartPosition) || (speed > 0 && transform.position.x > xStartPosition + distance))
                 {
                 transform.DetachChildren();               
                 speed *= -1;
                     // Multiply the object's x local scale by -1
                     Vector3 theScale = transform.localScale;
                     theScale.x *= -1;
                     transform.localScale = theScale;                    
                 }
                 transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
 
 if(colliding = true)
 {
 PlayerAttach.SetParent(gameobject.transform);            
 }
          
     void OnCollisionEnter2D(Collision2D other)
 {
         other.transform.SetParent(gameObject.transform);
         PlayerAttach = other.transform;
         colliding = true;
 }
 
 private void OnCollisionExit2D(Collision2D other)
 {
            colliding = false;
             transform.DetachChildren();           
 }
 }
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 justDeek · Mar 18, 2017 at 01:35 PM

@Danao

I tried to replicate your setting and came to the following suggestions: When the player is a child of the moving object it should be recentered (a position of (0,0,0)) so that it's previous position doesn't get affected by the flip (wich is probably what causes the erratical movement).

Other Solutions would be to set the moving object a child of the player, then the player follows the moving object but with an undampened offset. A better approach would be to unparent the moving object from the player (have them both separate) and add the SmoothFollow script from the Standard Assets to the player, wich you can enable when OnCollisionEnter and disable when OnCollisionExit.

Another problem could be that you use OnCollision instead of OnTrigger and set the Collider of either the player, the moving object or both to "is Trigger", so that they don't collide constantly, wich could also be the source of your problem.

The SmoothFollow.cs script:
 using UnityEngine;
 
 namespace UnityStandardAssets.Utility
 {
     public class SmoothFollow : MonoBehaviour
     {
 
         // The target we are following
         [SerializeField]
         private Transform target;
         // The distance in the x-z plane to the target
         [SerializeField]
         private float distance = 0f;
         // the height we want the camera to be above the target
         [SerializeField]
         private float height = 0.5f;
 
         [SerializeField]
         private float rotationDamping;
         [SerializeField]
         private float heightDamping;
 
         // Use this for initialization
         void Start() { }
 
         // Update is called once per frame
         void LateUpdate()
         {
             // Early out if we don't have a target
             if (!target)
                 return;
 
             // Calculate the current rotation angles
             var wantedRotationAngle = target.eulerAngles.y;
             var wantedHeight = target.position.y + height;
 
             var currentRotationAngle = transform.eulerAngles.y;
             var currentHeight = transform.position.y;
 
             // Damp the rotation around the y-axis
             currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
             // Damp the height
             currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
             // Convert the angle into a rotation
             var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
 
             // Set the position of the camera on the x-z plane to:
             // distance meters behind the target
             transform.position = target.position;
             transform.position -= currentRotation * Vector3.forward * distance;
 
             // Set the height of the camera
             transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
 
             // Always look at the target
             transform.LookAt(target);
         }
     }
 }

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 Danao · Mar 19, 2017 at 01:21 PM 0
Share

@pik$$anonymous$$36 I found the answer I needed. I appreciate your help looking into the problem and sent a reward point your way!

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

96 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

Related Questions

Can not access GetComponentInParent ( ). usegravity 1 Answer

GameObject "child" don't follow "parent". 1 Answer

How can i call a spell whit a animation events .. 0 Answers

From tutorial: Camera rotates when making child of Player object. 1 Answer

How to rotate a child object independently from it's parent so that it maintains it's transform integrity and shape 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