Search Unity

[Unsolved] Projectile Issue...

Discussion in 'Editor & General Support' started by VoidChimera, Nov 13, 2014.

  1. VoidChimera

    VoidChimera

    Joined:
    Jul 18, 2014
    Posts:
    26
    I've been trying to code a projectile for my 2D game that explodes when it reaches the point the mouse was when it was launched, but can't seem to get the timing right for the explosion. I've been using this code for the projectile so far:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ShockwaveblastScript : MonoBehaviour {
    6.     public int speed = 4;
    7.     public GameObject shockwave;
    8.     Vector3 object_pos;
    9.     Vector3 mouse_pos;
    10.     float lifespan;
    11.     public GameObject shipExplosion;
    12.     public GameObject energyDrop;
    13.     Vector2 oldVel;
    14.     // Use this for initialization
    15.     int timer = 0;
    16.     void Start () {
    17.         mouse_pos = Input.mousePosition;
    18.         object_pos = Camera.main.WorldToScreenPoint(rigidbody2D.transform.position);
    19.  
    20.         mouse_pos.x = mouse_pos.x - object_pos.x;
    21.         mouse_pos.y = mouse_pos.y - object_pos.y;
    22.  
    23.         //Calculates the distance from the origin to cursor
    24.         lifespan = Mathf.Sqrt(Mathf.Abs(mouse_pos.x) * Mathf.Abs(mouse_pos.x) + Mathf.Abs(mouse_pos.y) * Mathf.Abs(mouse_pos.y));
    25.  
    26.         //Calculates time for the projectile to reach the cursor point based on the projectile speed,
    27.         //then multiplies by 2.8 because that scale works with the window size i've been testing at, a very bad                //crutch that doesn't fix the real problem :/
    28.         lifespan = lifespan / speed * 2.8F;
    29.        
    30.         //Calculates the angle and velocities to launch at
    31.         float angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x);
    32.  
    33.         float xVel = speed * Mathf.Cos(angle);
    34.         float yVel = speed * Mathf.Sin(angle);
    35.        
    36.         rigidbody2D.velocity = new Vector2(xVel, yVel);
    37.            
    38.     }
    39.    
    40.     void Update () {
    41.         Debug.Log("Target:" + mouse_pos);
    42.         Debug.Log("Location:" + transform.position);
    43.         GameControlerScript controlerScript = (GameControlerScript)GameObject.FindGameObjectWithTag("GameController").GetComponent("GameControlerScript");
    44.  
    45.         if (controlerScript.GUIopen)
    46.         {
    47.             if (rigidbody2D.velocity.x != 0)
    48.             {
    49.                 oldVel.x = rigidbody2D.velocity.x;
    50.             }
    51.             if (rigidbody2D.velocity.y != 0)
    52.             {
    53.                 oldVel.x = rigidbody2D.velocity.x;
    54.             }
    55.             rigidbody2D.velocity = new Vector2(0, 0);
    56.         }
    57.         else
    58.         {
    59.             timer++;
    60.             if (oldVel.x != 0 && oldVel.y != 0)
    61.             {
    62.                 rigidbody2D.velocity = new Vector2(oldVel.x, oldVel.y);
    63.             }
    64.            
    65.             oldVel = new Vector2(0, 0);
    66.         }
    67.         //If the time it's been alive is greater then the lifespan set above, detonate
    68.         if (timer > lifespan)
    69.         {
    70.             Instantiate(shockwave, new Vector3(transform.position.x, transform.position.y, 0), new Quaternion(0, 0, 0, 1));
    71.             Destroy(this.gameObject);
    72.         }
    73.     }
    74.    
    75. }
    76.  
    77.  
    But, the projectile is over/undershooting the mark depending on the size window i'm using. I would assume this has to do with me using the function WorldtoScreenPoint, but I don't see a way around it. Anyone know what I should be doing instead?
     
  2. VoidChimera

    VoidChimera

    Joined:
    Jul 18, 2014
    Posts:
    26
    Anyone? This is really fricking weird, after more testing I still can't really find the source :/
     
  3. GameDevMig

    GameDevMig

    Joined:
    Nov 5, 2014
    Posts:
    18
    Okay, biggest problem is that you are using a frame dependent timer. You should not be counting updates, use Time.deltaTime instead.

    Next, you are measuring your distance in screenspace, but moving your projectile in worldspace. If you are using a perspective camera this will not work. Use Vector3.Distance and find out in world space where the user is touching. (http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html)

    Lastly (and least if you follow my above advice), Camera.WorldToScreenPoint only works if the z value of the Vector you send it is the z distance from the camera. If your camera is not at (0,0,0) sending the object's position will return the wrong point.

    Good Luck!