its similar to the axis used in unity, but i want the same principles applied to my axis.
How can i do this?
My Axis Script
function OnMouseDown(){
}
function OnMouseDrag(){
}
i havent typed anything yet but how can i drag the axis object on each different position (x, y, z).
no i do not want to use raycasting.
Why, because if i want to move an object from a distance and theres an object in front of me than the raycast shooting
from the cam will hit the object in front of the camera. than i cant move the object with something in front of me.
Please and thank you for reading this topic and reply as soon as possible.
But seriously, you will. except its not physics raycasting, but Plane raycasting (so objects ‘in the way’ dont matter)
Set up your objects like this, put the script on each ‘leg’, tick only one box, and you’re good to go
using UnityEngine;
using System.Collections;
public class AXIS: MonoBehaviour {
Plane plane;
public bool isX, isY, isZ;
void OnMouseDown () {
Debug.Log( "Clicked on " + gameObject.name );
if ( isX ) plane = new Plane( Vector3.up, transform.parent.position );
if ( isY ) plane = new Plane( Vector3.right, transform.parent.position );
if ( isZ ) plane = new Plane( Vector3.up, transform.parent.position );
}
void OnMouseDrag() {
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float dist;
plane.Raycast(ray, out dist);
Vector3 pos = transform.parent.position;
if ( isX ) pos.x = ray.GetPoint( dist ).x;
if ( isY ) pos.y = ray.GetPoint( dist ).y;
if ( isZ ) pos.z = ray.GetPoint( dist ).z;
transform.parent.position = pos;
}
}
You will probably want to make a note of the initial offset of the mouse position to the parent trasform, to stop it from snapping to the mouse pos as soon as you click, but I’m sure you can manage that
OnMouseDown behaves in a way that makes me believe it uses raycasting behind-the-scenes anyway, so it must obey collider ordering - the object infront is getting a mousedown event. You can see this for yourself, just have OnMouseDown with with a debug.Log in it, it will never call on a buried object.
you will have to use your own raycast (Ha-ha), put the axes on their own layer, and then you can use a layermask to ignore any other objects. This script will then have to go on some other object, one time only, so that you aren’t casting a ray for each axis, only once for each mouseclick.
using UnityEngine;
using System.Collections;
public class AXIS: MonoBehaviour {
Plane plane;
bool isX, isY, isZ, startedClicking = false;
Transform axisBeingMoved;
void Update () {
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if ( Input.GetMouseButtonDown( 0 ) )
if ( Physics.Raycast( ray, out hit, ( 1 << 8 ) ) ) { //the 3 axis handles are on layer 8
Debug.Log( "Clicked on " + hit.collider.name );
startedClicking = true;
if ( hit.collider.name == "X" ){
plane = new Plane( Vector3.up, transform.position );
isX = true;
}
if ( hit.collider.name == "Y" ){
plane = new Plane( Vector3.right, transform.position );
isY = true;
}
if ( hit.collider.name == "Z" ) {
plane = new Plane( Vector3.up, transform.position );
isZ = true;
}
axisBeingMoved = hit.transform.parent;
}
if ( Input.GetMouseButton( 0 ) startedClicking) {
float dist;
plane.Raycast( ray, out dist );
Vector3 pos = axisBeingMoved.position;
if ( isX ) pos.x = ray.GetPoint( dist ).x;
if ( isY ) pos.y = ray.GetPoint( dist ).y;
if ( isZ ) pos.z = ray.GetPoint( dist ).z;
axisBeingMoved.position = pos;
}
if ( Input.GetMouseButtonUp( 0 ) startedClicking ) {
startedClicking = false;
isX = isY = isZ = false;
}
}
}