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

Firing and Aiming

Discussion in 'Scripting' started by iiii_Hex, Jul 22, 2011.

  1. iiii_Hex

    iiii_Hex

    Joined:
    Nov 2, 2009
    Posts:
    34
    I have projectiles Instantiating and traveling along my Z axis, but I'd like to have them aim towards the mouse so the player may choose where to fire. Right now, I simply have:

    Code (csharp):
    1. if(Input.GetMouseButton(0)  Time.time > nextFire){
    2. nextFire = Time.time + fireRate;
    3. GameObject clone = Instantiate(playerShot, transform.position, transform.rotation) as GameObject;
    4.         }
    As well as some additional code for monitoring the firing rate. I noticed if I remove the "GameObject clone = " and " as GameObect" in the third line and it still functions. What is the purpose of those section?
     
    Last edited: Jul 22, 2011
  2. iiii_Hex

    iiii_Hex

    Joined:
    Nov 2, 2009
    Posts:
    34
    Shameless bump.
     
  3. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Try to bump around every other day...
     
  4. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    Download the unity fps tutorial and look at the machinegun Script.
     
  5. iiii_Hex

    iiii_Hex

    Joined:
    Nov 2, 2009
    Posts:
    34
    Sorry...



    Just to clarify pezz, is this something I should look at for a 2D game as well as a FPS?
     
    Last edited: Jul 24, 2011
  6. Stathis

    Stathis

    Joined:
    Jul 18, 2011
    Posts:
    10
    You can try this way:

    // Instantiate a rigidbody then set the velocity

    var projectile : GameObject;

    function Update () {
    // Ctrl was pressed, launch a projectile
    if (Input.GetButtonDown("Fire1")) {
    // Instantiate the projectile at the position and rotation of this transform
    var clone : GameObject;
    clone = Instantiate(projectile, transform.position, transform.rotation);

    // Give the cloned object an initial velocity along the current
    // object's Z axis
    clone.rigidbody.velocity = transform.TransformDirection (Vector3.forward * 100);
    }
    }
     
  7. iiii_Hex

    iiii_Hex

    Joined:
    Nov 2, 2009
    Posts:
    34
    I'm having some trouble with your code Stathis. (I'm working in C#.) I did find "transform.forward" which is along the z vector. Should I be looking for a way to point the player's object towards the mouse then using a "myPrefab.velocity = transform.forward;"? Or should I tell the projectile to locate the mouse's position upon instantiation and travel towards that point.

    Again, this game of mine is simply taking place on the x and z axis being a top down game.
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Atan2 is what you need, get the angle between mouse and player. You then rotate player to face that angle, rather than doing it back to front.
     
  9. iiii_Hex

    iiii_Hex

    Joined:
    Nov 2, 2009
    Posts:
    34
    I'm having trouble understanding how to connect Atan2 to my code.

    Is it possible to give an Instantiate coordinates after its Instantiation? For example:

    if(Input.GetMouseButton(0) Time.time > nextFire){
    nextFire = Time.time + fireRate;
    Instantiate (playerShot, transform.position, transform.rotation);
    }
    Then...

    playerShot is transform.Translate(ing) towards the position of the cursor at the moment it was created?