turret point at mouse

heyho, yes a turrent problem again …and again and again;)

k here we go - as is wrote in my headline i want my player turret point at the mouseposition, but i´m not sure how to get my mousePosition.x andmousePosition.y to some nice coords were my turret should point at… so my screen coords need to change from 2d to 3d, well…but there´s still a z axis…
for example remember freelancer player turrets or something like that…they always “look” into the infinty, but x and y depends on the mouse.

my ai turrets are working nicely, but thats easy… they have a tagged target.
is anybody experienced in that?

well, i tryed following thing because ScreenToWorldPoint to be my solution…but hmmmmmm…

function Update () {
var mpos = camera.ScreenToWorldPoint (Input.mousePosition);
//Debug.Log(mpos);}

this attached to my cam
but the Debug.Log(mpos); shows always the same values

this is attached to a sphere which is a child of the cam

var getS: GameObject;
var go;
function Update () {
	
	//version 1
	
	//MousePosition = Input.mousePosition;
  //  MousePosition.x = (Screen.height/2) - Input.mousePosition.x;
    //MousePosition.y = (Screen.width/2) + Input.mousePosition.y;
	//transform.localPosition = Vector3(MousePosition.x/100,MousePosition.y/100,20);
	//////////// WOW ...there´s an effect......////////////////////////


	//version 2
	//go = getS.GetComponent("cam");
	//transform.localPosition = Vector3((go.mpos.x/100),(go.mpos.y/100),(20));
}

can someone kick my butt into the right direction?

no ideas how to set my mouse as target for a turret?

To have freelancer-like turret you should do a raycast from the camera position to your transformed cursor position.

See: Unity - Scripting API: Camera.ScreenToWorldPoint
Any depth value will do. (We only really need the direction)

When you find the collision position you can aim the turrets to this position with

or any other more complex script.

If there is no collision just point the turret to a very distant point along the ray direction vector.

Someone else that played freelancer (2 of em?) awesome!

I had some time a while ago and started experimenting with vary basic space game - and obviously one of the first things I wanted to do was shoot things. Here is my (basic) turret aim script:

var targetRange : float = 50.0;
var turnSpeed : float = 6.0;
var _parent : Transform;

function LateUpdate(){

var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
var rayHit : RaycastHit;
var rotateTo : Quaternion;

Debug.DrawRay(ray.origin, ray.direction * targetRange, Color.yellow);

if(Physics.Raycast(ray, rayHit, targetRange)  rayHit.transform != _parent ){
      rotateTo = Quaternion.LookRotation(rayHit.transform.position - transform.position, transform.up);
   }else{
      rotateTo = Quaternion.LookRotation(ray.GetPoint(targetRange) - transform.position, transform.up);
}
transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime * turnSpeed);
}

IIRC:

We fire a ray out from the mouse (with a range of targetRange). If it hits something (other than parent - the player’s ship) we aim at that. If we don’t hit anything, we simply aim at where the ray ends.

I think there is a good chance to find freelancer players in these forums :wink:

@NPSF3000

thx for your example, it points me to the right directions! …of course i had to change some things and i still need to modify a lot of stuff, because not the cam should point at the target, the turret should do that…and so on :smile:
yesterday i tryed around with raycasts, because noone answered me… but without any result. now i understand what was wrong.

It’s good to be of help. Just for clarification:

This script should point whatever its attached to, to the target. If it is moving your camera it probably mean’s you’ve put the script on the camera rather than your turret.