Question by
Fortecs · Aug 12, 2016 at 01:56 PM ·
mousecoordinates
How can i know mouse coordinates?
Hello. I have such code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject player;
void Update () {
MoveWithMouse ();
}
void MoveWithMouse(){
Vector3 playerPos = new Vector3 (-4f, player.transform.position.y, 0f);
float mousePosition = Input.mousePosition.x / Screen.width * 16 - 8;
//float mousePosition = Input.mousePosition.x;
playerPos.x = Mathf.Clamp (mousePosition, -7.5f, 7.5f);
player.transform.position = playerPos;
print (mousePosition);
}
}
If i use "float mousePosition = Input.mousePosition.x / Screen.width * 16 - 8;" mouse coordinates less than player coordinates. When player X position = 5, mouse coordinates = 7. When mouse = 0, player coordinates =0. I pretty sure that problem in formula.
60c323e97a6647f9a1b3f55c59232375.png
(47.5 kB)
b0d49c2a9f574b8b904916c064454120.png
(29.7 kB)
Comment
Answer by Fortecs · Aug 12, 2016 at 03:05 PM
Well, Google help. Answer is:
Vector3 playerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
playerPos.z = 0;
gameObject.transform.position = playerPos;
Your answer
Follow this Question
Related Questions
Moving objects with Input.mouse in PC works but not with Android 1 Answer
Render mouse in VR? 1 Answer
Make an object move in place of the mouse X and Y [UPDATE] 1 Answer
How can I disable mouse input completely? 3 Answers
3rd Person Camera Controller, Character should follow the Camera Movement 0 Answers