Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 YosephWordofa · Aug 10, 2020 at 01:03 AM · rigidbody2dplayer movementplatformisometricmovment

How to move a child object freely (within), when parented to a new parent Object?

I am creating a moving platform a player can stand on, the platform is simple, just moves left and right. so what i have done is simply move the player Object and attach it to the platform Object whenever OntriggerEnter2D, and on Exit it just simply gets removed as a child of the platform, simple enough.

The problem, however, is when once player becomes the child of the platform it is nearly imposible to move, i can move just incredibly slow (i have the platform set to trigger and rigid to kinematic) where have i gone wrong and why cant i move normally? Code below if needed, any help is greatly appreciated.

//This is isometric Player movement strait from Unity sources

    void FixedUpdate()
  {
 Vector2 currentPos = rbody.position;
 float horizontalInput = Input.GetAxis("Horizontal"); //* movementSpeed;
 float verticalInput = Input.GetAxis("Vertical");// * movementSpeed;
 Vector2 inputVector = new Vector2(horizontalInput, verticalInput);
 inputVector = Vector2.ClampMagnitude(inputVector, 1);
 Vector2 movement = inputVector * movementSpeed;
 Vector2 newPos = currentPos + movement * Time.fixedDeltaTime;
 isoRenderer.SetDirection(movement);
 rbody.MovePosition(newPos);
   }

//this is the moving Platform movement

   public class Patroll : StateMachineBehaviour
 {

   GameObject NPC;
   GameObject[] waypoints;
    int currentWP;

   void Awake()
 {
 waypoints = GameObject.FindGameObjectsWithTag("WayPoint");
  }
   override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  {
 NPC = animator.gameObject;
 currentWP = 0;
   }
   override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
   {
 if (waypoints.Length == 0) return;
 if (Vector2.Distance(waypoints[currentWP].transform.position, NPC.transform.position) < 0.2f)
 {
     currentWP++;
     if (currentWP >= waypoints.Length)
     {
         currentWP = 0;
     }
 }
 
 NPC.transform.localPosition = Vector2.MoveTowards(NPC.transform.position, waypoints[currentWP].transform.position, Time.deltaTime * 1.0f);
   }
     }

//and this is the interaction is being handled

    public class MovingObject : MonoBehaviour
   {

     private void OnTriggerEnter2D(Collider2D other)
 {
 if (other.transform.gameObject.tag == "Player")
 {
     other.transform.parent = transform;

     //other.transform.parent.SetParent(transform);
 }

   }
 private void OnTriggerExit2D(Collider2D other)
   {

 if (other.transform.gameObject.tag == "Player")

 {
     other.transform.parent = null;
 }

   }
   }
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
Best Answer

Answer by WIIASD · Aug 10, 2020 at 02:39 AM

You are actually using Moveposition of rigidbody to move the player, but while on the platform, the player's position is already continuously changing even without input so that when you get your player moving in the opposite direction of the platform, I believe their position changing will be partially cancel out.

The solution I can come up with is to simply modify the velocity of the rigidbody to move it instead of changing its direction directly

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 YosephWordofa · Aug 10, 2020 at 11:28 AM

     this should do it figured it out no to long ago, could have used your feedback before i lost my mind lol!

``rbody.velocity = new Vector2(horizontalInput, verticalInput);

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

141 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

Related Questions

How to stop a moving 2D player ? 1 Answer

Why isn't my player moving? 1 Answer

Player falling through the floor on running? 1 Answer

Player slides off from moving platform when player isn't moving Bolt/visual scripting 0 Answers

Rigidbody stops working when parented to a moving object 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