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
2
Question by JoFalbo · Jun 24, 2021 at 04:10 PM · animationtransformpositionchild

My child move with the parent but can't move on it when the parent is moving

I'm moving my player which is a ball moving with a joystick with a script like that :

_rigidbody.MovePosition(transform.position + (transform.forward leftController.GetTouchPosition.y Time.deltaTime speedMovements) +(transform.right leftController.GetTouchPosition.x Time.deltaTime speedMovements));

but when I'm parenting the ball with a platform, the ball follow the platform like I want but I can't move the ball on the plateform when it's moving.

I don't know what to do to fix that.

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 DevManuel · Jun 25, 2021 at 03:27 PM 0
Share

Hi, I would change the local position of the player. I haven't tested it yet, but if I'm correct it should work:


When the parent of the player is the platform and the platform is moving, the global-position of the player is changing, but not the local-position! And I think to let the rididbody move in local space, you have to do this


So try this:

 Vector3 moveDirection = transform.localPosition + (transform.forward leftController.GetTouchPosition.y Time.deltaTime speedMovements) +(transform.right leftController.GetTouchPosition.x Time.deltaTime speedMovements) ;
 
 moveDirection = transform.TransformDirection(moveDirection);
 
 _rigidbody.MovePosition(moveDirection);


3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Crown3d · Jun 26, 2021 at 11:07 AM

Child objects follow their parent. The behavior you're describing is to be expected. Is there any particular reason why you're making the player a child of the platform?

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 JoFalbo · Jun 28, 2021 at 10:19 AM 0
Share

I want my player to follow the platform, I think parenting was the best choice to do that.

avatar image
0

Answer by DenisIsDenis · Jun 28, 2021 at 11:23 AM

In the script below, I have implemented binding an object to another. An object with this script will behave the same as if it were a child of the target. But at the same time, the object with this script can move (fall, walk, etc.). [The script seems cumbersome, but this is due to the fact that it has more than 30 lines of comments and indents.]

 using UnityEngine;
 
 public class RepeatMovement : MonoBehaviour
 {
     bool correctLastPosition;
     Transform lastTarget;
     public Transform target;
     Vector3 lastPosition;
 
     CharacterController isHasCharacterController;
 
     void Start()
     {
         isHasCharacterController = GetComponent<CharacterController>();
     }
 
     void Update()
     {
         // We must temporarily disable the
         // CharacterController (if assigned)
         // to move our object:
 
         //- - - - - - - - - - - - - - - - - - - - - -//
         // Remove this code if there is no CharacterController.
         // Only remove the condition if the
         // CharacterController is added to the
         // player object (this object).
         if (isHasCharacterController)
             isHasCharacterController.enabled = false;
         //- - - - - - - - - - - - - - - - - - - - - -//
 
         // We move our object after the target if it
         // exists and we have the correct lastPosition
         if (target && correctLastPosition)
         {
             transform.Translate(target.position - lastPosition, Space.World);
 
             lastPosition = target.position;
         }
 
         // Assign lastPosition when the target appears
         if (target && !correctLastPosition)
         {
             lastPosition = target.position;
             correctLastPosition = true;
         }
 
         // If there is no target,
         // it means lastPozition already wrong
         if (!target && correctLastPosition)
         {
             correctLastPosition = false;
         }
 
         // When we reassign the target,
         // then the lastPosition is also wrong.
         if (lastTarget != target)
         {
             lastTarget = target;
             correctLastPosition = false;
         }
 
         //- - - - - - - - - - - - - - - - - - - - - -//
         // Remove this code if there is no CharacterController.
         // Only remove the condition if the
         // CharacterController is added to the
         // player object (this object).
         if (isHasCharacterController)
             isHasCharacterController.enabled = true;
         //- - - - - - - - - - - - - - - - - - - - - -//
     }
 }

By changing the target, you can change the binding to another object. And setting the target to null will disable the binding.


@JoFalbo

EDITED: I've tested your script. It remains a mystery to me. I don't understand how, but for some reason MovePosition was ignored by the engine. BUT I found a solution. Enabling Interpolation makes MovePosition work (and why, I cannot explain). To enable interpolation, select Interpolate or Extrapolate:

Interpolation


interpolation.png (16.3 kB)
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 JoFalbo · Jun 28, 2021 at 02:34 PM 0
Share

I tried to implement this script on my player with the platform as the target. The player follows the platform like it was a child of the platform, but still can't move when the platform is moving

avatar image DenisIsDenis JoFalbo · Jun 29, 2021 at 02:25 AM 0
Share

This means that something is wrong with the player movement script. If you can show the script, then I can try to figure out what the problem is.

(Or is there nothing else besides that? If so, where is this function called?)

 _rigidbody.MovePosition
     (
     transform.position
     +
     transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements
     +
     transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements
     );
avatar image JoFalbo · Jun 29, 2021 at 05:50 PM 0
Share

I don't know why, but that solution work for me too.

Thank you it helps me a lot !

avatar image
0

Answer by JoFalbo · Jun 29, 2021 at 12:03 PM

My Player movement script is attached to the player.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 
 public class PlayerMoveController2 : MonoBehaviour
 {
     // PUBLIC
     public SimpleTouchController leftController;
     public float speedMovements;
 
     // PRIVATE
     private Rigidbody _rigidbody;
 
     void Awake()
     {
         _rigidbody = GetComponent<Rigidbody>();
     }
 
     void LateUpdate()
     {
         _rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
         (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements));
     }
 }
 
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 DevManuel · Jun 29, 2021 at 01:05 PM 0
Share

I have testet it and it worked (But I used a character Controller and not rigidbody). I have only created a simply scene with 3 cubes (player, target, collider) and added the RepeatMovement script to the player and it worked.


But I had to change the Update-function of the Repeat-script to LadeUpdate() and the player-movement script to Update()


Hope it helped you

avatar image DenisIsDenis · Jun 29, 2021 at 03:10 PM 0
Share

I supplemented the answer…

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

327 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 stop child object from twitching? 1 Answer

Animation Clobbers Position 2 Answers

Parent transform not following child transform 0 Answers

Match position of two object without making one a child of the other 4 Answers

Rotation issue with remote players projectiles 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