- Home /
Make game object face player and at the same time follow mouse position
Hi!
I'm making this 2d platform game. The player is playing the game with help by rigidbody2d. There is no actual control over the player character. He is moving down the levels with help of colliders and rigidbody2d. So he is bouncing his way downwards.
I have an game idea to implement lifelines that the one who plays the game can use to rescue the player from death. But for one of my ideas I need a game object to follow mousepointer and always have a special side of the lifeline to face the player. Looks something like this at the moment:
http://i.imgur.com/NG6CdSa.png
So, I first tried to find a way to make the lifeline to follow the mouse position. But I only found examples for 3D games.
And second I though that the upper side of the marked object that have a thin blue line should always face the player so I will be able to rescue my player from different threats. Like big falls and such.
I failed on both these tasks :(
Hope that someone understand what I need and can help out! :)
Another thing. The moveable game object should not collide with game level elements, but should "collide" with the player so I can use it to rescue him with it, but not get stuck in game level elements.
Answer by robertbu · Mar 21, 2014 at 04:29 PM
Your camera will be a certain distance from the playing plane. Say that that distance is 10. You can position a world game object at the cursor (both orthographic and perspective cameras) with:
var pos = Input.mousePosition;
pos.z = 10;
transform.position = Camera.main.ScreenToWorldPoint(pos);
You can get the object to face another object by:
var dir = player.position - transform.position;
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Note the above code assumes the 'forward' of your sprite is on the right.
As for 'collide', you can use a trigger, or you can calculate a distance from the player, or you can use any of the Physics2D functions like OverlapPoint().
Answer by nazia08 · Mar 26, 2014 at 10:54 AM
The Game Maker game development suite from YoYo Games contains a wide variety of drag and drop functions for fast game design...
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Rotate the player to face where the mouse is pointing 3 Answers
Camera Rotate to mouse position -2 Answers
Rotate to face player, drunk issues 0 Answers
Following mouse 2 Answers