playMaker

Author Topic: Move object in direction of touch[SOLVED]  (Read 489 times)

Chicky

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Move object in direction of touch[SOLVED]
« on: April 12, 2018, 12:14:11 PM »
Hi there,

I've been fumbling my way through this for a while. I'm making a 2D side scrolling flying 'shoot em up'. It's setup like an endless runner with the camera's x axis locked and objects moving across the screen behind the player.

I've been working on getting a machine gun function for the ship, controlled by touch input on the right hand side of the screen. The idea is that you touch and hold anywhere on the right of the screen and the turret will shoot bullets in that direction.

It's working, but I feel I'm going about this wrong.

I'm using a touch event and storing the hit point (Vector2)

Using 'Get Vector2 XY' then 'Set Vector3 XYZ' with the Vector2's x and y and z as 0. Storing this in a global variable called HitPointVector3

Then using 'Move Towards' to move the bullet.

Problem I was having here was the bullet was moving to the hitpoint then stopping.

So I used 'Vector3 Multiply' on my HitPointVector3 to increase the distance so the bullet goes off screen.

Like I said, this works but I now have the issue when I move the ship upwards (y axis) the bullets aim above where the hitpoint is. The more I move the ship up the further up the bullets aim.

I hope this makes sense, I tried using add force instead of move towards (which I feel could be the solution) but I couldn't figure out how to make the bullet aim towards my touch input.

Any help would be greatly appreciated!



« Last Edit: April 13, 2018, 08:32:27 AM by jeanfabre »

Chicky

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Move object in direction of touch
« Reply #1 on: April 13, 2018, 06:29:35 AM »
Well, it only took about four hours of redesigning and reworking my FSM's over and over but I figured it out myself in the end.

I'll leave this here in case anyone else has the same issue as there is very little information about how the Touch Object 2D Event works when storing it's Hit Point.

The object I wanted to touch is a child of my camera, it works like a GUI button detecting when the player has clicked or dragged touch input across it. For some reason the 'Store Hit Point' part of the Touch Object 2D Event was also storing the Y transform data of the camera. So it was detecting where I touched and then adding the difference in Y (which was a negative value) when the ship was moved upwards. This meant that the further upwards I moved the ship, the more the gun would aim upwards, offset from where the touch point was.


I fixed this by storing the player X and Y values as global variables in my touch controller game object (the object where I detect touch input), then in my bullet's movement FSM I would set a local Vector2 using the global player X and Y, then subtract that from my Vector2 for the touch hit point.

So yeah, tldr: subtract the player position before moving the bullet to the touch hit point.

From what I can tell, the touch object script needs the option to set it's hit point in screen space, not world space. All of the 'world to screen space' functions I tried didn't achieve this.

Extra note:

Because it's a machine gun I also had to set global variables within my touch controller FSM, then create local variables from these globals within my bullet's movement FSM. That way each bullet acts independently with it's own target based on the touch point when it was spawned.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14458
  • Official Playmaker Support
    • View Profile
Re: Move object in direction of touch[SOLVED]
« Reply #2 on: April 13, 2018, 08:32:51 AM »
Hi,

 thanks for sharing back your answer :)

 Bye,

 Jean