I’m trying to get a Camera to move opposite of the direction a RocketShip moves when I transform the position of the RocketShip up (along green axis) for every frame the “w” button is pressed down. When the “w” button is not pressed, I need the camera to follow the same direction as the RocketShip, but still stay the same distance away from the RocketShip.
When “w” is pressed I have the RocketShip thrusting up:
public class PlayerController : MonoBehaviour {
void FixedUpdate () {
ThrustRocketShip ();
}
void ThrustRocketShip () {
if (Input.GetKey ("w")) {
rb.AddForce (transform.up * thrust);
if (thrust < 10f) {
thrust += 0.8f;
}
} else {
thrust = 0;
}
}
}
I can’t figure out how to get the camera’s transform to move in the opposite direction. The closest I’ve gotten is by trying to modify the transform of the camera when “w” is pressed. I just don’t know how to modify the camera’s position by the inverse of the RocketShip’s up:
public class CameraFollow : MonoBehaviour {
public Transform rocketShip;
public Vector3 offset = -10;
void FixedUpdate () {
if (Input.GetKey ("w")) {
//1st attempt, I know this doesn't work
/*
Vector3 temp = rocketShip.up;
temp.y += 2f;
transform.position = temp;
*/
//2nd attempt, I also know this won't work
/*
transform.position = rocketShip.up * -1f;
*/
}
}
This is, by far, the most challenging mechanic I’ve ever tried to implement. And this point I’m losing hope, yet unwilling to give up. If you need anymore context or clarification please let me know.