playMaker

Author Topic: Force in Direction  (Read 4553 times)

Lamb

  • Playmaker Newbie
  • *
  • Posts: 8
Force in Direction
« on: April 16, 2016, 02:37:33 AM »
I have a 2d game/object/Rigidbody2D that I'm controlling with arrow keys and Add Torque to rotate. I also want to have it move forward, I'm guessing using Add Force, in the direction it's facing after rotating it around a bit.

How would I go about keeping track of what angle/direction is "forward" for it and have it add the force for movement in that direction?

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Force in Direction
« Reply #1 on: April 16, 2016, 03:25:30 AM »
Hi and welcome to the forum!

Is your unity setup for a 2d project? (when you start unity chose the option to make a 2d project)

Also is your entire project in 2d, or just this sprite? (if it is just this sprite then you may have to add it into a gui or something Im not sure, but Ill proceed as if youre making a whole 2d project, below.)

Moving forward for a 2d sprite should be the x axis I believe (or z if i am wrong). So a positive number on the x axis would move right and a negative number would move left.

There is a 2d action pack, which can be found on the ecosystem or here: https://hutonggames.fogbugz.com/?W1150
(there is also  vector2 action pack out there, you can search for it, if you can see it being useful for your 2d project).

Download it and then import it, it will give you an action called 'add force 2d' (or set velocity 2d),  which will work with controlling a 2d rigidbody.

Also check out this tutorial that does a pretty good job of introducing 2d basic features that can be directly used in your game:
Good luck with it and I hope it helps. Let us know how it goes or if Im totally off topic or something lol.
« Last Edit: April 16, 2016, 03:31:00 AM by Zeldag »

Lamb

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Force in Direction
« Reply #2 on: April 16, 2016, 01:47:34 PM »
The whole project is set for 2D and I have both of the packages you mentioned.

My game is a top-down spaceship game [rotating the ship on an axis using Add Torque, keeping w/ the using rigidbody2d route] so forward depends on direction the ship has rotated but it doesn't seem like you can just add force to a "direction" so much as x or y axis.

So I guess it's more like how can I feed the game data telling it the angle my ship is facing [ in 2d so it's the degree of the Z axis right? ] and then apply force forward?

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Force in Direction
« Reply #3 on: April 16, 2016, 03:33:55 PM »
Okay, I see your issue, sorry I did not understand it initially.

Im sure there is a cleaner and more elegant way to do it, but I have an idea that may work for you:

Add a circle child object to the spaceship and move it away in front of the spaceship. Then disable the sprite renderer.

Make another independant circle game object that always 'moves towards' the child game object (every frame). Then disable the sprite renderer.

When you press the forward arrow, make the spaceship 'move towards' the second empty game object.

I hope that makes sense and works. Good luck with it. You could probably take this further and use get position and other things to make add force work for you, but I believe thisbshould generally work.

Lamb

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Force in Direction
« Reply #4 on: April 16, 2016, 05:31:55 PM »
What a work-around but that totally got me what I wanted.
Thanks a lot!

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Force in Direction
« Reply #5 on: April 16, 2016, 05:57:41 PM »
Lol, no problem. As a noob I often find long work arounds to get things working due to my clumsiness lol.

Really glad it worked for you!

Lamb

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Force in Direction
« Reply #6 on: April 16, 2016, 08:56:45 PM »
It'd be cool if someone knew a way to use addForce or apply physics to though since movement doesn't seem to allow for inertia this way.

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Force in Direction
« Reply #7 on: April 16, 2016, 09:19:56 PM »
Well I will say Im a noob and have had more issues with velocity and adding force than I can clearly remember lol (also havnt used 2d in a while), but maybe this would work:

Get position of the second circle you created every frame into seperate float variables (x and y axis).

Then use add force 2d (or maybe set velocity) to add force in these two variables when you press forward.

As I mentioned I may be totally misunderstanding the add force (and set velocity) action here, especially as ive never recreated the exact scenario, but my imagination says it could be a way to do it. Maybe something to experiment with- if you have time?
« Last Edit: April 16, 2016, 09:22:14 PM by Zeldag »