Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Projectile Not Shooting at Target Properly

Discussion in 'Scripting' started by imnickb, Jul 31, 2014.

  1. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    I'm shooting a projectile at a target and it seems that the projectile is not reliably firing at the target. It seems to favor going to the left side a little bit and it seems to move at a faster rate when shooting towards the right? I can't figure this out and just need it to work consistantly. My shooting script is very simple so I'm not sure where I'm going wrong here. I've also attached a test level for you to see what I'm talking about. Move the cannon with a and s keys, shoot with space.

    EDIT: A good test to see what I'm talking about is to aim at about 3 o'clock and fire a ball, then aim at 9 o'clock and fire. I'd expect them to behave similarly but they don't.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CannonBall : MonoBehaviour
    5. {
    6.  
    7.     float ballSpeed = 0;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {  
    12.         //Add velocity in the direction of the Cannon Sight
    13.         rigidbody2D.velocity = transform.TransformDirection      
    14.         (GameObject.Find("CannonSight").transform.position);
    15.  
    16.         //Wait three seconds after instantiation and destroy the cannon ball
    17.         Destroy(GameObject.Find("CannonBall(Clone)"), 3);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {  
    23.  
    24.     }
    25. }
     

    Attached Files:

  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    I don't think transform.TransformDirection does what you think it does. It transforms a direction from local space to world space.

    http://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

    But CannonSight's transform.position is already in world space, and it's not a direction.

    Does the CannonSight rotate? You might just try CannonSight.up or CannonSight.right or something? Those are directions instead of positions, and they are already updated for world space.


    Also, using Find to find itself is redundant and potentially buggy if there are ever two cannon balls.
    Destroy(gameObject, 3);
     
  3. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    Thanks for the input. I'm pretty sure you're right, using Find to find itself is maybe not the best idea, but using Destroy(gameObject, 3); results in the game object I'm copying also being deleted which means I can no longer instantiate anymore cannon ball game objects. Maybe I need to instantiate copies of a prefab rather than instantiate copies of a game object.
     
  4. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    Cloning and destroying a prefab seems to be much more reliable, thanks for the help with simplifying the destruction of the projectile. You were right, transform.right was exactly what I needed, it's much more predictable and accurate. The main issue of projectiles not firing the correct direction was mostly due to my sprite images being angled when they should've been straight. I fixed the art and it's much better. The script fixes you suggested also were a big help. Here's my updated script if anybody's interested:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CannonBall : MonoBehaviour
    5. {
    6.  
    7.     float ballSpeed = 25.5f;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {  
    12.         //Add velocity in the direction of the right transform of the cannon barrel
    13.         rigidbody2D.velocity = GameObject.Find("CannonBarrel").transform.right * (-ballSpeed);
    14.  
    15.         //Wait three seconds after instantiation and destroy the cannon ball
    16.         Destroy(gameObject, 3);
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {  
    22.  
    23.     }
    24. }