- Home /
How do I lock a certain Axis on a drag object script?
Currently I can drag my object which uses a CharacterController (Don't want to use Rigid body) and I can drag it around my scene. How would I make it the object movement be locked only for a certain Axis, for example: on Axis Z?
The drag object movement script I'm using is:
var speed: float = 10; // speed
private var dragObj: Transform = null;
private var hit: RaycastHit;
private var length: float;
private var character: CharacterController;
function Update(){
if (Input.GetMouseButton(0)){ // if left mouse button pressed...
// cast a ray from the mouse pointer
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!dragObj ){ // if nothing picked yet...
// and the ray hit some character...
if (Physics.Raycast(ray, hit)){
character = hit.transform.GetComponent(CharacterController);
if (character){
dragObj = hit.transform; // save picked object's transform
length = hit.distance; // get "distance from the mouse pointer"
}
}
}
else { // if some object was picked...
// calc mouse pointer displacement
var mov = ray.GetPoint(length) - dragObj.position;
// set object velocity
character.Move(mov * speed * Time.deltaTime);
}
}
else { // no mouse button pressed
dragObj = null; // dragObj free to drag another object
}
}
Comment
Your answer
Follow this Question
Related Questions
Objects aren't moveable? 1 Answer
Lerp a Object along X and Z axis only. 3 Answers
Move character's position to middle of object 1 Answer
How to move a object by an object 1 Answer
Editor like spore? (move object on other object) C# 0 Answers