I’m trying to make a space ship thruster by using AddForce when clicking somewhere in space.
However the ship object refuses to move. I’ll provide with some code and maybe you can help me.
private Vector3 target;
public float thrust;
public float deceleration;
public Rigidbody2D rb;
// Use this for initialization
void Start () {
//set initial target to current position
target = transform.position;
rb = GetComponent<Rigidbody2D>();
thrust = 1000f;
deceleration = 1f;
rb.isKinematic = false;
}
void FixedUpdate () {
//Moves the player to the clicked position
if (Input.GetMouseButton(0))
{
//set new target to where player clicked on the screen
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target = new Vector3(target.x, target.y, 0);
//rb.AddForce(new Vector2(target.x, target.y) * thrust);
//rb.AddForce(target * thrust);
rb.AddForce(transform.forward * thrust);
Debug.Log("ADD FORCE!");
}
}
