Problem with Quaternion. Instantiate does not work properly

Started by
1 comment, last by plainoldcj 9 years ago

Hi guys,

I use the following code to instantiate bullets which should fly into the direction of the mouse cursor. It does not work, as you can see on the image below, the bullets fly exactly at the opposite side of the mouse pointer!

I tried to multiply with -1 in every way, it didnt help!

Can anybody help? I am using Unity 5 right now and I would like to create a little 2D mobile App.


void Shoot () {
         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
         if (Time.time >= timeToSpawnEffect) {
             //Hier Sound Effekt
             //AudioSource.PlayClipAtPoint(shoot,transform.position);
             if(GameMaster.flipActive==false){
                 Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
                 //Effect ();
             }
             else if (GameMaster.flipActive==true){
                 Vector3 firePosi = new Vector3 (firePoint.position.x,firePoint.position.y,firePoint.position.z);
                 Quaternion fireRota = new Quaternion(firePoint.rotation.x,firePoint.rotation.y,firePoint.rotation.z,firePoint.rotation.w);
                 Debug.Log(firePosi);
                 Debug.Log(fireRota);
                 //import ab hier
 
                 Instantiate (BulletTrailPrefab, firePosi, fireRota);
             }
             timeToSpawnEffect = Time.time + 1/effectSpawnRate;
         }
         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
         if (hit.collider != null) {
             Debug.DrawLine (firePointPosition, hit.point, Color.red);
 
 
             Enemy enemy = hit.collider.GetComponent<Enemy>();
             if(enemy != null){
                 enemy.DamageEnemy(Damage);
                 Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
             }
         }
     }

E3B363X.jpg

Advertisement
Why are you using quaternions in a 2D game? Unit-length complex numbers are much better for representing 2D rotations.
Okay, I'm tired and maybe I'm missing something.
However, it appears that you simply adopt the orientation of firePoint for the
newly instantiated game object. I assume firePoint is the tip of the gun and its a child
of the player game object.
So, I'd say you turn the player in direction of the mouse, the firePoint turns accordingly
and then you instantiate the bullet with the same orientation.

What you want to do is shoot the bullet in direction (mousePos - firePoint.pos).
If I recall the Unity API correctly, you could simply assign that direction
to transform.forward. Alternatively, there is a method Quaternion.LookRotation.

This topic is closed to new replies.

Advertisement