- Home /
Can't move game object left of center
I'm new (obviously...) and I'm trying to use the mouse to control my game object's X position while running the following code:
transform.position.x = Input.mousePosition.x;
When my mouse is at the left edge of the screen the corresponding game object will stay at the center, if I move the cursor to the right I can move the game object to the right and then back to the center, but regardless how far to the left my cursor is on the screen the game object will not move past the center. After adjusting the "Game" drop down selection menu from "Free Aspect" to "Standalone" the game object will move freely from left to right but the numbers in the debug log are not accurately depicting the center of the screen as 0.
What do I have to do to get the game object to move properly to the left side of the screen while in "Free Aspect"? Also please note that I'm using a laptop with a finger mouse in case the mouse type has anything to do with it.
Thanks,
Alex
Answer by aldonaletto · Sep 28, 2011 at 01:06 AM
You are messing things: the screen is a 2D space whose coordinates range from (0,0) to (Screen.width, Screen.height), while the objects are in a 3D world where the coordinate system is totally different (it depends on the camera position and orientation).
If you want to drag the object with the mouse, you must convert from the screen space to the world space - but since the world is 3D, the mouse position must be extended to 3D by adding the distance from the camera as the Z coordinate:
var dist: float;
function OnMouseOver(){ // if this object clicked, save the distance from the camera in dist if (Input.GetMouseButtonDown(0)){ dist = Vector3.Distance(transform.position, Camera.main.transform.position); } }
function Update(){ // while the button remain pressed, drag the object if (Input.GetMouseButton(0)){ var mousePos: Vector3 = Input.mousePosition; mousePos.z = dist; // extend mousePosition to 3D with the distance var pos = Camera.main.ScreenToWorldPoint(mousePos); // convert to 3D world transform.position = pos; // follow the mouse pointer } }
Answer by PatrikNyblad_SI · Oct 14, 2011 at 07:17 AM
This piece of code will check to see if you press the left mouse button and when you do it will cast a ray from the coordinates (pixels) in you gamewindow. If this ray hits an object like a plane with a collider attached (you have to have some sort of ground like a plane for this to work) it will move the current objects position.x to the hits position.x.
if (Input.GetMouseButtonDown(0)) {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
Debug.DrawRay (ray.origin, ray.direction * 10, Color.red);
if (Physics.Raycast(ray, hit)) {
transform.position.x = hit.position.x;
}
}
Hope it helps!
Your answer
Follow this Question
Related Questions
I am stuck in tutorial 9/12 Activate the elevator 0 Answers
Are there any in depth beginner tutorials? 6 Answers
Essential math for games 2 Answers
New to Unity and stuck!!! 1 Answer
UI color issue 1 Answer