- Home /
Click & Drag change from xy to xz
So I've done up a script with allows the player to click and drag objects with the mouse currently at the moment it moves on the XY axis however I want to make it so the object can only be set to move on the XZ axis. How would I go about doing this? I also have made an snap to ground script so that if the object collides with another object it will snap to the surface of it.
MouseScript
using UnityEngine;
using System.Collections;
public class MouseScript : MonoBehaviour {
private RaycastHit rayHit;
private GameObject collideObj;
private float distance;
private Vector3 posObj;
private bool lockObj;
private int layerMask;
// Use this for initialization
void Start ()
{
layerMask =~ (1 << 8);
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButton (0))
{
var ray = camera.ScreenPointToRay (Input.mousePosition);
var hit = Physics.Raycast (ray.origin, ray.direction, out rayHit, 100f, layerMask);
if (hit && !lockObj)
{
lockObj = true;
collideObj = rayHit.collider.gameObject;
collideObj.GetComponent<StickToGround>().enabled = false;
distance = rayHit.distance;
Debug.Log (collideObj.name);
}
if (lockObj)
{
posObj = ray.origin + distance * ray.direction;
collideObj.transform.position = new Vector3 (posObj.x, posObj.y, collideObj.transform.position.z);
}
}
else
{
lockObj = false;
if (collideObj != null)
{
collideObj.GetComponent<StickToGround>().enabled = true;
collideObj = null;
}
}
}
}
Stick to Ground Script
using UnityEngine;
using System.Collections;
public class StickToGround : MonoBehaviour
{
private RaycastHit rayHit;
public float deltaPos;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
var hit = Physics.Raycast(transform.position, transform.up * (-1), out rayHit);
if (hit)
{
transform.position = new Vector3(rayHit.point.x, rayHit.point.y + deltaPos, rayHit.point.z);
}
}
}
How are you stopping it from immediately clicking back down in place?
I'm having trouble visualizing what you're trying, I mean I understand you want to move objects into the scene rather than up, but I just can't think how it's going to work without instantly snapping to the ground if you don't allow for an initial move upwards.
You got a screen shot?
Incidentally the theory is just translate the y position of the mouse to be z distance from the camera but I still think it'll be more complex than that makes it sound.
Well the script in question is being used to depict the pads from a defibrillator so stick to the ground script relates to the the pads being placed on the body. Too which at a later point I'll put notification that states either if the user has placed it in the correct spot or not.
I kind of know where the problem in question is as I'm controlling the object with ScreenPointToRay on the camera and as the camera is in a cone shape by moving the object towards the middle of the screen in turns brings the object high due to the cone like formation of the camera.
How would i go about forcing the Ray so that e.g. the camera was square sized or 2D e.g. it still used the Y value to revert back to as a step up. But it was still fixed in place so it could only run along the x & z axis?
An idea has crossed my $$anonymous$$d on how to fix the problem but I haven't the foggiest how to implement it. $$anonymous$$y idea is to make a set of boundaries for the object when its moved by the mouse so e.g. the object can't move away from the boundaries of the camera or invisible barriers preventing the mouse from dragging the object past a certain point. If placed above the gameobject then it would prevent it from rising up in the cone shape of the camera and keep it at a fixed height. How would I implement this?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers
(Oculus Rift) Casting a ray from mouse through distortion matrix 1 Answer
Raycast won't fire 3 Answers