- Home /
Object dragging works in X and Y but not in Z axis using plane.raycast.
Hello,
I made a little script to move an object with three children handles (colored arrow meshes). The script works pretty well in X and Y axis but does not work in Z axis. I tried many things but I do not get why I can not get to move the cube in Z with the blue handle.
Here is the code of the script:
using UnityEngine;
using System.Collections;
public class plane : MonoBehaviour {
public float ent;
RaycastHit hit;
public Vector3 lastHitPos;
public string moveAxis="";
void Update()
{
Plane groundPlane= new Plane(Vector3.up, transform.position);
Plane rightPlane= new Plane(Vector3.right, transform.position);
Plane fowardPlane= new Plane(Vector3.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.name=="xHandle")
{
if (groundPlane.Raycast(ray, out ent))
{
moveAxis="x";
lastHitPos=ray.GetPoint(ent);
}
}
if(hit.collider.name=="yHandle")
{
if (rightPlane.Raycast(ray, out ent))
{
moveAxis="y";
lastHitPos=ray.GetPoint(ent);
}
}
if(hit.collider.name=="zHandle")
{
if (fowardPlane.Raycast(ray, out ent))
{
moveAxis="z";
lastHitPos=ray.GetPoint(ent);
}
}
}
}
if (Input.GetMouseButton(0))
{
if(moveAxis=="x")
{
if (groundPlane.Raycast(ray, out ent))
{
float deltaPosX=ray.GetPoint(ent).x-lastHitPos.x;
transform.position = new Vector3(transform.position.x+deltaPosX,transform.position.y,transform.position.z);
lastHitPos=ray.GetPoint(ent);
}
}
if(moveAxis=="y")
{
if (rightPlane.Raycast(ray, out ent))
{
float deltaPosY=ray.GetPoint(ent).y-lastHitPos.y;
transform.position = new Vector3(transform.position.x,transform.position.y+deltaPosY,transform.position.z);
lastHitPos=ray.GetPoint(ent);
}
}
if(moveAxis=="z")
{
if (fowardPlane.Raycast(ray, out ent))
{
float deltaPosZ=ray.GetPoint(ent).z-lastHitPos.z;
transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z+deltaPosZ);
lastHitPos=ray.GetPoint(ent);
}
}
}
else{
moveAxis="";
}
}
}
Answer by itsharshdeep · Dec 01, 2015 at 01:24 PM
Hello
Sorry I didn't read your code because of little less time. But I have achieved this with the following code but I'm only taking the point where the player clicked on the 3D table
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)) {
Debug.DrawLine(ray.origin, hit.point);
}
It may be not perfect .. but for more info you can check here
Your quick reply helped me refigure how to calculate the Z axis movement. It works with the groundplane ins$$anonymous$$d of a fowardPlane(Vector3.forward)...
Here is the corrected code:
using UnityEngine;
using System.Collections;
public class plane : $$anonymous$$onoBehaviour {
public float ent;
RaycastHit hit;
public Vector3 lastHitPos;
public string moveAxis="";
void Update()
{
Plane groundPlane= new Plane(Vector3.up, transform.position);
Plane rightPlane= new Plane(Vector3.right, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.name=="xHandle")
{
if (groundPlane.Raycast(ray, out ent))
{
moveAxis="x";
lastHitPos=ray.GetPoint(ent);
}
}
if(hit.collider.name=="yHandle")
{
if (rightPlane.Raycast(ray, out ent))
{
moveAxis="y";
lastHitPos=ray.GetPoint(ent);
}
}
if(hit.collider.name=="zHandle")
{
if (groundPlane.Raycast(ray, out ent))
{
moveAxis="z";
lastHitPos=ray.GetPoint(ent);
}
}
}
}
if (Input.Get$$anonymous$$ouseButton(0))
{
if(moveAxis=="x")
{
if (groundPlane.Raycast(ray, out ent))
{
float deltaPosX=ray.GetPoint(ent).x-lastHitPos.x;
transform.position = new Vector3(transform.position.x+deltaPosX,transform.position.y,transform.position.z);
lastHitPos=ray.GetPoint(ent);
}
}
if(moveAxis=="y")
{
if (rightPlane.Raycast(ray, out ent))
{
float deltaPosY=ray.GetPoint(ent).y-lastHitPos.y;
transform.position = new Vector3(transform.position.x,transform.position.y+deltaPosY,transform.position.z);
lastHitPos=ray.GetPoint(ent);
}
}
if(moveAxis=="z")
{
if (groundPlane.Raycast(ray, out ent))
{
float deltaPosZ=ray.GetPoint(ent).z-lastHitPos.z;
transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z+deltaPosZ);
lastHitPos=ray.GetPoint(ent);
}
}
}
else{
moveAxis="";
}
}
}
Your answer
Follow this Question
Related Questions
Creating a custom runtime scaler !? 0 Answers
Handles.matrix seems strange? 2 Answers
see colliders in editor 1 Answer
How to mimic Unity Scene Gizmo? 2 Answers