Rotate To Mouse Help
Hitman2131
Member Posts: 2
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.
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
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)
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.
Constrain self.rotation to vectorToAngle(self.x - mouse.x, self.y - mouse.y)
Am i doing something wrong, it wont even preview
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)
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...