Now what i can do is rotate the spaceship directions using wasd so i can change the spaceship direction it will move right,left,up,down but now i want to add more two options:
Instead that the ship will move in static speed the movementspeed variable i want to use some key not sure wich one and how to make acceleration. For example when running the game the speed will start with 10 and when i press the key none stop it will start accelerate the speed more and more. And how do i add if it’s needed a addforce ?
And also another key/s to rotate the ship not only for directions wsad but also to rotate the ship it self for example the Z axis.
using UnityEngine;
using System.Collections;
public class ControlShip : MonoBehaviour {
public int rotationSpeed = 75;
public int movementspeed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
transform.position += transform.forward * Time.deltaTime * movementspeed;
}
}
This two lines i added to the Update function solved the Z axis rotation
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
Now i need to solve how to add/make the acceleration.
This is a working code with the acceleration.
But now i’m facing another problem. I want to show in real time the thrust speed when i press the p key and i’m not clicking the p key i’m pressing it none stop. And for some reason when i’m adding the line of the Debug.Log it’s making everything slow or almost freeze when i press the p key. Why the debug.log make it ?
Not the line in the Start function but the Debug.Log in the Update function inside the p press key:
In this part the Debug.Log make the problem:
if (Input.GetKey("p"))
{
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
Debug.Log("Acc Speed: " + thrust);
}
And the script:
using UnityEngine;
using System.Collections;
public class ControlShip : MonoBehaviour {
public int rotationSpeed = 75;
public int movementspeed = 10;
private int thrust = 5;
Rigidbody _rigidbody;
// Use this for initialization
void Start () {
_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}
// Update is called once per frame
void Update () {
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
transform.position += transform.forward * Time.deltaTime * movementspeed;
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (Input.GetKey("p"))
{
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
Debug.Log("Acc Speed: " + thrust);
}
}
}
Instead Debug.Log in the Update function i added a OnGUI function and did:
void OnGUI()
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + thrust);
}
But now the thrust value never change in the OnGUI function. Why ?