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
using UnityEngine;
using System.Collections;
public class PlayerShipController : MonoBehaviour
{
//speed stuff
float speed;
public int cruiseSpeed;
float deltaSpeed;//(speed - cruisespeed)
public int minSpeed;
public int maxSpeed;
float accel, decel;
//turning stuff
Vector3 angVel;
Vector3 shipRot;
public int sensitivity;
public Vector3 cameraOffset; //I use (0,1,-3)
void Start()
{
speed = cruiseSpeed;
}
void FixedUpdate()
{
//ANGULAR DYNAMICS//
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.
//since angles are only stored (0,360), convert to +- 180
if (shipRot.x > 180) shipRot.x -= 360;
if (shipRot.y > 180) shipRot.y -= 360;
if (shipRot.z > 180) shipRot.z -= 360;
//vertical stick adds to the pitch velocity
// (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
//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
float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
angVel.y += turn * .5f;
angVel.z -= turn * .5f;
//shoulder buttons add to the roll and yaw. No deltatime here for a quick response
//comment out the .y parts if you don't want to turn when you hit them
if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
{
angVel.y -= 20;
angVel.z += 50;
speed -= 5 * Time.fixedDeltaTime;
}
if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
{
angVel.y += 20;
angVel.z -= 50;
speed -= 5 * Time.fixedDeltaTime;
}
//your angular velocity is higher when going slower, and vice versa. There probably exists a better function for this.
angVel /= 1 + deltaSpeed * .001f;
//this is what limits your angular velocity. Basically hard limits it at some value due to the square magnitude, you can
//tweak where that value is based on the coefficient
angVel -= angVel.normalized * angVel.sqrMagnitude * .08f * Time.fixedDeltaTime;
//and finally rotate.
transform.GetChild(1).Rotate(angVel * Time.fixedDeltaTime);
//this limits your rotation, as well as gradually realigns you. It's a little convoluted, but it's
//got the same square magnitude functionality as the angular velocity, plus a constant since x^2
//is very small when x is small. Also realigns faster based on speed. feel free to tweak
transform.GetChild(1).Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
//LINEAR DYNAMICS//
deltaSpeed = speed - cruiseSpeed;
//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
//brake and accelerate a lot faster. Could potentially do the same thing with the angular stuff.
decel = speed - minSpeed;
accel = maxSpeed - speed;
//simple accelerations
if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
speed += accel * Time.fixedDeltaTime;
else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
speed -= decel * Time.fixedDeltaTime;
//if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
//(added clamping since it's more of a gradual slowdown/speedup)
else if (Mathf.Abs(deltaSpeed) > .1f)
speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
//moves camera (make sure you're GetChild()ing the camera's index)
//I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
transform.GetChild(0).localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
float sqrOffset = transform.GetChild(1).localPosition.sqrMagnitude;
Vector3 offsetDir = transform.GetChild(1).localPosition.normalized;
//this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
//I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
transform.GetChild(1).Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
//(**************** this ***************) is what actually makes the whole ship move through the world!
transform.Translate((offsetDir * sqrOffset * 50 + transform.GetChild(1).forward * speed) * Time.fixedDeltaTime, Space.World);
//comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
}
void Update()
{
}
}
using UnityEngine;
using System.Collections;
public class PlayerShipController : MonoBehaviour
{
//speed stuff
float speed;
public int cruiseSpeed;
float deltaSpeed;//(speed - cruisespeed)
public int minSpeed;
public int maxSpeed;
float accel, decel;
public Transform TheChildIWant;
//turning stuff
Vector3 angVel;
Vector3 shipRot;
public int sensitivity;
public Vector3 cameraOffset; //I use (0,1,-3)
void Start()
{
speed = cruiseSpeed;
}
void FixedUpdate()
{
//ANGULAR DYNAMICS//
shipRot = transform.TheChildIWant.localEulerAngles; //make sure you're getting the right child (the ship). I don't know how they're numbered in general.
//since angles are only stored (0,360), convert to +- 180
if (shipRot.x > 180) shipRot.x -= 360;
if (shipRot.y > 180) shipRot.y -= 360;
if (shipRot.z > 180) shipRot.z -= 360;
//vertical stick adds to the pitch velocity
// (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
//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
float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
angVel.y += turn * .5f;
angVel.z -= turn * .5f;
//shoulder buttons add to the roll and yaw. No deltatime here for a quick response
//comment out the .y parts if you don't want to turn when you hit them
if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
{
angVel.y -= 20;
angVel.z += 50;
speed -= 5 * Time.fixedDeltaTime;
}
if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
{
angVel.y += 20;
angVel.z -= 50;
speed -= 5 * Time.fixedDeltaTime;
}
//your angular velocity is higher when going slower, and vice versa. There probably exists a better function for this.
angVel /= 1 + deltaSpeed * .001f;
//this is what limits your angular velocity. Basically hard limits it at some value due to the square magnitude, you can
//tweak where that value is based on the coefficient
angVel -= angVel.normalized * angVel.sqrMagnitude * .08f * Time.fixedDeltaTime;
//and finally rotate.
transform.TheChildIWant.Rotate(angVel * Time.fixedDeltaTime);
//this limits your rotation, as well as gradually realigns you. It's a little convoluted, but it's
//got the same square magnitude functionality as the angular velocity, plus a constant since x^2
//is very small when x is small. Also realigns faster based on speed. feel free to tweak
transform.TheChildIWant.Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
//LINEAR DYNAMICS//
deltaSpeed = speed - cruiseSpeed;
//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
//brake and accelerate a lot faster. Could potentially do the same thing with the angular stuff.
decel = speed - minSpeed;
accel = maxSpeed - speed;
//simple accelerations
if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
speed += accel * Time.fixedDeltaTime;
else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
speed -= decel * Time.fixedDeltaTime;
//if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
//(added clamping since it's more of a gradual slowdown/speedup)
else if (Mathf.Abs(deltaSpeed) > .1f)
speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
//moves camera (make sure you're GetChild()ing the camera's index)
//I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
transform.TheChildIWant.localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
float sqrOffset = transform.TheChildIWant.localPosition.sqrMagnitude;
Vector3 offsetDir = transform.TheChildIWant.localPosition.normalized;
//this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
//I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
transform.GetChild(1).Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
//(**************** this ***************) is what actually makes the whole ship move through the world!
transform.Translate((offsetDir * sqrOffset * 50 + transform.GetChild(1).forward * speed) * Time.fixedDeltaTime, Space.World);
//comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
}
void Update()
{
}
}
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?
It isn’t “transform.TheChildIWant.localPosition”. TheChildIWant is defined in your PlayerShipController class, not in Unity’s Transform class. Just use “TheChildIWant.localPosition”.
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