moshe ralf moshe ralf - 10 days ago 5
C# Question

How can i display the acceleration value of my spaceship?

While the game is running and i press none stop on the key p i see the spaceship moving faster and faster. But the value of the variable thrust is all the 5 never change. Then what value show and change that make the acceleration ? And what the thrust do ? If i change the thrust from 5 to 10 ?

I added to my spaceship a Rigidbody component.

The way i'm doing it is the right way to add acceleration to my spaceship ?

I want to display in the OnGUI the value of the acceleration each time i press the p key.

using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour {

public int rotationSpeed = 75;
public int movementspeed = 10;
private int thrust = 5;

Rigidbody _rigidbody;

void Start () {

_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}

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);
}
}

void OnGUI()
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + thrust);
}
}


Update

My script now after changes. When i press the p key the spaceship is gone fast and the value of the acceleration is 0 all the time in the OnGUI:

using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour {

public int rotationSpeed = 75;
public int movementspeed = 10;
private int thrust = 5;

bool isPKeyDown = false;
float acceleration = .0f;

Vector3 previousPosition = Vector3.zero;

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);
float acceleration = distance / Mathf.Pow(Time.deltaTime, 2);

previousPosition = transform.position;
_rigidbody.AddRelativeForce(0f, 0f, acceleration, ForceMode.Acceleration);
}
}

void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}

Answer

Starting from the beginnig, thrust is the value of the actual force ( a push force ) for your starship. So the bigger the value the stronger it "pushes" your starship.

If your script works then yes it is a good way of adding acceleration ( that depends on what you think )

To be able to show acceleration value when user pushes "P" key you should calculate the acceleration value ( how to calculate, or just use @Dan Wilson answer/comment ).

Then you should modify your Update method:

public void Update(){
    isPKeyDown = Input.GetKey("p");
    // ... rest of your code
} 

update your class members:

bool isPKeyDown = false;
float acceleration = .0f;

update your OnGUI method:

public void OnGUI(){
    if ( isPKeyDown ) {
        GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
    }
}

and to calculate your acceleration and playing with rigidbody i would recommend using fixed update:

public void FixedUpdate(){
    // calculate acceleration here...
    acceleration = ... ;
    if ( isPKeyDown ) {
        _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
    }
}

EDIT: Easiest way of finding out the acceleration:

// as a member field
Vector3 previousPosition = Vector3.Zero;

// in update
float distance = Vector3.Distance(previousPosition, transform.Position);
float acceleration = distance / Mathf.Pow(Time.deltaTime, 2);

previousPosition = transform.Position; 

EDIT2: updated code on pastebin

Comments