- Home /
The spaceship acceleration script is good ? And how to use it with engine ?
In the script i click on the key Z to rotate the spaceship and on P to accelerate.
In the spaceship there is a Rigidbody component both Use Gravity and Is Kinematic are set to false turned off.
I wonder if the acceleration part is real enough for spaceship ? I also can't figure out how to make that when i keep clicking on P it will increase the acceleration ? Do i need to use the Editor: Edit > Project Settings > Input for controlling the spaceship or i can do it with keys like i'm doing now using Z and P ?
Whats is the acceleration and thrust variables does ? I mean what each one do ? they are not the same i guess.
And i have an engine object i attach it as child under the spaceship and position it in the back of the spaceship. How can i combine the engine with the script so when i click/press the P key it will also increase/decrease the engine power ?
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour
{
public int rotationSpeed = 75;
public int movementspeed = 10;
public int thrust = 10;
private bool isPKeyDown = false;
private float acceleration = .0f;
private Vector3 previousPosition = Vector3.zero;
private 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"))
{
isPKeyDown = Input.GetKey("p");
float distance = Vector3.Distance(previousPosition, transform.position);
acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
previousPosition = transform.position;
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
}
}
void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}
This is a screenshot of the Engine after i added it to the back of the spaceship and on the right the Engine properties in the Inspector.
Your answer
Follow this Question
Related Questions
Attach to other coordinat SpringJoint2D from code. 0 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers