- Home /
My spaceship does not move forward when mouse is pressed down
To summarize what I want it to do: I am trying to code it so that when one button is pressed, the player's ship will start moving forward in the direction that it is facing. If a different button is pressed, the player's ship will start moving backward from the direction that it is facing.
I starting coding it with rigidbody2d, but it doesn't move forward or backwards when I press them down. Tried many times to fix it, to figure out where I am going wrong, but I cannot find out what code is being problematic. Here is the code right now:
public class SpaceShip : MonoBehaviour
{
public float acceleration;
private Rigidbody2D rb2d;
private Vector2 forward;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
forward = transform.position + transform.forward;
if (Input.GetMouseButton(0))
{
rb2d.AddForce(forward * acceleration);
}
else if (Input.GetMouseButton(1))
{
rb2d.AddForce(-forward * acceleration);
}
}
You dont need to add transform.position in forward = transform.position + transform.forward;
. Just have it set to transform.forward as the direction. This should work. Is your rigidbody kinematic or maybe static?
That's right. AddForce takes direction as its argument. Not point. But changing it to transform.forward won't work either since this is 2D game and OP declared "forward" as Vector2. It should be for example transform.up.
$$anonymous$$y rigidbody is dynamic, not kinematic nor static. I tried just setting the direction to transform.forward and even transform.up. Both times, the ship doesn't move. The thing is that I somehow got it to work using transform.forward but now it doesn't move at all.
Answer by CmdrZin · Jan 05, 2021 at 04:04 AM
Ran your script. Set acceleration to 100 in Editor.
Added a Debug.Log() that showed your forward calc was always 0.0. You also didn't specify the ForceMode2D mode. Don't know what the default is if you don't have it.
Debug.Log("LMB " + forward);
// rb2d.AddForce(forward * acceleration, ForceMode2D.Force);
rb2d.AddForce(Vector3.up, ForceMode2D.Force);
seemed to work for moving upwards.
Answer by arbellrm · Jan 05, 2021 at 08:22 AM
Do you set the acceleration value anywhere? If that's not set (or 0 for that matter), the AddForce function won't have anything to add.
When you debug, does it reach the AddForce line? If so, what value does the (forward * acceleration) have?
Answer by Delgoodie · Jan 05, 2021 at 01:32 PM
I think CmdrZin solved your problem but I can't help but notice you are replacing the transform.forward vector with a vector that is dependent on position. This means it will move differently based on where it is in the map and since that vector is not normalized it will move faster as it gets farther from 0, 0. I think you just want to use transform.forward instead of forward.
Your answer
Follow this Question
Related Questions
How to freeze all enemy objects at once 1 Answer
Unity 2d mobile movement 0 Answers
Player Bump on map border 0 Answers
Using direction and Rigidbody2D.AddForce() to move towards object. 1 Answer
How to stop spam jumping? 1 Answer