Hey Folks,
I’ve gone a little bit further and implemented another spaceship with a different movement (and slightly different controls, TBC):
You can see the models close-up in the original thread:
https://community.gamedev.tv/t/detailed-space-ship-for-project-space/
Interested in the modifiedCode?
PlayerController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
enum SHIPTYPE {
Vertical,
Speedster
}
public class PlayerController : MonoBehaviour {
// config parameters
[SerializeField] private SHIPTYPE shiptype;
[SerializeField] [Range(1, 100)] private float thrustSpeed;
[SerializeField] [Range(1, 10)] private float thrustBonusMultiplier;
[SerializeField] [Range(1, 100)] private float rotationSpeed;
// cached references
private Rigidbody rigidBody;
private AudioSource audioSource;
// Start is called before the first frame update
void Start() {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
HandleThrusting();
HandleRotation();
}
private void HandleThrusting() {
// The user shall be able to thrust while moving
if (Input.GetKey(KeyCode.Space)) {
Vector3 direction = new Vector3(0, 0, 0);
float tmpThrustBonusMultiplier = thrustBonusMultiplier;
switch (shiptype) {
case SHIPTYPE.Vertical:
direction = Vector3.up;
break;
case SHIPTYPE.Speedster:
if (Input.GetKey(KeyCode.LeftShift)) {
direction = Vector3.up;
tmpThrustBonusMultiplier = 1f;
}
else {
direction = Vector3.right;
}
break;
default:
throw new UnassignedReferenceException();
break;
}
rigidBody.AddRelativeForce(direction * thrustSpeed * tmpThrustBonusMultiplier * Time.deltaTime);
if (!audioSource.isPlaying)
audioSource.Play();
}
else {
audioSource.Stop();
}
}
private void HandleRotation() {
// Take Manual Control of rotation
rigidBody.freezeRotation = true;
// The user shan't be able to rotate left and right simultaneously
if (!(Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))) {
Vector3 direction = new Vector3(0, 0, 0);
if (Input.GetKey(KeyCode.A)) {
switch (shiptype) {
default:
direction = Vector3.forward;
break;
}
}
else if (Input.GetKey(KeyCode.D)) {
switch (shiptype) {
default:
direction = Vector3.back;
break;
}
}
transform.Rotate(direction * rotationSpeed * Time.deltaTime);
}
// Release rotation of the rocket
rigidBody.freezeRotation = false;
}
}
Have a nice day!
Nefasu