Hello, i need help. Ok, i need only to move an object like a sphere with my mouse. My script work and i can move my sphere with my mouse, but i have a problem, the sphere and the mouse cursor do not move in the same time, then when i play in a window mode, the cursor is out of the window but not my sphere and if i click,the cursor click out of my window and my program stop. Can i script the sphere to follow the cursor ? if yes how ?
var horizontalSpeed = 2.0;
var verticalSpeed = 2.0;
function Update ()
{
// Get the mouse delta. This is not in the range -1...1
var h = horizontalSpeed * Input.GetAxis ("Mouse X");
var v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Translate (0, v, 0);
transform.Translate (h, 0, 0);
}
Thanks to help my, i search from 2 days…
Stephen
Hello, i have found this script, it is what i want the gameobject follow the cursor, but only when i click on them, but i want the object follow all time, without click on then, can you help me ?
function Update () {
}
var screenSpace;
var offset;
function OnMouseDown(){
//translate the cubes position from the world to Screen Point
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
//calculate any difference between the cubes world position and the mouses Screen position converted to a world point
offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
}
/*
OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseDrag is called every frame while the mouse is down.
*/
function OnMouseDrag () {
//keep track of the mouse position
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world
transform.position = curPosition;
}
You need to place the code in the Update function, so it will be called on each frame update:-
function Update() {
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}