Here's a Youtube video of my problem in action ((in the video I am clicking in various directions, yet the fireball is always created facing down in some direction.))
I've been having a lot of trouble wrapping my brain around Quaternion rotations in Unity2D.
What I'm trying to do is so simple, yet it keeps beating me. I simply want to instantiate a prefab of fireball when I click the button. The problem is that I want the fireball to be facing in the direction of the mouse before firing along. I haven't gotten to the fire and velocity part because I can't get it face the right direction when it's created.
In my playercontrollerscript attached to the player Object I have this (In C#)
if (Input.GetMouseButtonDown (0) && anim.GetBool ("canAtk")) { //Basic attack controls
curTarget = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(classState == "Cutter"){
anim.SetTrigger ("attacking");
anim.SetBool ("canAtk", false);
StartCoroutine (AttackTiming ());
anim.SetFloat ("animCombo", playerCombo);
}
else if(classState == "Flame"){
Debug.Log (curTarget);
var worldTarget = Input.mousePosition;
Quaternion rot = Quaternion.LookRotation(transform.position - curTarget, Vector3.right);
Rigidbody2D fballClone = (Rigidbody2D) Instantiate(fireBallz, transform.position, rot);
(You can safely ignore the cutter class part. The problem is when I'm in Flame class. I also have a script attached to the prefab object, but at this moment all it does is control Euler angles so it doesn't rotate along the weirdness axis.
Anyways, each time I hit the button it creates a fireball, but it's like they are 90 degrees behind where they should be. I say "like" because that's not always the case. If I click under my character, then the fireball is pointing up. If I click sort of "Down/right" it points "up/right".
This would be easier if I could visually wrap my head around how the rotations work. Any advice would be appreciated. If you need any more information or screenshots let me know as well. Thanks again.
Update: so previously I had left a line setting curTarget.z to 1 because it was returning -10 for the camera Z and I thought that was related to the problem. Oddly, when I removed that line it halfway fixed the problem. Now, if I click in the down/right or down/left or down it will create the fireball facing correct direction, but if it's "up" any, then it will pretty much face the opposite direction. Also, directly left or directly right causes the fireball to face down.
New Update: (Sort of just updating as I try to work it out.) I turned on Debug.Log(rot) just so I could see the rotation values when I create the fireball.
If I click bottom right I get (0.0, -0.6, -0.4, 0.7) the fireball is facing the correct position as it does always as long as I'm clicking in the "downish" area. When I click top right I get (-0.6, 0.0, 0.7, -0.3) and the fireball is facing Bottom-Left. So I click in the bottom-left direction and I get (0.6, 0.1, 0.7, -0.4) and it's facing bottom-left. My brain is so fried I'm not piecing it together correctly. Something I'm missing.