- Home /
dragging object horizontally only
hi. I'm looking for a script that would allow for horizontal drag (works just as a slider, only it is not GUI, but attached to game object). so you basically could click on an object, then drag it horizontally from min to max position..
here, in the example, we'd be able to drag the "playbutton" horizontally. but I think I messed the thing up.
var TimeLine_PLAYBUTTON:Transform;
function Update(){ Select_and_drag(); }
var focus:Transform; function selectingFocus(){ var hit:RaycastHit; var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var tempFocus = focus; //last frame focus
if(Physics.Raycast(ray, hit)){
focus = hit.transform;
}
else
{
focus= null;
}
}
var dragging = false; function Select_and_drag(){ if(Input.GetMouseButtonDown(0)){ //checking what did we click on selectingFocus(); //checking if that thing is a PLAYBUTTON if(focus == TimeLine_PLAYBUTTON) { //if so, we drag the play button Drag(TimeLine_PLAYBUTTON); dragging = true; } } //if we lift the Mousebutton - we stop dragging the playbutton if(Input.GetMouseButtonUp(0)){ dragging =false; } }
function Drag(trans_dragged:Transform){ var position_dragged = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, 0.0, 0.0)); trans_dragged.position = position_dragged; //checking for end... if(!dragging) return; //...or loop yield; }
Answer by yoyo · Dec 20, 2010 at 11:27 PM
I'd use an OnGui method to check for a mouse drag event. You can apply Event.current.delta.x to the horizontal position of your object.
Your answer
Follow this Question
Related Questions
Can I force z position to match prefab when it's dragged onto scene? 1 Answer
Drag Question 1 Answer
how i can drag a sprite in 2D mode?? 1 Answer
match an image to location when drag 0 Answers
Move a tile (sprite) with drag and drop in Unity 2D 0 Answers