Hi,
I am making a space(ish) game that uses FPS type controls instead of normal spaceship controls (to see if it is any good). And I’m using the rigidbody.MovePosition() which is al working exactly how I want it but it is moving the rigid body relative to world space. I have looked up google for this issue and they are all saying that I need to use transform.forward. Fair enough. However I have tried to put it in just about all the position of my code and when I run it the ship goes forward relative to it (yay) regardless of any input (boo). The only way I can see this working is if I break up the forward, right, and up directions into seperate functions or seperate line instead of what I already have which is nice and concise. My question is: Is there any way for me to use the transform.forward without having to rewrite the move function with less elegant code?
Below is the code I have - This is the entire script that goes onto the object with the rigidbody:
using UnityEngine;
public class ShipController : MonoBehaviour
{
private Rigidbody rb;
public float moveSpeed;
public float rotateSpeed;
// Start is called before the first frame update
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Turn();
Move();
}
private void Move()
{
float thrust = Input.GetAxis("Thrust");
float strafe = Input.GetAxis("Strafe");
float elevate = Input.GetAxis("Elevation");
Vector3 tempVector = new Vector3(strafe, elevate, thrust);
tempVector = tempVector.normalized * moveSpeed * Time.fixedDeltaTime;
rb.MovePosition(transform.position + tempVector);
}
private void Turn()
{
float pitch = Input.GetAxis("Mouse Y");
float yaw = Input.GetAxis("Mouse X");
float roll = Input.GetAxis("Roll");
Vector3 tempVector = new Vector3(-pitch, yaw, roll);
Quaternion tempRotation = Quaternion.Euler(tempVector * rotateSpeed * Time.fixedDeltaTime);
rb.MoveRotation(rb.rotation * tempRotation);
}
}
Thank you so much!
-Scobbo