- Home /
Click and Drag not Clicking or Dragging
I am trying to create a level builder. My first task is to make a click and drag feature, tagged on to that is snap to corner but that's less of a problem now. Below is my code, and it plane doesn't work. There are no errors, it just won't pick up my clicking and dragging. I assume it has something to deal with the camera and ray casting because I know little about them. If you know a fix, please help, if you can explane how it all works too that would be nice.
var moving = false;
var posX : float;
var posY : float;
var ray : Ray;
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
function update () {
if(moving == true){
//The Object is 14 away from the camera,
Debug.Log(ray.GetPoint(14));
rigidbody.MovePosition(rigidbody.position + ray.GetPoint(14) * Time.deltaTime);
//transform.position.x = ray.GetPoint(14).x;
//transform.position.y = ray.GetPoint(14).y;
//transform.position.z = 1.1;
}
}
function OnMouseDown () {
moving = true;
}
function OnMouseUp () {
moving = false;
//Snap to feature
posX = transform.position.x;
Mathf.Round(posX);
if(posX > transform.position.x){
posX--;
}
posY = transform.position.y;
Mathf.Round(posY);
if(posY > transform.position.y){
posY--;
}
transform.position.x = posX;
transform.position.y = posY;
}
function OnCollisionEnter () {
rigidbody.isKinematic = true;
}
function OnCollisionExit () {
rigidbody.isKinematic = false;
}
Please note, I've tried using the DragObject from the wiki page, but first this is meant to be on an XY plane, and I'm not sure how to fix that properly.
Have you attached a collider to the thing you want to click and drag?
No I mean does it have a box collider, a mesh collider etc?
Yes the its a standard box with the usual box collider
Right I am just shooting at the low hanging fruit still, but your Update function has a lowercase "u" as well - not sure that will run.
Just to be clear - if you stick debug logging in each of those routines, what happens?
Answer by whydoidoit · May 21, 2012 at 08:43 PM
Ok to summarise the answer:
The Update function needed a capital U. The kinematic property needed to be set in the OnMouseXXXX functions and the rigidbody needed to be set to the ray.GetPoint(14) not moved by it.
Answer by easilyBaffled · May 21, 2012 at 08:52 PM
Here's the working code, for anyone that wants to use it. If you can make it run better or smarter, please feel free to add to it.
var moving = false;
var posX : float;
var posY : float;
var ray : Ray;
function Update () {
if(moving == true){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//The Object is 14 away from the camera,
Debug.Log(ray.GetPoint(14));
//rigidbody.MovePosition(rigidbody.position + ray.GetPoint(14) * Time.deltaTime);
transform.position.x = ray.GetPoint(14).x;
transform.position.y = ray.GetPoint(14).y;
transform.position.z = 1.1;
}
}
function OnMouseDown () {
moving = true;
}
function OnMouseUp () {
moving = false;
//Snap to feature
posX = transform.position.x;
posX = Mathf.Round(posX);
if(posX > transform.position.x){
posX--;
}
posY = transform.position.y;
posY = Mathf.Round(posY);
if(posY > transform.position.y){
posY--;
}
transform.position.x = posX + .5;
transform.position.y = posY + .5;
}
function OnCollisionEnter () {
rigidbody.isKinematic = true;
}
function OnCollisionExit () {
rigidbody.isKinematic = false;
}