- Home /
Negative scale, rotation and center of screen deathspot
Hello,
I have a problem with rotation and scaling. I rotate my characters arms with the following code:
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 toMousePos = mouseWorldPos - transform.position;
toMousePos.z = 0;
frontArm.transform.up = -toMousePos;
backArm.transform.up = -toMousePos;
All works well until my character moves, then the arms are being the wrong direction. The game is a 2D platformer with shooting elements. the Arms should rotate towards the mouse. I use 2D Platform Controller for the movement, and 2D Toolkit for the 2D related tasks. My characters arms stay on the same position because of a script that sets the position of the transform to a attachPoint in the body, which is a snippet by 2D Toolkit, which I altered to not include the rotation.
I also tried setting the Mouse Position to angles and update the Z rotation of arms, which has the same effect. I hope somebody can help me, I will look further into this problem myself too and post information and maybe even a solution as soon as I found one. For more information, just reply to this thread.
Thanks in advance,
K93
Edit: Basically it works better now, I use the following code:
private void AimDirection () {
if(inventoryAndEquipment.CurrentWeapon != null) {
Vector3 mousePos = Input.mousePosition;
mousePos.x -= Screen.width / 2;
mousePos.y -= Screen.height / 2;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if (angle<0) angle += 360;
if (inventoryAndEquipment.CurrentWeapon is RangedWeapon) {
Quaternion rotation = Quaternion.Euler(0f, 0f, angle);
frontArm.transform.localRotation = rotation;
}
}
}
Which works way better, but the problem now is that when the character quickly changes scale by moving the mouse over the character to the left, the rotation is not aligned properly. Sometimes it's the opposite, sometimes it's just a bit. I've searched a lot of questions on here already, this is how I came up with the following solution. But somehow the center of the screen has some kind of death spot. Which changes the rotation to an undesired state.