Search Unity

MouseLook on a weapon launcher with a stationary camera?

Discussion in 'Scripting' started by ddsinteractive, May 8, 2013.

  1. ddsinteractive

    ddsinteractive

    Joined:
    May 1, 2013
    Posts:
    28
    Hello!

    Am prefixing this with my very new voyage in coding this type of project in Unity. I have a simple project that is in the works and am not certain how best to work the shooting portion of the interactivity. Here is what I have:

    Have a water terrain and am looking straight down at it with the MainCamera (rotated to face the water). I have the weapon launcher empty gameObject as a child of the MainCamera. Am shooting into the water to harpoon a fish as it swims by. I want the harpoon to aim and enter the water where the mouse position is. The Fire1 input is the trigger.

    The fish are spawned randomly - have that working. They also are propelled (forward) with force across the scene and are destroyed on the other side of the viewport when they collide with a collider that is out of the viewport view.

    I believe that my struggle is with the mouseLook script that I have applied to the Launcher. That is what I am attempting to use to "aim" the launcher - which "shoots" down the forward (Z) axis. I've also tried using Rays but that doesn't seem to work either. On the mouseLook script I tried limiting the X and Y movement using the script controls but the harpoon still doesn't enter the water at the mouse cursor. The MainCamera can't have the mouseLook on it because it needs to be stationary because the viewport should not change, just stay static as the fish float through the scene.

    Am at my wit's end and am not sure the best way to get the aiming to line up with the mouse cursor the right way. Is it something to do with the gimble lock? Wrong place to attach the mouseLook script? Wrong use of the mouseLook script? Have been scouring the forums for answers but am not quite sure the questions I need to ask.

    Any guidance, other forum topics that I may have missed or a cold beer are welcome.

    Cheers,
    Monica
    :eek:
     
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
  3. ddsinteractive

    ddsinteractive

    Joined:
    May 1, 2013
    Posts:
    28
    Thank you Arterie, I'll give that a go. Since the camera and water surface are fixed, the ray distance should be fairly easy to set.
    Will repost with my results.

    Cheers,
    Monica
     
  4. ddsinteractive

    ddsinteractive

    Joined:
    May 1, 2013
    Posts:
    28
    Here is the code I used. This project will have unlimited ammo but one could add a variable for an ammo or clip count and deduct a count (--) as needed through appropriate loops.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var projectile : Rigidbody;         // projectile
    5. var initialSpeed : float = 100.0;           // projectile speed
    6. var reloadTime : float = 1.0;               // reload time between shots fired
    7. private var lastShot : float = -10.0;       // time of last shot fired
    8.  
    9. public var ShootAudio : AudioClip;  // Audio for shot fired
    10.  
    11. function Update () {
    12.  
    13. // Check if Fire Button pressed
    14.   if (Input.GetButtonDown ("Fire1")) {
    15.    
    16.     // Did the time exceed the reload time?
    17.     if (Time.time > reloadTime + lastShot) {
    18.    
    19.         // Cast a Ray to the current mouse position
    20.         var rayToMouse = Camera.main.ScreenPointToRay (Input.mousePosition);
    21.         var hitToMouse : RaycastHit;
    22.  
    23.         if (Physics.Raycast (rayToMouse, hitToMouse)) {
    24.        
    25.             Debug.DrawLine (Camera.main.transform.position, hitToMouse.point, Color.yellow);
    26.             Debug.Log(hitToMouse.point);
    27.                                    
    28.             // create a new projectile then add velocity at an angle between the position and mouseHit; rotate forward to face direction
    29.             var instantiatedProjectile : Rigidbody = Instantiate (projectile, hitToMouse.point, transform.rotation);
    30.             instantiatedProjectile.velocity = (hitToMouse.point - transform.position).normalized * initialSpeed;
    31.             instantiatedProjectile.rotation = Quaternion.LookRotation(instantiatedProjectile.velocity);
    32.  
    33.             // Play the audio
    34.             PlayShootAudio();
    35.            
    36.             // Ignore collisions between the missile and the character controller
    37.             Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    38.        
    39.             // Mark the time of the last projectile firing
    40.             lastShot = Time.time;
    41.            
    42.  
    43.         } // END CHECK RAYCAST     
    44.     } // END CHECK RELOAD TIME
    45.   } // END CHECK FIRE BUTTON
    46. }
    47.  
    48. function PlayShootAudio () {
    49.     audio.PlayOneShot(ShootAudio);
    50. }
    51.  
    52.  
    Thanks for pointing me in the right direction!
    Credits to the following additional forum posts as well:

    http://answers.unity3d.com/questions/129516/Shoot-bullet-along-the-ray-cast.html
    http://forum.unity3d.com/threads/38646-ScreenPointToRay-MousePosition

    Cheers,
    Monica