Hi, how is everyone?
I have been working on this game project for a school studio project (and possibly it might end up being more if I could get this one thing working correctly). The idea is to be a arcade space racing game, subsequently, I want to have physics that are quick (like the ability to turn yaw-wise in a quick fashion).
The issue I keep running into is how to pull this off? I have the script working, I have barrel rolls, strafing, but I am using rigidbody.AddRelativeForce as the means of propulsion/movement. This due to the fact that I couldn’t find anyway outside of using Unity’s physics system for this. I tried transform.Translate, but that would only propel the ship in one direction, along the z-axis, and not the direction the ship is pointing in. So I am also open to figuring out a non-Unity physics-based method to get this working (if anyone knows, otherwise I’ll stick to using rigidbody physics) as the physics system seems laggy/lazy to me turning-wise.
So can anyone help me? Here is my script for just the ship’s basic controls.
Thanks for any help.
using UnityEngine;
using System.Collections;
public class PlayerShip_Easy : MonoBehaviour
{
private float shipSpd = 300.0f;
private float turnSpd = 150.0f;
private float rotSpd = 50.0f;
private float strafeSpd = 50.0f;
float rollRate = 0.0f;
float pitchRate = 0.0f;
float yawRate = 0.0f;
float invert = -1.0f;
void Update ()
{
float roll = Input.GetAxis("Roll");
float pitch = Input.GetAxis("Pitch");
float yaw = Input.GetAxis("Yaw");
float strafe = Input.GetAxis("Strafe");
if (Input.GetAxis("Pitch") > 0)
{
pitchRate -= pitch * invert * turnSpd * Time.deltaTime;
pitchRate = Mathf.Clamp(pitchRate, -45.0f, 45.0f);
transform.rotation = Quaternion.Euler(pitchRate, yawRate , 0);
}
else if (Input.GetAxis("Pitch") < 0)
{
pitchRate += pitch * turnSpd * Time.deltaTime;
pitchRate = Mathf.Clamp(pitchRate, -45.0f, 45.0f);
transform.rotation = Quaternion.Euler(pitchRate, yawRate , 0);
}
if (Input.GetAxis("Yaw") > 0)
{
yawRate += yaw * turnSpd * Time.deltaTime;
yawRate = Mathf.Clamp(yawRate, -60.0f, 60.0f);
transform.rotation = Quaternion.Euler(pitchRate, yawRate, 0);
}
else if (Input.GetAxis("Yaw") < 0)
{
yawRate += yaw * turnSpd * Time.deltaTime;
yawRate = Mathf.Clamp(yawRate, -60.0f, 60.0f);
transform.rotation = Quaternion.Euler(pitchRate, yawRate, 0);
}
rigidbody.AddRelativeForce(pitch, yaw, shipSpd);
}