• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Rotating a sprite so that it faces the direction the player must travel to reach an object (pointer)

C

Chris Livermore

Guest
please delete.
 
Last edited by a moderator:

Slyddar

Member
Here's something you can use, which I just wrote in relation to someone else's question a few mins ago. May as well recycle it :)
Now it depends on how you want to do it, but I had this on an object that moved itself to do the testing. Also depends on how you are getting the camera dimensions, but you can probably adjust this to your needs.

o_target_arrow object DRAW event
Code:
//get camera position
global.cw = camera_get_view_width(o_camera.camera);
global.ch = camera_get_view_height(o_camera.camera);
global.cx = camera_get_view_x(o_camera.camera);
global.cy = camera_get_view_y(o_camera.camera);

if instance_exists(o_target) and !on_screen(o_target, 10) {
    //set obj to middle of view
    x = cx + cw/2;
    y = cy + ch/2;
 
    //get angle to object
    var angle_to_obj = point_direction(x ,y , o_target.x, o_target.y);
 
    //start at the furthest point we can, which is half the view height
    x = x + lengthdir_x(ch/2, angle_to_obj);
    y = y + lengthdir_y(ch/2, angle_to_obj);
  
    //move until we are no longer on screen
    while on_screen(id, 10) {  
        x = x + lengthdir_x(1, angle_to_obj);
        y = y + lengthdir_y(1, angle_to_obj);
    }
 
    //draw arrow
    draw_sprite_ext(o_arrow, 0, x, y, 1, 1, angle_to_obj + 90, -1, 1);
}
on_screen script
Code:
/// @desc                on_screen(obj, buffer);
/// @arg                real    object to test
/// @arg                real    buffer we accept as on screen, + means more in screen is accepted

/// Returns true if the object passed is in the view/on screen

var _obj = argument0;    //object
var _b = argument1;        //buffer

if ((_obj.x + _b < global.cx + global.cw and _obj.x - _b > global.cx) and
(_obj.y + _b < global.cy + global.ch and _obj.y - _b > global.cy)) return true else return false;
I get those global camera position each step in the o_camera object, but added them here in case you don't do that.
Hope it helps.
 
C

Chris Livermore

Guest
ah, I am drawing directly to the GUI layer. I posted my code in the other thread, Thus asked for the thread to be deleted.

thanks though :)
 
Top