- Home /
Unity2D Why are the distances calculated different on different screen size?
Hello everyone,
I am making a game about a ball rotating around a pivot. Now, I want to make my ball move nearer or further away from the pivot, based on the position of my mouse.
For example, if I click hold and drag my mouse further from the pivot, the ball will move further.
Therefore, I need to calculate the distance between the pivot and the mouse.
Everything seems to be running great, ONLY ON unity editor. When I play it on my mobile phone, it doesn't function the same way as in the unity editor.
And the problem seems to be the DISTANCE CALCULATED IS DIFFERENT.
Can you guys explain, why is there a difference in distance calculated, and how do I solve this? The red cross is where I place my mouse (on unity), touch (on mobile)
On Unity
On Mobile
This is the code, I will only put up the codes relating to the distance. Target is the pivot.
using UnityEngine; using System.Collections;
public class playerController : MonoBehaviour {
public Transform target;
public float fRadius = 3.0f;
private Vector3 vt;
private float vx;
private float vy;
public float dist;
public bool mouseTrack;
public bool secondmode;
void Start ()
{
}
void Update () {
if (!secondmode)
{
fRadius = dist;
}
if (fRadius < 1.5f)
{
fRadius = 1.5f;
}
//vt is the vector/distance from pivot to mouse
vt = Camera.main.WorldToScreenPoint (target.position);
vt = Input.mousePosition - vt;
//Calculating distance of mouse from pivot
vx = vt.x - target.position.x;
vy = vt.y - target.position.y;
dist = Vector3.Distance (vt,target.position);
}
}