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

Question Children not moving with parent when parent becomes a child.

Discussion in 'Scripting' started by Crazyspawn, Jul 6, 2020.

  1. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    I'm working on a moving platform script and everything is working great except when I parent the player to the moving platform.
    Code (CSharp):
    1. public class MovingPlatform : MonoBehaviour
    2.     {
    3.         public Vector3[] points;
    4.         public int pointNumber = 0;
    5.         private Vector3 currentTarget;
    6.  
    7.         public float tolerance;
    8.         public float speed;
    9.         public float delayTime;
    10.  
    11.         private float delayStart;
    12.  
    13.         public bool automatic;
    14.  
    15.         private void Start()
    16.         {
    17.             if (points.Length > 0)
    18.             {
    19.                 currentTarget = points[0];
    20.             }
    21.             tolerance = speed * Time.deltaTime;
    22.         }
    23.  
    24.         private void FixedUpdate() //Using FixedUpdate because of character controller
    25.         {
    26.             if (transform.position != currentTarget)
    27.             {
    28.                 MovePlatform();
    29.             }
    30.             else
    31.             {
    32.                 UpdateTarget();
    33.             }
    34.         }
    35.  
    36.         private void MovePlatform()
    37.         {
    38.             Vector3 heading = currentTarget - transform.position;
    39.             transform.position += (heading / heading.magnitude) * speed * Time.deltaTime;
    40.             if (heading.magnitude < tolerance)
    41.             {
    42.                 transform.position = currentTarget;
    43.                 delayStart = Time.time;
    44.             }
    45.         }
    46.  
    47.         private void UpdateTarget()
    48.         {
    49.             if (automatic)
    50.             {
    51.                 if (Time.time - delayStart > delayTime)
    52.                 {
    53.                     NextPlatform();
    54.                 }
    55.             }
    56.         }
    57.  
    58.         public void NextPlatform()
    59.         {
    60.             pointNumber++;
    61.             if (pointNumber >= points.Length)
    62.             {
    63.                 pointNumber = 0;
    64.             }
    65.             currentTarget = points[pointNumber];
    66.         }
    67.  
    68.         private void OnTriggerEnter(Collider other)
    69.         {
    70.             if (other.gameObject.tag == "Player")
    71.             {
    72.                 other.transform.parent = transform;
    73.             }
    74.         }
    75.  
    76.         private void OnTriggerExit(Collider other)
    77.         {
    78.             other.transform.parent = null;
    79.         }
    80.  
    81.     }
    When the player enters the trigger the character controller stays on the platform but the camera moves elsewhere but is still controllable. When the player leaves the trigger and not a child of the platform anymore the camera snaps back to the player and works fine.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Sounds like the problem is really in the camera script?
     
    Joe-Censored likes this.
  3. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    I already tested that by disabling the mouse look script but it still does it.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is the mouse look script the only script which does any movement of the camera?
     
  5. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    There is some code in the player controller script where if the player uses the crouch key the camera lowers but I commented all that out and it's still doing it. I have a temporary fix where I add
    Code (CSharp):
    1. transform.position = new Vector3(playerTransform.position.x, playerTransform.position.y + 0.9f, playerTransform.position.z);
    to the mouse look Update() function, which works, but I'm not sure if that's the best way to handle this.