MouseLook on a weapon launcher with a stationary camera?

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
:shock:

MouseLook isnt meant for looking at your cursor.

I would go with the rays. Cast a ray from your camera through your mouse position until you hit the water surface. Then you compute the angle of the delta vector of the hitpoint and your gun. After that set the rotation to your gun.

Ref:

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

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.

#pragma strict

var projectile : Rigidbody;			// projectile
var initialSpeed : float = 100.0; 			// projectile speed
var reloadTime : float = 1.0;				// reload time between shots fired
private var lastShot : float = -10.0;		// time of last shot fired

public var ShootAudio : AudioClip; 	// Audio for shot fired

function Update () {

// Check if Fire Button pressed
  if (Input.GetButtonDown ("Fire1")) {
  	
	// Did the time exceed the reload time?
	if (Time.time > reloadTime + lastShot) {
	
		// Cast a Ray to the current mouse position
	    var rayToMouse = Camera.main.ScreenPointToRay (Input.mousePosition);
    	var hitToMouse : RaycastHit;

 		if (Physics.Raycast (rayToMouse, hitToMouse)) {
 		
 			Debug.DrawLine (Camera.main.transform.position, hitToMouse.point, Color.yellow);
            Debug.Log(hitToMouse.point);
 									
			// create a new projectile then add velocity at an angle between the position and mouseHit; rotate forward to face direction
			var instantiatedProjectile : Rigidbody = Instantiate (projectile, hitToMouse.point, transform.rotation);
			instantiatedProjectile.velocity = (hitToMouse.point - transform.position).normalized * initialSpeed;
			instantiatedProjectile.rotation = Quaternion.LookRotation(instantiatedProjectile.velocity); 

			// Play the audio
			PlayShootAudio();
			
			// Ignore collisions between the missile and the character controller
			Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
		
			// Mark the time of the last projectile firing
			lastShot = Time.time;
			

		} // END CHECK RAYCAST		
	} // END CHECK RELOAD TIME
  } // END CHECK FIRE BUTTON
}

function PlayShootAudio () {
	audio.PlayOneShot(ShootAudio);
}

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