Rotate To Mouse Help

Hitman2131Hitman2131 Member Posts: 2
edited November -1 in Working with GS (Mac)
Hey Everyone. i just had a quick question regarding the rotation of an actor. What i am trying to do is make a small shooter game where the enemies come from the sides of the screen and cross, while the player occupies the screen, moving around and shooting.

However, i am having some trouble with the player's actor rotating to follow the mouse. The idea is to move with the WASD keys moving in the corresponding directions, and allowing the mouse to move freely in order to aim and shoot. The problem is the player actor does not seem to rotate properly, only when the mouse is in a top right position relative to the actor. I tried constraining the actors `self.Rotation` to position of mouse x and y, which didnt work. I then tried using vectortoangle function which also didnt work.

I am fairly new and quite lost, any help would be awesome.

Comments

  • ThundaThunda Member Posts: 1
    L2P noob
  • Hitman2131Hitman2131 Member Posts: 2
    Bump
  • JonTiStudiosJonTiStudios Member Posts: 66
    Vectortoangle is what you want to use for this. Constrain the player's rotation to the vectortoangle formula.

    Here is the info from the wiki:

    ----------------------------

    vectorToAngle

    Usage: vectorToAngle(x,y)

    Find the angle relative to the origin, (0,0), given an X and Y coordinate. You can include an offset,(x',y'), to find an angle relative to the offset.

    e.g. vectorToAngle(x-x',y-y')

    ----------------------------

    The expression you are looking for is something like this:

    vectorToAngle(player.x - mouse.x, player.y - mouse.y)
  • JonTiStudiosJonTiStudios Member Posts: 66
    To elaborate on why your first attempt with VectorToAngle probably did not work:

    I'm assuming you did something like "VectorToAngle(Mouse.x-Mouse.y)"? The reason that would not work is you are actually telling your actor to point at that spot assuming he was in 0x,0y (bottom left of the screen).

    I'm going to assume your player is not there, and in fact is moving around on screen or more towards the center. Using the above post formula "vectorToAngle(player.x - mouse.x, player.y - mouse.y)" will take into account your players X & Y position as well. It will give your mouse position assuming your player was the center point of the formula.

    Also note: if your players "front" is not pointing to zero-degrees (right direction in GameSalad) you may need to add a rotation offset after your formula.

    Let's assume your player image is actually created with "Up" being his front. Since technically when placed on screen his 0 rotation point is his right side you'll need to subtract 90 degrees. So here is what your formula would look like (on your player Rotation constraint)

    vectorToAngle(player.x - mouse.x, player.y - mouse.y) - 90

    Depending on how your player's graphic is drawn you may not need the 90 degree offset, or you may need more.
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    I cant get this to work, im using ...

    Constrain self.rotation to vectorToAngle(self.x - mouse.x, self.y - mouse.y)

    Am i doing something wrong, it wont even preview
  • JonTiStudiosJonTiStudios Member Posts: 66
    Hmm, sorry beefy_clyro, it could be a number of things.

    If you haven't figured it out, paste the exact text you have in that formula field in here for us to look at.

    Also, make sure you click on the VectorToAngle expression in the drop down (rather than type it in) as I think that capitalization can cause issues. Clicking from the list will ensure it's got the correct caps.

    Also, I think the order is backwards there. I think it should be Mouse - Player.

    vectorToAngle(mouse.x - self.x , mouse.y - self.y)
  • firemaplegamesfiremaplegames Member Posts: 3,211
    And just to be certain,
    You are choosing all those attributes from the drop-down menu, right?
    You aren't just typing them in..

    If it's not even previewing, there must be a typo somewhere...
  • beefy_clyrobeefy_clyro Member Posts: 5,394
    Thanks for the help guys, i have it by putting mouse before self, as said above by jontistudios
Sign In or Register to comment.