Help modifying code for a spaceship,How do I change the camera following settings on this script?
So I'm really new to Unity, Coding and game design in general so any and all help would definitely be greatly appreciated.
I'm using this Code/Tutorial (Posted Below) to control my Spaceship in my Space game I'm trying to make. I copied the code, created a script called "PlayerControlls", added it to an empy game object that had my Spaceship as a child of the empty game object and added in my camera under the empty game object along side the spaceship. The code works great!!! It's the only code I've gotten to work out of 20+ codes I've tried so big thanks to the maker of this code!!!
My problem is that the camera follows the spaceship but on what appears to be an "advanced delay"???? Essentially, when I turn, move, rotate whatever, the camera reacts instantly and the ship is slower than the camera obviously but the ship goes out of the cameras view while this is happening and then comes back into view when the action is complete.
What I'm trying to figure out, and have been for several days now, is how do I change the code so the camera follows the ship directly and not react faster than the ship so that the ship is always in view of the camera....????
If you need a video of what I'm talking about I can take a screen capture video and upload it.
The website and code are below-------------------------------------------------------------------------------------------------------https://www.reddit.com/r/gamedev/comments/176w63/my_3rd_person_spaceship_controls_in_unity_ill/
using UnityEngine; using System.Collections;
public class PlayerController : 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 * .00f);
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 * 22 * 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()
{
}
}
Your answer
Follow this Question
Related Questions
Spaceship Script not working? 0 Answers
[Solved] I am trying to build a space sim/ shooter type game. 1 Answer
VR Cockpit 0 Answers
Camera following player, how do get it to stop after the player falls? 0 Answers
BoatRiderController? 1 Answer