- Home /
How can I make character arm rotation/mouse aim work correctly?
link textSo I have a dilemma. I'm following along with a 2D platformer tutorial to learn some basics and I have one issue, that is I need my arm to flip/ change directions with the player (which it currently does) so that the gun and arm I designed looks right aiming in the correct direction. The issue I run into is that when the arm flips it will no longer aim correctly. If anyone has ideas as to how I can fix this it would be much appreciated.
Should the player be able to shoot left if he is facing right?
Ok, here is the script I have in place to rotate the arm. It is attached to the arm itself, while the arm/gun are a child of the Player.
using UnityEngine; using System.Collections;
public class ArmRotation : $$anonymous$$onoBehaviour {
public int rotationOffset = 90;
PlatformerCharacter2D player;
// Update is called once per frame
void Update () {
// subtracting position of player from mouse position
Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
difference.Normalize (); //normalizing the vector. $$anonymous$$eaning all the sum of the vector will be equal to 1.
float rotZ = $$anonymous$$athf.Atan2 (difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
}
}
I'm not a guru on this by any means but I'm quite sure flipping the scale won't automatically flip everything else.
The arm does flip, but then the issue you see in the second pic occurs when I move the cursor to aim to the left. Prior to moving the cursor the arm faces the left along with the rest of the character. I don't know if it the problem is that I need to do something to the rotation of the arm or what.
Answer by robertbu · Aug 26, 2014 at 06:23 AM
Here is a bit of code. To use:
Setup your sprite so that the front of the sprite is facing right. Given your code, right now you have it facing up.
Attach this script
Disable whatever logic is currently flipping the the texture
using UnityEngine; using System.Collections;
public class Example : MonoBehaviour { private float x; private Vector3 ls;
void Start() { x = transform.localScale.x; ls = transform.localScale; } void Update () { Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position); float rotZ = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg; if (dir.x >= 0) { transform.rotation = Quaternion.Euler (0f, 0f, rotZ); ls.x = x; transform.localScale = ls; } else { transform.rotation = Quaternion.Euler (0f, 0f, rotZ+180); ls.x = -x; transform.localScale = ls; } } }
Just try it and it works like a charm. Thanks, I knew it was something super simple but I've been working at it forever. $$anonymous$$ost other threads I see with a similar issue don't end up getting this solution so I've been going in circles. Thanks again.
Hey, so I been having the same problem, and i tried using your script but is says that it is not a valid script. Why is that?
@SlaireStudios - You'll have to be specific about the error. Any chance you pasted this script into a Javascript file? It is C#.
@robertbu , I know this is quite old and your solution worked at some point. However, since we disabled the flipping function, the body wont flip to the left now. The arms rotate correctly though.
I tried adding your code to the body(which in my case is the parent of the arms) while removing the " transform.rotation = Quaternion.Euler (0f, 0f, rotZ);" and "transform.rotation = Quaternion.Euler (0f, 0f, rotZ+180);" part. The body flips left and right using the mouse pointer however the arms go back to its previous issue. Is there a proper solution for this?
@wenson16 I was having similar issues in my game. I'm doing things a little differently though, since things are in 3d (but the gameplay is 2D). What I did was set the arm rotation to Quaternion.Euler(0f, 0f, rotZ) if the direction is to the right, and set it to Quaternion.Euler (180f, 0f, rotZ*-1) if the direction is to the left. I don't know if it'll work in this scenario, but I spend a crap ton of my time trying to figure it out so I thought i'd go ahead and share. Hope it helps.