- Home /
slingshot style mouse mechanic, don't want inverted when player facing left
For a 2D game (movement on x/y only) I have a 'slingshot' style moving mechanic in progress where you click on the player and drag behind him, then release to propel forward. During the drag, the angle the player faces is opposite the mouse position (see code below). I have this close, but have hit a wall...when the player flips to look left due to the mouse being dragged to the right of the player center, the y-axis mouse movement is being used in the opposite direction, so that up=up, as opposed to up=down as it is when the player is facing right.
This is what I have right now:
function OnMouseDrag(){
zCurrent = transform.rotation;
mousepos = Input.mousePosition;
mousepos.z = Input.mousePosition.z; //The distance between the camera and object, is flattened here
var objectpos = Camera.main.WorldToScreenPoint(transform.position);
mousepos.x = (mousepos.x - objectpos.x);
mousepos.y = (mousepos.y - objectpos.y);
mouseAngle = Mathf.Atan2(mousepos.y, mousepos.x) * Mathf.Rad2Deg;
angleFlip = (mouseAngle - 180);
if(((angleFlip+360)>90)&&((angleFlip+360)<270)){
angleFlip = mouseAngle;
var y : float = 180.0;
Debug.Log("rotated to face left");
}
else{
y = 0.0;
Debug.Log("rotated to face right");
}
zTarget = Quaternion.Euler(Vector3(zCurrent.x, y, angleFlip));
//zTarget = Quaternion.Euler(Vector3(zCurrent.x, zCurrent.y, angleFlip));
ChangeDirection(zCurrent, zTarget);
And...
function ChangeDirection(zCurrent : Quaternion, zTarget : Quaternion){
// Dampen towards the target rotation
var pointInTime : float = 0.0;
while (pointInTime <= rotationDamping) {
transform.localRotation = Quaternion.Slerp(zCurrent, zTarget, pointInTime/rotationDamping);
pointInTime += Time.deltaTime;
yield 0;
}
}
What can I do to keep the behavior the same as when facing right while facing left? (so that the player is rotated to 'look' opposite the mouse position)
Answer by SilverTabby · Jul 30, 2011 at 03:41 AM
Your problem is in this line:
angleFlip = (mouseAngle - 180);
You are moving your angle across both the X && the Y axis by just subtracting by 180. You need to flip across the X axis, but not the Y axis for the result you want
you can fix this in either of two ways:
angleFlip = 180 - mouseAngle; //flips across X, but not Y
or the more descriptive but less efficent
angleFlip = Mathf.Atan2(mousepos.y, -mousepos.x) * Mathf.Rad2Deg;
Thanks for the reply! While that suggestion did not work (and I had tried that before), it made me rethink how I was rotating, and I came up with a new scheme that does work. Basically, I'm rotating a child object within a new 'master' object now, and that made this easier to deal with.
I somehow knew clever parenting would solve your problem.
Though I'm confused why this wouldn't work, I actually did a bit of algebra to get to the angle - 180 so I would like to know where my mistake is, if any :\
I think I found it. This line:
if(((angleFlip+360)>90)&&((angleFlip+360)<270))
makes absolutely no sense :|
The first condition will ALWAYS be true because the range of atan (converted to degrees) is [180, -180] + 360 = [540, 180]
and the second condition will only be true if angle flip is between [-180, -90) + 360 = [180, 270)
I don't think it was my math, but the fact that the script never actually uses it. Ins$$anonymous$$d of using +360, just try $$anonymous$$athf.abs(). Then the if will be true when the angle is between (-270, -90) U (90, 270)
Your answer
![](https://koobas.hobune.stream/wayback/20220612162253im_/https://answers.unity.com/themes/thub/images/avi.jpg)