# playMaker

### Author Topic: Relative position between two game Objects.  (Read 2693 times)

#### sebaslive

• Hero Member
• Posts: 557
##### Relative position between two game Objects.
« on: May 01, 2013, 05:28:18 PM »
Hi all, anyone know how I can get position of my player in local space compared to the enemy?

So if I am on the left of the enemy it would be negative x but if I were to the right of the enemy it would be positive x.

It's so that I can rotate the enemy to look at the player on X and if I hit the enemy it would know that I hit it from that direction and addForce to the opposite direction. smooth look at doesn't work very well when it comes to 2D...
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5

#### Lane

• Hero Member
• Posts: 2488
• Yup.
##### Re: Relative position between two game Objects.
« Reply #1 on: May 01, 2013, 06:03:19 PM »

If you use real add-force to fire the projectile and have a rigidbody on the enemy then its already implied. Otherwise as I understand it the normal thing to do is store the collision point and apply force there in some manner or applying force in the opposite direction of the collision point. Basically use the collision to save the location, figure where it is in relation to the impacted object then use that data to apply a force where you want it.

If you calculated it between the player and the target then you would get inconsistent results from play movement, projectile speed and so on.

If  only left/right matters then I would first try still use the collision point for that and figure if it is greater or less than 0 and go from there.
« Last Edit: May 01, 2013, 06:05:11 PM by Lane »

#### sebaslive

• Hero Member
• Posts: 557
##### Re: Relative position between two game Objects.
« Reply #2 on: May 01, 2013, 10:13:20 PM »
How do I get the info to know which side the collision is on (action?) I tried to find any actions on collision points but no luck or definitely missing something.

Or is there just an action that is like (get distance) but with local space, positive and negative?

Edit: any advice preferably on the second one as to not rely on impact since I believe I can use it more from distances etc.
« Last Edit: May 01, 2013, 10:19:42 PM by sebaslive »
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5

#### izzycoding

• Playmaker Newbie
• Posts: 29
##### Re: Relative position between two game Objects.
« Reply #3 on: May 02, 2013, 03:35:37 AM »
Hi,

I did something similar a few weeks ago where I did a check to see if an object was in front or behind my player. If you get the position in world points of both game objects and then subtract the enemy from the player you get the relative difference as a Vector3. I might have a custom action that I wrote for it if that is any help? Will have to post the code when I get home later though.

Also to make the enemy rotate to face the player there is an action called "Look At" which is under Transform in the Action Browser.

Hope this helps.

#### sebaslive

• Hero Member
• Posts: 557
##### Re: Relative position between two game Objects.
« Reply #4 on: May 02, 2013, 09:35:03 AM »
The more actions the merrier  I got the vector3 to work just like you said as well, thanks to both for your replies. As for the "look at" I've never been able to make it work on a 2D plane.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5

#### izzycoding

• Playmaker Newbie
• Posts: 29
##### Re: Relative position between two game Objects.
« Reply #5 on: May 19, 2013, 09:00:42 AM »
Hey,

I have searched everywhere for the actions I wrote before and cant find them, but glad that you got the vector thing working.
If your using a 2D plane then the look at probably would not work as I believe it uses the Vector3.Forward in order to rotate the object (this would effectively make your plane disappear as its forward for this would be on the wrong axis.
I saw somewhere on a website an FBX Plane that had been exported from 3dsMax with different axis settings. Something like that might help?

Regards,

#### Satrio

• Junior Playmaker
• Posts: 67
##### Re: Relative position between two game Objects.
« Reply #6 on: May 19, 2013, 12:42:26 PM »
Or you can use a 3D cube as the logic part of the spaceship and make that look at the player, then parent the 2D plane to the 3D cube. Having en empty as a parent to the 2D plane allows you to rotate and compensate as an extra layer of control.

3dcube--
"empty"
--"2Dplane

« Last Edit: May 19, 2013, 12:43:59 PM by Satrio »