Author Topic: Move player to mouse coordinates  (Read 265 times)

Glenjamin

  • video games are for nerds
Move player to mouse coordinates
« on: 30 Jan 2018, 16:08 »
Hello everyone,

I wanted to program a unique kind of movement for a part in my game where the player is on a raft.

My plan was to make a region that when clicked on, sets the player to walk to the mouse location, but with some distance added to simulate momentum.

Also it would be blocked so the player could only move to one location, then wait, and then move to another location to simulate the raft only moving when they paddle with an oar

Code: Adventure Game Studio
  1. function hswimtest_AnyClick()
  2. {
  3. //if the mouse is in front of the player
  4.  if (mouse.x > player.x){
  5.     player.Move(mouse.x+5, mouse.y, eNoBlock, eAnywhere);
  6.  
  7. //if the mouse is behind the player    
  8.    }else  if (mouse.x < player.x){
  9.      player.Move(mouse.x-5, mouse.y, eBlock, eAnywhere);
  10.  
  11.    }  
  12. }

The issue is the mouse coordinates seem to change to the far left of the screen, and the character moves there instead of where the mouse had clicked.

As far as I can tell the in-engine walk mechanics aren't interfering with this. Thoughts?

selmiak

  • ǝsıɔɹǝxǝ ʞɔǝu puɐ uıɐɹq
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
    • selmiak worked on a game that was nominated for an AGS Award!
Re: Move player to mouse coordinates
« Reply #1 on: 30 Jan 2018, 16:20 »
is this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.

Glenjamin

  • video games are for nerds
Re: Move player to mouse coordinates
« Reply #2 on: 30 Jan 2018, 16:29 »
Quote
is this a scrolling room? Then you have to add the viewport coordinates to the mouse coordinates.

This IS a scrolling room!

How would I go about adding the viewport coordinates to the mouse coordinates?

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Move player to mouse coordinates
« Reply #3 on: 30 Jan 2018, 16:38 »

dayowlron

  • been programming computers since 1981
    • I can help with AGS tutoring
    •  
    • I can help with proof reading
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
Re: Move player to mouse coordinates
« Reply #4 on: 30 Jan 2018, 16:38 »
Code: Adventure Game Studio
  1. function hswimtest_AnyClick()
  2. {
  3. //if the mouse is in front of the player
  4.  if (mouse.x > player.x){
  5.     player.Move(GetViewPortX() + mouse.x+5, GetViewPortY() + mouse.y, eNoBlock, eAnywhere);
  6.  
  7. //if the mouse is behind the player    
  8.    }else  if (mouse.x < player.x){
  9.      player.Move(GetViewPortX() + mouse.x-5, GetViewPortY() + mouse.y, eBlock, eAnywhere);
  10.  
  11.    }  
  12. }
  13.  
Pro is the opposite of Con
This fact can clearly be seen,
If progress means to move forward
Then what does congress mean?  --Nipsey Russell

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Move player to mouse coordinates
« Reply #5 on: 30 Jan 2018, 16:41 »
dayowlron, you forgot to subtract Viewport coordinate from player.x:
Code: Adventure Game Studio
  1. if (mouse.x > player.x - GetViewportX())
  2.  

IMO its better to translate everything into room coordinates first. This way the further calculations will be much simplier.

Code: Adventure Game Studio
  1. int room_click_x = mouse.x + GetViewportX();
  2. int room_click_y = mouse.y + GetViewportY();
  3. if (room_click_x  > player.x){
  4.     player.Move(room_click_x + 5, room_click_y, eNoBlock, eAnywhere);
  5.  
and so on...

EDIT: my apologies, did wrong conversion :) fixed now.
« Last Edit: 30 Jan 2018, 16:44 by Crimson Wizard »

dayowlron

  • been programming computers since 1981
    • I can help with AGS tutoring
    •  
    • I can help with proof reading
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
Re: Move player to mouse coordinates
« Reply #6 on: 30 Jan 2018, 17:52 »
good point.
Pro is the opposite of Con
This fact can clearly be seen,
If progress means to move forward
Then what does congress mean?  --Nipsey Russell