- Home /
Getting the cursor to follow an object - Unity C#
Hi Guys
I've been having some problems getting the cursor to follow a "Ball" (simple sphere). They Y-axis works ok (with a slight deviation as the cursor goes higher or the screenor lower) but the x-axis is followed by the cursor in the wrong direction i.e. the cursor goes left and the ball goes right.
Any help would be really appreciated!
private Vector3 Ball;
private float speed = 15.0f;
void Start()
{
Ball = transform.position;
}
void Update ()
{
float zValue = transform.position.z - Camera.main.transform.position.z;
Ball = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zValue);
Ball = Camera.main.ScreenToWorldPoint(Ball);
transform.position = Vector3.Lerp (transform.position, Ball, speed * Time.deltaTime);
}
Have you tried inverting the x value?
-Input.mousePosition.x
Yes and it weirdly sends the ball to -20 in the x direction, I've tried changing the x value many times and nothing will change it apart from Input.mousePosition.x
UPDATE
I have just realised that I have rotated the camera 180 degrees because in my game there are 2 cameras (one for player 1 and one for player 2).
The code works for the camera that is first created but when I add another camera I have to rotate it 180 degrees in the Y-axis to get it to face the opposite way...
Is there any way I can fix my code or the way my camera is set up so that the original error doesn't happen?
Thanks
oh, in that case you'll need a direct reference to the camera you want the object to move in relation to. I'm assu$$anonymous$$g that your main camera isn't the one that you are looking through when trying to have the ball follow the cursor. I updated my answer to reflect this.
Thanks for your input it really helps and I can genuinely see I'm making progress but now I have an error saying that the variable 'cam' of the script has not been assigned.
Answer by TheDarkVoid · May 21, 2015 at 05:18 PM
Your original code seems to work just fine, apart from some issues with camera rotation which may be the source of your problem. The updated code below should solve it.
public Camera cam; //Set this to your camera
private Vector3 Ball;
private float speed = 15.0f;
private float zValue;
void Start()
{
Ball = transform.position;
zValue = Mathf.Abs(transform.position.z - cam.transform.position.z);
}
void Update()
{
Ball = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zValue);
Ball = cam.main.ScreenToWorldPoint(Ball);
transform.position = Vector3.Lerp(transform.position, Ball, speed * Time.deltaTime);
}
Unfortunately this has not solved the problem, the effects are still the same. $$anonymous$$aybe I might have some settings wrong or something? Can't think of anything really.
Your answer
Follow this Question
Related Questions
3D Water Drop / Elastic Ball 1 Answer
Adding Force to a rolling ball 1 Answer
Flick soccer: Code correct but ball refuses to turn left/right 2 Answers
getting udp package info inside unity (GlovePIE) 0 Answers
Ball Falling Game. 2 Answers