GameMaker How to make object turn in certain direction

M

Medievalwarfare

Guest
I have a rectangle that I can move by right clicking on the screen and it will move to that point. I want to make a code to where I can designate which side of the rectangle is the “front,” and before it starts moving towards the point, I want it to rotate until it’s front is at the same angle as the chosen point. How do I do this?
 
Ok, for one thing, always remember that objects are a blueprint and instances are the thing that actually exist inside the game (just a note as this is the most common source of problems for beginners). Now how have you coded the movement? Does the rectangle instance have a sprite, or are you drawing it using draw_rectangle? We'll need some actual code so we know how your specific situation is set up.
 
M

Medievalwarfare

Guest
the rectangle is an actual object with a sprite that is put in the instances layer. It moves by having a separate object spawn with a right click of the mouse button that saves the location of the mouse. Then, when the original object detects that this location exists and the object has been left-clicked to toggle a variable (moveTo) to set it to true, the object moves to that location. I've managed to figure out how to make the object rotate (Step lines 8-10) but I want the object to stay facing that new angle.

Code:
//CREATE
//Variables for movement
moveTo = false;

//Variables for rotating
rspeed = 5;
initialR = point_direction(x,y,x,y);

//STEP
//Moves army if right mouse is clicked and the target point exists
if (moveTo = true && instance_exists(objTargetLocation)){  
   
    //variables
    mouseDistance = point_distance(x,y,objTargetLocation.x,objTargetLocation.y);  
    mouseDirection = point_direction(x,y,objTargetLocation.x,objTargetLocation.y) - 90;
   
    image_angle = mouseDirection;
    move_towards_point(objTargetLocation.x,objTargetLocation.y, min (4, mouseDistance)); //make speed depend on fatigue
} else {
    speed = 0;}

//GLOBAL RIGHT CLICK
if (!instance_exists(objTargetLocation)){
instance_create_layer(mouse_x,mouse_y,"Instances",objTargetLocation);
}

//LEFT CLICK
if (moveTo = false){
    moveTo = true;
}

if (moveTo = true && keyboard_check(vk_shift)){
    moveTo = false
    instance_destroy(objTargetLocation);
}
 
Stop saying object, lol, that was the point of my first line. It's an instance of an object. Learning the difference is very important. What do you mean by stay facing the new angle? The code you have given should do exactly that. You must be changing image_angle somewhere else or when the rectangle reaches the location, it bounces back and forth because it doesn't seem as though you have a limit on how close it can get which would make the rectangle rotate back and forth. Is that what is happening?
 
M

Medievalwarfare

Guest
Stop saying object, lol, that was the point of my first line. It's an instance of an object. Learning the difference is very important. What do you mean by stay facing the new angle? The code you have given should do exactly that. You must be changing image_angle somewhere else or when the rectangle reaches the location, it bounces back and forth because it doesn't seem as though you have a limit on how close it can get which would make the rectangle rotate back and forth. Is that what is happening?
when the instance reaches its target location, it snaps to a rotation of 90 degrees from the original angle it was placed at. When I continue to click to different locations from that point, it will rotate when it's moving but return to that same 90 degrees rotation
 
Code:
//STEP
//Moves army if right mouse is clicked and the target point exists
if (moveTo = true && instance_exists(objTargetLocation)){ 
   
   //variables
   mouseDistance = point_distance(x,y,objTargetLocation.x,objTargetLocation.y); 
   
   image_angle = mouseDirection;
   move_towards_point(objTargetLocation.x,objTargetLocation.y, min (4, mouseDistance)); //make speed depend on fatigue
} else {
   speed = 0;}

//GLOBAL RIGHT CLICK
if (!instance_exists(objTargetLocation)){
instance_create_layer(mouse_x,mouse_y,"Instances",objTargetLocation);
}

//LEFT CLICK
if (moveTo = false){
   moveTo = true;
   mouseDirection = point_direction(x,y,objTargetLocation.x,objTargetLocation.y) - 90;
}
That should fix it unless you are setting image_angle elsewhere.
 
M

Medievalwarfare

Guest
This didn't work. The only location image_angle is used is in the step event I've shown you.
Here's the code for objTargetLocation because I think you need it:
Code:
//GLOBAL RIGHT PRESSED
x = mouse_x;
y = mouse_y;

//DESTROY
x = x;
y = y;
 
M

Medievalwarfare

Guest
Code:
//STEP
//Moves army if right mouse is clicked and the target point exists
if (moveTo = true && instance_exists(objTargetLocation)){
  
   //variables
   mouseDistance = point_distance(x,y,objTargetLocation.x,objTargetLocation.y);
  
   image_angle = mouseDirection;
   move_towards_point(objTargetLocation.x,objTargetLocation.y, min (4, mouseDistance)); //make speed depend on fatigue
} else {
   speed = 0;}

//GLOBAL RIGHT CLICK
if (!instance_exists(objTargetLocation)){
instance_create_layer(mouse_x,mouse_y,"Instances",objTargetLocation);
}

//LEFT CLICK
if (moveTo = false){
   moveTo = true;
   mouseDirection = point_direction(x,y,objTargetLocation.x,objTargetLocation.y) - 90;
}
That should fix it unless you are setting image_angle elsewhere.

I got rid of all the rotational commands I had originally put it. If I started from scratch, how would you do it?
 


My exact code:

Create
Code:
moveTo = false;
mouseDistance = 0;
Step
Code:
if (moveTo = true && instance_exists(objTargetLocation)){
   
   //variables
   mouseDistance = point_distance(x,y,objTargetLocation.x,objTargetLocation.y);
   
   image_angle = mouseDirection;
   move_towards_point(objTargetLocation.x,objTargetLocation.y, min (4, mouseDistance)); //make speed depend on fatigue
} else {
   speed = 0;}
Left Click on Object
Code:
if (instance_exists(objTargetLocation)) {
   if (moveTo = false){
      moveTo = true;
      mouseDirection = point_direction(x,y,objTargetLocation.x,objTargetLocation.y) - 90;
   }
}
Right Click Global
Code:
if (!instance_exists(objTargetLocation)){
instance_create_layer(mouse_x,mouse_y,"Instances",objTargetLocation);
}
else {
   instance_destroy(objTargetLocation);
   moveTo = false;
}
 
M

Medievalwarfare

Guest
I used your exact code, so it works now. I'm not sure what my error was because I couldn't find another place where I use image_angle. However, you've solved my problem and I thank you
 
Top