- Home /
turret rotation accuracy problem
I've already tryed my luck in the unity forum, but I haven't found anyone who could help me, so please excuse the crossposting.
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 (y), because with setting y to zero the bullet always hits the center of the crosshair.
I think the bullet-height must be added to the rotation calculation somehow.
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?
I think you'll need more detail. A 2D mouse position maps to a ray projected into the view frustum, so I presume you have a 3D turret? If so, what point on that ray do you want to aim at? What about the turret, how many axis can it rotate on?
I don't use raycasting, but the way you describe it would also work. I use ScreenToWorldPoint ins$$anonymous$$d of ScreenPointToRay.
Yes, the turret is in 3D and always rotates on it's y axis (x + z is locked) to the position of the mouse cursor.