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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Transform Child Out Of Bounds

Discussion in 'Scripting' started by Vampyr_Engel, Oct 31, 2018.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    OK picked up this script and it was working OK now it has stopped working I keep getting this ''Transform Child Out Of Bounds '' error

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerShipController : MonoBehaviour
    5. {
    6.     //speed stuff
    7.     float speed;
    8.     public int cruiseSpeed;
    9.     float deltaSpeed;//(speed - cruisespeed)
    10.     public int minSpeed;
    11.     public int maxSpeed;
    12.     float accel, decel;
    13.  
    14.     //turning stuff
    15.     Vector3 angVel;
    16.     Vector3 shipRot;
    17.     public int sensitivity;
    18.  
    19.     public Vector3 cameraOffset; //I use (0,1,-3)
    20.  
    21.     void Start()
    22.     {
    23.         speed = cruiseSpeed;
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.  
    29.         //ANGULAR DYNAMICS//
    30.        
    31.         shipRot = transform.GetChild(1).localEulerAngles; //make sure you're getting the right child (the ship).  I don't know how they're numbered in general.
    32.  
    33.         //since angles are only stored (0,360), convert to +- 180
    34.         if (shipRot.x > 180) shipRot.x -= 360;
    35.         if (shipRot.y > 180) shipRot.y -= 360;
    36.         if (shipRot.z > 180) shipRot.z -= 360;
    37.  
    38.         //vertical stick adds to the pitch velocity
    39.         //         (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
    40.         angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
    41.  
    42.         //horizontal stick adds to the roll and yaw velocity... also thanks to the .5 you can't turn as fast/far sideways as you can pull up/down
    43.         float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
    44.         angVel.y += turn * .5f;
    45.         angVel.z -= turn * .5f;
    46.  
    47.  
    48.         //shoulder buttons add to the roll and yaw.  No deltatime here for a quick response
    49.         //comment out the .y parts if you don't want to turn when you hit them
    50.         if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
    51.         {
    52.             angVel.y -= 20;
    53.             angVel.z += 50;
    54.             speed -= 5 * Time.fixedDeltaTime;
    55.         }
    56.  
    57.         if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
    58.         {
    59.             angVel.y += 20;
    60.             angVel.z -= 50;
    61.             speed -= 5 * Time.fixedDeltaTime;
    62.         }
    63.  
    64.  
    65.         //your angular velocity is higher when going slower, and vice versa.  There probably exists a better function for this.
    66.         angVel /= 1 + deltaSpeed * .001f;
    67.  
    68.         //this is what limits your angular velocity.  Basically hard limits it at some value due to the square magnitude, you can
    69.         //tweak where that value is based on the coefficient
    70.         angVel -= angVel.normalized * angVel.sqrMagnitude * .08f * Time.fixedDeltaTime;
    71.  
    72.  
    73.         //and finally rotate.
    74.         transform.GetChild(1).Rotate(angVel * Time.fixedDeltaTime);
    75.  
    76.         //this limits your rotation, as well as gradually realigns you.  It's a little convoluted, but it's
    77.         //got the same square magnitude functionality as the angular velocity, plus a constant since x^2
    78.         //is very small when x is small.  Also realigns faster based on speed.  feel free to tweak
    79.         transform.GetChild(1).Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
    80.  
    81.  
    82.         //LINEAR DYNAMICS//
    83.  
    84.         deltaSpeed = speed - cruiseSpeed;
    85.  
    86.         //This, I think, is a nice way of limiting your speed.  Your acceleration goes to zero as you approach the min/max speeds, and you initially
    87.         //brake and accelerate a lot faster.  Could potentially do the same thing with the angular stuff.
    88.         decel = speed - minSpeed;
    89.         accel = maxSpeed - speed;
    90.  
    91.         //simple accelerations
    92.         if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
    93.             speed += accel * Time.fixedDeltaTime;
    94.         else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
    95.             speed -= decel * Time.fixedDeltaTime;
    96.  
    97.         //if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
    98.         //(added clamping since it's more of a gradual slowdown/speedup)
    99.         else if (Mathf.Abs(deltaSpeed) > .1f)
    100.             speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
    101.  
    102.  
    103.         //moves camera (make sure you're GetChild()ing the camera's index)
    104.         //I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
    105.         transform.GetChild(0).localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
    106.  
    107.  
    108.         float sqrOffset = transform.GetChild(1).localPosition.sqrMagnitude;
    109.         Vector3 offsetDir = transform.GetChild(1).localPosition.normalized;
    110.  
    111.  
    112.         //this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
    113.         //I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
    114.         transform.GetChild(1).Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
    115.                                                        //(**************** this ***************) is what actually makes the whole ship move through the world!
    116.         transform.Translate((offsetDir * sqrOffset * 50 + transform.GetChild(1).forward * speed) * Time.fixedDeltaTime, Space.World);
    117.  
    118.         //comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
    119.         transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
    120.     }
    121.  
    122.     void Update()
    123.     {
    124.     }
    125. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You are accessing transform.GetChild(1) everwhere. That's at least part of your problem.

    Instead create a public member and drag the transform you actually WANT into that field in the editor:

    Code (csharp):
    1. public Transform TheChildIWant;
    And then use TheChildIWant instead, everywhere that you're presently doing the getchild() construct.
     
  3. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PlayerShipController : MonoBehaviour
    7. {
    8.     //speed stuff
    9.     float speed;
    10.     public int cruiseSpeed;
    11.     float deltaSpeed;//(speed - cruisespeed)
    12.     public int minSpeed;
    13.     public int maxSpeed;
    14.     float accel, decel;
    15.     public Transform TheChildIWant;
    16.     //turning stuff
    17.     Vector3 angVel;
    18.     Vector3 shipRot;
    19.     public int sensitivity;
    20.  
    21.     public Vector3 cameraOffset; //I use (0,1,-3)
    22.  
    23.     void Start()
    24.     {
    25.         speed = cruiseSpeed;
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.  
    31.         //ANGULAR DYNAMICS//
    32.      
    33.         shipRot = transform.TheChildIWant.localEulerAngles; //make sure you're getting the right child (the ship).  I don't know how they're numbered in general.
    34.  
    35.         //since angles are only stored (0,360), convert to +- 180
    36.         if (shipRot.x > 180) shipRot.x -= 360;
    37.         if (shipRot.y > 180) shipRot.y -= 360;
    38.         if (shipRot.z > 180) shipRot.z -= 360;
    39.  
    40.         //vertical stick adds to the pitch velocity
    41.         //         (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
    42.         angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
    43.  
    44.         //horizontal stick adds to the roll and yaw velocity... also thanks to the .5 you can't turn as fast/far sideways as you can pull up/down
    45.         float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
    46.         angVel.y += turn * .5f;
    47.         angVel.z -= turn * .5f;
    48.  
    49.  
    50.         //shoulder buttons add to the roll and yaw.  No deltatime here for a quick response
    51.         //comment out the .y parts if you don't want to turn when you hit them
    52.         if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
    53.         {
    54.             angVel.y -= 20;
    55.             angVel.z += 50;
    56.             speed -= 5 * Time.fixedDeltaTime;
    57.         }
    58.  
    59.         if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
    60.         {
    61.             angVel.y += 20;
    62.             angVel.z -= 50;
    63.             speed -= 5 * Time.fixedDeltaTime;
    64.         }
    65.  
    66.  
    67.         //your angular velocity is higher when going slower, and vice versa.  There probably exists a better function for this.
    68.         angVel /= 1 + deltaSpeed * .001f;
    69.  
    70.         //this is what limits your angular velocity.  Basically hard limits it at some value due to the square magnitude, you can
    71.         //tweak where that value is based on the coefficient
    72.         angVel -= angVel.normalized * angVel.sqrMagnitude * .08f * Time.fixedDeltaTime;
    73.  
    74.  
    75.         //and finally rotate.
    76.         transform.TheChildIWant.Rotate(angVel * Time.fixedDeltaTime);
    77.  
    78.         //this limits your rotation, as well as gradually realigns you.  It's a little convoluted, but it's
    79.         //got the same square magnitude functionality as the angular velocity, plus a constant since x^2
    80.         //is very small when x is small.  Also realigns faster based on speed.  feel free to tweak
    81.         transform.TheChildIWant.Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
    82.  
    83.  
    84.         //LINEAR DYNAMICS//
    85.  
    86.         deltaSpeed = speed - cruiseSpeed;
    87.  
    88.         //This, I think, is a nice way of limiting your speed.  Your acceleration goes to zero as you approach the min/max speeds, and you initially
    89.         //brake and accelerate a lot faster.  Could potentially do the same thing with the angular stuff.
    90.         decel = speed - minSpeed;
    91.         accel = maxSpeed - speed;
    92.  
    93.         //simple accelerations
    94.         if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
    95.             speed += accel * Time.fixedDeltaTime;
    96.         else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
    97.             speed -= decel * Time.fixedDeltaTime;
    98.  
    99.         //if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
    100.         //(added clamping since it's more of a gradual slowdown/speedup)
    101.         else if (Mathf.Abs(deltaSpeed) > .1f)
    102.             speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
    103.  
    104.  
    105.         //moves camera (make sure you're GetChild()ing the camera's index)
    106.         //I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
    107.         transform.TheChildIWant.localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
    108.  
    109.  
    110.         float sqrOffset = transform.TheChildIWant.localPosition.sqrMagnitude;
    111.         Vector3 offsetDir = transform.TheChildIWant.localPosition.normalized;
    112.  
    113.  
    114.         //this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
    115.         //I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
    116.         transform.GetChild(1).Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
    117.                                                        //(**************** this ***************) is what actually makes the whole ship move through the world!
    118.         transform.Translate((offsetDir * sqrOffset * 50 + transform.GetChild(1).forward * speed) * Time.fixedDeltaTime, Space.World);
    119.  
    120.         //comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
    121.         transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
    122.     }
    123.  
    124.     void Update()
    125.     {
    126.     }
    127. }

    Getting Errors
     
    Last edited: Oct 31, 2018
  4. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Assets/PlayerShipController.cs(109,33): error CS1061: Type `UnityEngine.Transform' does not contain a definition for `TheChildIWant' and no extension method `TheChildIWant' of type `UnityEngine.Transform' could be found. Are you missing an assembly reference?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It isn't "transform.TheChildIWant.localPosition". TheChildIWant is defined in your PlayerShipController class, not in Unity's Transform class. Just use "TheChildIWant.localPosition".
     
  6. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    OK thanks though I got it to work but what I really want to do is make sure that X axis and Y axis are for steering and pitching upwards and downwards and the U axis ( or I think the 5th axis) is for the throttle and the 4th axis would be for the rudders I have been trying to get this to work like that but I think all the control instructions are coded in
     
  7. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449