itween turret / lookupdate inaccuracy problem

Hello,

I’m currently trying to setup a turret that rotates and fires at the players mouse position:

As you can see here it’s very inaccurate, I think it has to do with the bullet height, because with 0 height it’s perfectly fine.

var bullet : Rigidbody;

function Update(){
	var mousePos : Vector3 = Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.transform.position.y);
	var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
	iTween.LookUpdate(gameObject,{"looktarget":worldPos,"time":2,"axis":"y"});
	
	if(Input.GetMouseButtonDown(0)){
		var clone : Rigidbody = Instantiate(bullet, Vector3(transform.position.x,1.5,transform.position.z),transform.rotation);
		clone.velocity = transform.TransformDirection (Vector3.forward * 10);
	}
}

Anyone could point me in the right direction?

Your variable mousePos is storing the x and y components from Input.mousePosition which is a 2D vector in screen space. Then you are adding the camera Z component for Y , which is a 3D vector in world space.

If you leave the Y component of mousePos at zero it should be fine.

The ScreenToWorldPoint documentation uses the camera.nearClipPlane instead of Zero.

More specifically - the Z value in ScreenToWorldPoint is the distance from the camera.

So without the correct distance from the camera ScreenToWorldPoint doesn’t work properly?

The turret rotation stops working if I set y to zero.

I think the height (y) of the bullet (1.5) somhow must be taken into account at the calculation.

Sorry i typed that back to front You are using the camera.y for the mousePos.z ,

lol and Kelso said it much more concisely .

Yes, but I think that part is fine

As Kelso said the Z param for ScreenToWorldPoint is the distance from the camera, so Camera.main.transform.position.y should be correct.

You can’t use Input.mousePosition.z because it’s always zero in the 2D Screenspace.

mousePosition.z is the distance from the camera as Kelso said. As I said the documentation uses camera.nearClipPlane for this value which will keep your object a visible distance from the camera.

Using Camera.main.transform.position.y is not right. It may work but only through luck.

Z is the distance from the camera @ ScreenToWorldPoint. I don’t get it why I should use nearClipPlane (defaults to 0.3), it doesn’t make sense.

if you use a value smaller than 0.3 the object will not be visible. If you use camera.y then you are taking a position in world space and using that as a distance from the camera, totally arbitrary unless you are lucky.

If you want it to be on the same level as the turret try using camera.y - turret.y (assuming the camera is looking down on the turret)

(Of course i may have misunderstood completely if so im sure someone clever than me will be along shortly to correct me :slight_smile: )

The example just uses that to put the object in front of the camera but always ensure that the object is drawn. (IE it doesn’t get clipped for being too far away). Setting Z to 0 basically puts the object ‘in’ the camera. Using the camera’s Y position will work in a scenario where the camera is pointing straight down at the ground and the object should also be on the ground.

Your solution will depend on how the turret is supposed to react. If the turret cannot pitch up or down and you are, in essence, just making it ‘swing’ from point to point then Z will be a constant value of ((distance from camera to ground plane) - (turret height off the ground)). If the turret can pitch up and down then it gets a bit more complicated.

I think that isn’t my problem. :slight_smile:

If I set the bullet y to zero the bullet always hits the center of the crosshair, so the problem part is here:

var clone : Rigidbody = Instantiate(bullet, Vector3(transform.position.x,1.5,transform.position.z),transform.rotation);

imo I need to add the bullet instantiate y to the calculation from the mouse position somehow, but I’m completely lost here :confused:

http://webdesign.headingwest.co.uk/unity/turret/WebPlayer.html

I got bored so I made it the way I would do it. It’s in c# i’m afraid but im sure you can see , example above, zip file of project below.

http://webdesign.headingwest.co.uk/unity/turret/Turret.zip

There will be the same accuracy problem if you set the height (y) of the bullet to 1.5 and if the turret is not centered.

i have the same problem with the itween turret example :confused: