# When i give acceleration to my spaceship she will keep moving to the direction is it logic ?

In the script that attached to my spaceship with the keys wsad i can change the ship rotation and then the ship will move forward to the direction it’s pointing.

Then i added acceleration to my script when i click the p key it will keep accelerate the ship. But now if the ship if pointing a direction if i rotate it with wsad after pressing p it will not move to that direction it’s pointing i need to give it a “push” to press the p key again so it will move to the direction it’s pointing.

My question is if this behaviour is logic and right or i should change something in the script so even if pressed the p key the ship will keep moving to the pointing direction ? The result now is that the ship might be pointing up but she will move down or left and if i will press the p key then it will move up.

How can i change it so even if i press the p key it will keep moving to the direction it’s pointing also if i rotate the ship ?

The ship is moving forward all the time on speed 10.
And in this part i rotate the ship and the ship will move forward to the rotated point it’s facing:

``````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;
``````

But the part with the if (Input.GetKey(“p”)) make the problem.

``````using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour {

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

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

previousPosition = transform.position;
}
}

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

You’re manually moving the position and adding a physics force. Normally I’d think you want to do one or the other.

I would say either calculate velocity manually and directly change the position or only use the physics engine.

If you want to use the physics engine and want to have exact speeds, look up how to use PID controllers. Often the exact movement speed isn’t that important though and you can just adjust the drag and forces to get the behavior you want.

I’m a little confused about what it is you’re trying to achieve, think it’s getting lost in translation a little… but you really shouldn’t be mixing direct transform.position updates with rigidbody forces. It’ll get some really odd behaviour at the best of times…

edit: lol jinx

What i want to do is to keep the moving part and the rotation/s.
And i wanted to add another key press that will give some kind of boost to the speed of the ship.
For example when i’m running the game now it’s on static 10 speed. I want when i press the p key it will give some boost to the speed maybe for a limited time like i press on the car gas with the leg for short time make it move fast then when i leave the gas(p key) it will slowly go back to it’s regular speed 10 in this case.

So i thought to add acceleration effect. So when i press the p key none stop it will keep raising the speed of the ship if i leave the p key it will go slowly back to it’s original speed 10. And again in time i press the p none stop it will raise the speed.

speed i mean the movementspeed variable. So maybe this acceleration part i’m using with the p key is wrong.
What i want to is to make like the gas pedal effect in a car.

I mean exhaust effect.
so i got the effect it self from here: