Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Spawning a projectile

Discussion in 'Editor & General Support' started by elias723, Apr 6, 2006.

  1. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    Hey everyone. I'm using a script to spawn and fire a projectile from the tip of a weapon, but for some reason it doesn't ever spawn from the tip. Depending on which way I face, the thing spawns in different locations, even though the empty I assigned the script to is at the tip of the barrel and is a child of the weapon. The script I'm using is this:

    Code (csharp):
    1.  
    2.  
    3. /// This behavior script fires a projectile to the point the user clicked on.
    4.  
    5. // Anything declared outside a function is visible and can be changed in the inspector
    6. var projectile : GameObject;
    7. var speed = 40;
    8. var autoDestroySeconds = 10;
    9.  
    10. var fireRate = 1.0;
    11. private var nextFire = 0.0;
    12.  
    13. function Update () {
    14.     // Fire a projectile when the user hits mouse!
    15.     if (Input.GetMouseButtonDown (0)  !Input.GetKey("q")  Time.time > nextFire) {
    16.         nextFire = Time.time + fireRate;
    17.  
    18.         // The target position of the projectile is found by casting a ray from the camera with the mouse position.
    19.         // This gives us the point the user clicked on.
    20.         ray = Camera.mainCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0.0));
    21.  
    22.         // (Note: We need to declare this variable, because it is modified inside Physics.Raycast)
    23.         var hit : RaycastHit;
    24.         if (Physics.Raycast (ray, hit)) {
    25.             target = hit.point;
    26.         }
    27.         else {
    28.             // If the user clicks outside any object, we assume the target is 1000 units in front of the camera!
    29.             target = (ray.origin + ray.direction * 1000);
    30.         }
    31.        
    32.         // The direction the projectile should fly along is simply the offset between the target position and the launch position
    33.         direction = target - transform.position;
    34.  
    35.         // Instantiate the projectile and its entire transform hierarchy
    36.         // (Note: We need to declare this variable because Instantiate can't tell which type it will return)
    37.         var instantiatedProjectile : GameObject = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.fwd, direction));
    38.         // Give it an initial velocity
    39.         instantiatedProjectile.rigidbody.velocity = direction.normalized * speed;
    40.        
    41.         // Destroy the projectile in a few seconds.
    42.         // Destroying the game object kills the game object
    43.         // with all components and its transform hierarchy.
    44.         Destroy (instantiatedProjectile, autoDestroySeconds);
    45.     }
    46. }
    47.  
    48.  
    This is a modified version of the projectile launcher script. Anything obviously wrong?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,735
    Your script launches from the camera positon and shoots in the direction where you click. Probably not what you want.

    The attached script is made for launching rigidbody based projectiles in a fps like way.

    Code (csharp):
    1.  
    2. var projectile : Rigidbody;
    3. var speed = 20.0;
    4.  
    5. function Update () {
    6.     if (Input.GetButtonDown ("Fire1")) {
    7.         // Instantiate rigidbody
    8.         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
    9.  
    10.         // Set velocity
    11.         instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
    12.  
    13.         // Ignore collision with the character collider
    14.         Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    15.     }
    16. }
    17.  
     

    Attached Files:

  3. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    Well, that's close but not exactly true. My script shoots at where the center of the screen is from where it is. It is actually attached to an empty placed at the tip of the weapon. Plus, I don't want my projectile to go where I am facing - I want it to go to where the crosshairs are in the center of the screen. That's my only problem with the script you gave me, and the reason why mine is so complicated.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,735
    Have you tried single stepping your game?

    Just before hitting fire, pause. And then hit step, to get to the next frame.
    This way you can see exactly where your objects are spawned and to which transform the launcher script is attached etc.

    Maybe that will give you an idea where the script or setup goes wrong.