Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Moving platform affects my player movement

Discussion in 'Scripting' started by AudaxDeus, Apr 27, 2021.

  1. AudaxDeus

    AudaxDeus

    Joined:
    Apr 24, 2021
    Posts:
    3
    Hello, this is my first post and I am currently new to unity. I've made a simple moving platform script that moves a block from point to point. My problem is that my player's movement becomes weird once I enter and exit the platform. I do not know why this is happening and would want an explanation and how to solve the problem. Thank you.

    On my OnCollision methods I am moving the parent player object inside the moving platform/block when I touch it and on exit I move it back outside.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BlockMover : MonoBehaviour
    6. {
    7.     [SerializeField] Vector3[] points;
    8.     [SerializeField] int pointNumber = 0;
    9.     [SerializeField] bool repeat = false;
    10.     [SerializeField] float blockSpeed = 10f;
    11.     [SerializeField] float delayTime = 2;
    12.     float delayStart;
    13.  
    14.     private void Start()
    15.     {
    16.     }
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         if (transform.position != points[pointNumber])
    21.         {
    22.             MovePlatform();
    23.         }
    24.         else
    25.         {
    26.             UpdateTarget();
    27.         }
    28.     }
    29.  
    30.     void MovePlatform()
    31.     {
    32.         transform.position = Vector3.MoveTowards(transform.position, points[pointNumber], blockSpeed * Time.deltaTime);
    33.         Vector3 heading = transform.position - points[pointNumber];
    34.         if (heading.magnitude < blockSpeed * Time.deltaTime)
    35.         {
    36.             delayStart = Time.time;
    37.         }
    38.     }
    39.  
    40.     void UpdateTarget()
    41.     {
    42.         if (Time.time - delayStart > delayTime)
    43.         {
    44.             if (repeat)
    45.             {
    46.                 pointNumber++;
    47.                 if (pointNumber >= points.Length) pointNumber = 0;
    48.             }
    49.             else
    50.             {
    51.                 if (pointNumber != points.Length - 1) pointNumber++;
    52.             }
    53.         }
    54.     }
    55.  
    56.     private void OnCollisionEnter(Collision other)
    57.     {
    58.         if (other.transform.tag == "Player")
    59.             other.transform.parent.SetParent(transform);
    60.     }
    61.  
    62.     private void OnCollisionExit(Collision other)
    63.     {
    64.         if (other.transform.tag == "Player")
    65.             other.transform.parent.SetParent(null);
    66.     }
    67. }
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    I dont understand what do you mean by weird but from your post.

    If you move some object under parent, it change position relative to its parent.So i think if you remove those 2 methods you get rid of your "weird" movement.Also for what reason you set is as child and then remove it?This can be good if you want some "weird" movement and channge position of object but i think it is not your case.
    Code (CSharp):
    1.   private void OnCollisionEnter(Collision other)
    2.     {
    3.         if (other.transform.tag == "Player")
    4.             other.transform.parent.SetParent(transform);
    5.     }
    6.     private void OnCollisionExit(Collision other)
    7.     {
    8.         if (other.transform.tag == "Player")
    9.             other.transform.parent.SetParent(null);
    10.     }
     
    Last edited: Apr 27, 2021
  3. AudaxDeus

    AudaxDeus

    Joined:
    Apr 24, 2021
    Posts:
    3
    I do that so that the player won't slide off the moving platform. If I remove the code you mentioned the player will slide of the platform instead of moving with it.
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    And cant you just set that player will move with platform only OnCollisionStay?I mean with no child,parent things becuase that is imo what cause "weird movement"
     
  5. AudaxDeus

    AudaxDeus

    Joined:
    Apr 24, 2021
    Posts:
    3
    Ahh I see, I did the child parent thing since it was easier to do. I'll try your suggestion see if I can make it work. Thanks.