Mouse Aim X,Y coordinates

I’m getting curiously close but slightly off X,Y coordinates back when trying to aim with the Mouse. Here is a visual example of the issue (and code I’m using below that). Each sphere is .25 X units away and I drew a red dot where the cursor is. The position I get back, circled on the right of the pic, is for 4.25 which should be the center of the sphere, not slightly to the left of the sphere.

The position in X gets off by about .1 the further across the screen I go from 0. It makes the aim just slightly off and is kind of frustrating. Here’s the pic:

You can also see the aim get just slightly off here on my webplayer, the effect is worse furthest to the left/right or the bottom left/right: https://dl.dropboxusercontent.com/u/30841964/Photos/Firefly.html

It’s minor, but it’s definitely reading me the wrong position X,Y coordinates the further away from 0 I get. Here’s code:

function GetXY()
{
	var ray : Ray;
	var dist : float;
	
	plane = new Plane(cannonCamera.transform.position, Vector3.up);
	ray = cannonCamera.ScreenPointToRay(Input.mousePosition); // create a ray from the mousePosition
	
	if (plane.Raycast(ray, dist)) //dist is a float from the ray start to the hit point
	{
		target = ray.GetPoint(dist); //target is a Vector3 describing where the mouse is hovering
		target.z = -4;  //this gets set to avoid any z value (the cannon sits at z of -4)
	}	
}

Any idea why? Any suggestions? Thanks in advance for any help.

Just a hunch: try commenting the target.z = -4 out.
Your camera is at an angle so the z axis does have influence.

Sorry I should have mentioned it, this is true regardless of the cannon so you can disregard it as just for visual appeal. Meaning if I choose just an empty Object as the spawn point and disregard the cannon as a visual (and thus ignore rotation), it is still slightly off. Commented out z doesn’t fix it :frowning:

To be more clear: the trajectory it calculates is correct, but the X,Y I’m feeding it is what is slightly off (if I feed it manual X,Y it does accurately hit it).

For what it’s worth, the cannon doesn’t actually have any z rotation (which I can confirm in the inspector) because what I do is set target.z manually to -4, the cannon itself is at -4, and then I transform the cannon like this:

targetPosition = Vector3(target.x, target.y, target.z);
cannon.transform.LookAt(targetPosition);

So in terms of z, everything is at -4 to avoid that kind of an issue.

It’s really all about returning the correct X,Y values and I’m not sure why they’re just sliiiightly off! Any other ideas? :slight_smile:

I have a problem with my mouse aiming system. unity says: UnityException: Input Axis Mouse X is not setup.
But i think it is setup on the script

var crosshairCenter : Vector2 = Vector2(0.5,0.6);
var offseting : float = 2.0;
var textureUp : Texture;
var textureDown : Texture;
var textureRight : Texture;
var textureLeft : Texture;
var accuracyLoss : float;
var xOffset : float;
var yOffset : float;

private var position : Vector3;
private var xOffsetSpeed : float;
private var yOffsetSpeed : float;
private var crosshairColor : Color = Color.white;
private var crosshairAlpha : float = 1.0;

//External Scripts.
private var healthScript : health;

function Start(){
healthScript = transform.root.GetComponent(“health”);
}

function LateUpdate () {
var health : float = 100.0;
if(healthScript != null){
health = healthScript.GetHealth();
}
if(health > 0){
xOffsetSpeed += Input.GetXAxis(“Mouse X”) * Time.deltaTime * 0.2;
yOffsetSpeed += Input.GetYAxis(“Mouse Y”) * Time.deltaTime * 0.2;
}
xOffsetSpeed = Mathf.Lerp(xOffsetSpeed, 1, Time.deltaTime * 20.0);
yOffsetSpeed = Mathf.Lerp(yOffsetSpeed, 1, Time.deltaTime * 20.0);
xOffset += xOffsetSpeed;
yOffset += yOffsetSpeed;
xOffset = Mathf.Lerp(xOffset, 0, Time.deltaTime * Mathf.Pow(Mathf.Abs(xOffset),offseting ) * offseting * 100);
yOffset = Mathf.Lerp(yOffset, 0, Time.deltaTime * Mathf.Pow(Mathf.Abs(yOffset),offseting ) * offseting * 100);
position = Vector3(crosshairCenter.x + xOffset, crosshairCenter.y + yOffset, 0);
transform.position = position;
}

function OnGUI(){
var health : float = 100.0;
if(healthScript != null){
health = healthScript.GetHealth();
}
var alphaWave : float = 0.1;
if (health > 0){
crosshairAlpha = Mathf.Sin(Time.time) * alphaWave + (1-alphaWave);
}
else{
crosshairAlpha = Mathf.Lerp(crosshairAlpha,0,Time.deltaTime);
}
crosshairColor.a = crosshairAlpha;
GUI.color = crosshairColor;
var scale : float = Screen.width * 0.03;
var xPosition : float = Screen.width * crosshairCenter.x + xOffset * Screen.width - scale *0.5;
var yPosition : float = Screen.height * (1-crosshairCenter.y) - yOffset * Screen.height - scale *0.5;
var screenAccuracyDisplay : float = (accuracyLoss * Screen.width) / 40;
GUI.DrawTexture(Rect(xPosition, yPosition + screenAccuracyDisplay, scale, scale),textureUp);
GUI.DrawTexture(Rect(xPosition, yPosition - screenAccuracyDisplay, scale, scale),textureDown);
GUI.DrawTexture(Rect(xPosition - screenAccuracyDisplay, yPosition, scale, scale),textureRight);
GUI.DrawTexture(Rect(xPosition + screenAccuracyDisplay, yPosition, scale, scale),textureLeft);
}