Hey guys.
I finally decided to seriously pick up Unity recently and now I’m watching a ton of tutorials and making tests with all sorts of codes for a space game, with an almost top-down perspective. (first time I ever dabbled in C# codes)
Looks like I hit a wall regarding controlling the movement of the player’s spaceship.
My current code so far does this:
- it rotates the player ship to always face the direction of the mouse using Raycast
- it moves the player ship in relation to the mouse direction, so “forward” will always be towards the mouse
What additions I need: - tilting/banking the ship in the direction it’s going
- the ship accelerates to max speed in an instant, I want to make the acceleration/deceleration smoother
Been trying for a while now to figure it out, by making a Frankenstein monster from various tutorial code parts, but nothing works properly, so I’m down to your mercy.
This is my code:
public float moveSpeedFront;
public float moveSpeedBack;
public float moveSpeedSides;
void FixedUpdate ()
{
PlayerLooksAtMouse ();
if (Input.GetKey (KeyCode.W))
transform.Translate (Vector3.forward * moveSpeedFront * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
transform.Translate (-Vector3.forward * moveSpeedBack * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
transform.Translate (Vector3.left * moveSpeedSides * Time.deltaTime);
if (Input.GetKey (KeyCode.D))
transform.Translate (-Vector3.left * moveSpeedSides * Time.deltaTime);
}
void PlayerLooksAtMouse () // this is so the ship always faces/rotates in the direction of the mouse pointer
{
Vector3 mousePosition = Vector3.zero;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
mousePosition = new Vector3 (hit.point.x, transform.position.y, hit.point.z);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (mousePosition - transform.position), 1f * Time.deltaTime);
}
}
Now I found an official tutorial with a spaceship that tilts left/right, but I just can’t figure out how to merge that code with mine to make my ship tilt. while also preserving my current control scheme.
This is the example tutorial code:
[System.Serializable]
public class Done_Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class Done_PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Done_Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}
Thank you for your time.