# Instantiated Prefab Facing Wrong Way

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.

• For me its pretty hard to get what this is about and how we could help you.. I think it would be better for ones understanding if you could provide a screenshot or so.
– d4Rk
Apr 12 '15 at 17:44
• Sorry. Tried to include as much info as possible so it would make sense. I don't want to capture 100 screenshots and then make an photobucket account and all that so I just shot a video of it and uploaded to Youtube. I will link the video in my question. I hope that's okay. ((apparently the screen capture didn't capture my mouse in the video, but I was clicking in various directions. It always comes out facing down in some manner.)) Apr 12 '15 at 17:54
• Hey guys. New update. I'm starting to think it's related to the camera and the screentoworld point. My camera is on Z axis -1 and the bottom half works as described above. If I change it to 0 or a positive number I can't see anything. If I change it to -10, then the fireballs are always created facing down. So this leads me to believe they might be connected. Apr 12 '15 at 18:32

## 1 Answer

I finally managed to solve my problem. I was able to find an old question and answer from a few years ago. The code didn't work exactly the same anymore, but I was able to get the idea of what was happening.

Here's the code to instantiate the object.

else if(classState == "Flame") {
one = (Rigidbody2D) Instantiate(fireBallz, transform.position, transform.rotation);
}


Then, on the actual fireball itself is this little snippet that controls the actual rotation.

void Start () {
if(spellName == "SmallFireball");{
mouseClickPos = Camera.main.ScreenToWorldPoint(
new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
transform.eulerAngles = new Vector3(0, 0, Mathf.Rad2Deg * Mathf.Atan2(
mouseClickPos.y - transform.position.y,
mouseClickPos.x - transform.position.x));
}
}


Works like a charm now.

• You won't delete the question. Leave it up for others to see and search for. Just select your answer as the acceptable answer in a couple days time. Apr 12 '15 at 22:05
• Posting your solution (like you did) is the best and preferred option, just don't forget to select it as the answer to your question, that stops the automatic system from bringing your question up to the front page every once in a while. Jun 12 '15 at 0:09