- Home /
How drag a block along Z-Axis and keep it under the mouse?
With the below code, I am able to click and drag an object, restricting said objects transforms to only X and Z. The issue is that when you drag the cube towards the edges of the screen, the object will either fall behind or go ahead of the mouse pointer, dependent on what "factor" I set. Is there a better way to have drag the cube around?
This is for a SimCity'ish type game I am creating: I want to be able to drag an object around, as well as have the ability to freely rotate the camera around.
var factor : float = 1.50;
private var objectStartPos : Vector3; // Initial position of object
private var mousePosition : Vector3; // Initial position of the mouse in world coordinates
private var baseZDistance : float; // Base Z distance to use in conversion
function OnMouseDown(){
objectStartPos = transform.localPosition;
var worldScreenPoint : Vector3 = Camera.main.WorldToScreenPoint(objectStartPos);
baseZDistance = worldScreenPoint.z;
mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, baseZDistance);
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
}
function OnMouseDrag(){
worldScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, baseZDistance);
worldScreenPoint = Camera.main.ScreenToWorldPoint(worldScreenPoint);
var worldScreenPoint2 : Vector3 = objectStartPos;
worldScreenPoint2 = objectStartPos;
worldScreenPoint2.x = Mathf.Round(worldScreenPoint2.x + (worldScreenPoint.x - mousePosition.x) / 2) * 2;
worldScreenPoint2.z = Mathf.Round(worldScreenPoint2.z + (worldScreenPoint.z - mousePosition.z) / 2) * 2;
transform.localPosition = worldScreenPoint2;
}
What is the point of view of the camera? What axis are you looking down and is the camera tilted?
Guidez : I've decided to scrap the project. $$anonymous$$y current knowledge and understanding of coding and Unity is way too little for what's needed for this particular game, so I am going to start with something a LOT smaller. Thanks!
$$anonymous$$e : while it is probably a good idea do do alot of small simple projects first when starting out, don't scrap your project, just put it on the shelf for later =]
In regard to your problem, what you need to learn about is layers and Layer$$anonymous$$ask. With this, you can cast a ray on your terrain ignoring the 'cube', so the cube will stay under the mouse while you are dragging.
http://docs.unity3d.com/Documentation/Components/Layers.html
http://answers.unity3d.com/questions/8239/what-is-the-purpose-of-layers.html
http://docs.unity3d.com/Documentation/ScriptReference/Layer$$anonymous$$ask.html
the layermask bitshift stuff can be very confusing. Another way is to use the Inspector. First create your new layer, assign it to your object, then in script create a variable for your Layer$$anonymous$$ask, eg :
var terrainLayer : Layer$$anonymous$$ask;
In the Inspector, there should be a drop-down box with all the layers, just tick the layer you want to raycast against.
if ( Physics.Raycast( rayPos, rayDir, rayHit, rayDist, terrainLayer ) )
Answer by Guidez · Feb 18, 2013 at 03:07 AM
I've decided to scrap the project. My current knowledge and understanding of coding and Unity is way too little for what's needed for this particular game, so I am going to start with something a LOT smaller. Thanks!
Noooooo.
BTW, Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.