- Home /
The question is answered, right answer was accepted
Simple Object Dragging
Hello all. I'm at a game jam right now, time's running low, and I'm stuck with this object dragging.
//////
using UnityEngine;
using System.Collections;
public class Dragable : MonoBehaviour {
bool dragging;
RaycastHit hit;
// Use this for initialization
void Start () {
dragging = false;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit, Mathf.Infinity)){
Debug.Log ("Clicked Biyatch!");
dragging = true;
}
if (dragging == true){
Vector3 mpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float tx = mpos.x;
float ty = mpos.y;
rigidbody.MovePosition(new Vector3(tx,ty,transform.position.z));
}
}
void onMouseDown() {
Debug.Log("Click");
if (dragging == false){
dragging = true;
}
}
void onMouseUp(){
if (dragging == true) {
dragging = false;
}
}
}
This registers a click, and it's supposed to update the object position to the mouse x/y. It's crude but I'm trying to do it quickly so I can move on.
What winds up happening is when I click, it sets the transform to the center of the camera. Can't find why it's doing that. Am I using anything incorrectly?
Figured it out, line 21 needs to be "Vector3 mpos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, $$anonymous$$athf.Abs(transform.position.z - Camera.main.transform.position.z)));" The z position was way in the background, which messed up my mouse pos value.
Follow this Question
Related Questions
Need help on the 3ds max style camera control 0 Answers
drag to move object in perspective camera 0 Answers
Mouse dragging detection 0 Answers
Strange Camera Behavior on iOS - Runs perfect in Unity 0 Answers
Move camera with mouse (2D) 1 Answer