Shooting Where Clicking

I have been all over the forums looking for a way to do this and I’m not sure if I’m searching the wrong words or just missing something relatively simple but the fact remains that I’m still at a loss.

In a first person view, I want to shoot an object from where the camera is positioned in the direction the mouse is pointing. I’m currently using a static camera so all the shooting will have to take place with the mouse. Eventually I plan to do movement by moving the mouse to the edges of the screen but I thought I should solve the shooting problem first.

Thoughts and/or direction?

Do you mean something along these lines?

–Eric

That’s pretty much exactly what I was thinking.

I think I may have just solved the problem though. It seems that when I finally give up and post the question, 9 time out of 10 I have a lightning bolt shortly after. Here’s what I came up with. Please correct me if I’ve not thought it though.

  1. Create a target object in front of the camera and attach this script to it.
var depth = 10.0; 

function Update () 
{ 
     var mousePos = Input.mousePosition; 
     var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth)); 
     transform.position = wantedPos; 
}
  1. Create another object, parent it to the camera and add this script to it.
var target : Transform; 

function Update() {
    transform.LookAt(target);
}
  1. Add this script to the object parented to the camera.
var projectile : Rigidbody;
function Update () {
    if (Input.GetButtonDown("Fire1")) {
        var clone : Rigidbody;
        clone = Instantiate(projectile, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection (Vector3.forward * 10);
    }
}

And now it seems to be working. I tried parenting the target object to the camera so I can rotate and so far things seem to be good.

Any thoughts on turning the camera, just heading, when the mouse is on the far right or left of the screen?

Yep, that’s basically what I did.

Probably something like

if (mousePos.x < 1) {
   camera.main.transform.Rotate(-Vector3.up*rotateSpeed*Time.deltaTime);
}
if (mousePos.x > Screen.width-2) {
   camera.main.transform.Rotate(Vector3.up*rotateSpeed*Time.deltaTime);
}

–Eric

Thanks.

I managed to come up with this.

function OnGUI () { 
  	var rect = Rect (0,0,Screen.width/10,Screen.height); 
	
	if (rect.Contains(Input.mousePosition)){
        transform.Rotate(Vector3.up * Time.deltaTime*-10);
	}

  	var rect1 = Rect (Screen.width/10*9,0,Screen.width/10,Screen.height); 
	
	if (rect1.Contains(Input.mousePosition)){
        transform.Rotate(Vector3.up * Time.deltaTime*10);
	}
}