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.