I have some public var Speed: float and Horizontal: bool in my dragObject.js script.
This script allow me to click and drag (move) tagged objects.
My speed variable allow me to move the object slowly or quickly.
My Horizontal variable allow me to drag horizontally(X-axe) OR vertically (Z-axe).
There is my problem:
When I create 1 cube, the script works well, but when I create 2 cubes, I can’t choose different speed or Horizontal for each object.
Speed will be the same for both object and both object will drag in the same sens.
I want to drag the first cube horizontally and the second vertically with each one different speed, but I CAN’T.
What is the problem?
Thanks!
#pragma strict
private var pickObj: Transform = null;
private var hit: RaycastHit;
private var dist: float;
private var oldPos: Vector3;
private var newPos: Vector3;
var Speed: float = 5.0;
var Horizontal: boolean;
function Start ()
{
}
function Update ()
{
if (Input.GetMouseButton(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!pickObj)
{
if (Physics.Raycast(ray, hit) hit.transform.tag == "Pick")
{
if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
pickObj = hit.transform;
oldPos = pickObj.position;
dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
}
}
else
{
newPos = ray.GetPoint(dist);
pickObj.position = Vector3.Lerp(pickObj.position, newPos, Time.deltaTime * Speed);
if(Horizontal)
{
if(pickObj.position.z != oldPos.z) pickObj.position.z = oldPos.z;
if(pickObj.position.x < 0) pickObj.position.x = 0;
if(pickObj.position.x > 5) pickObj.position.x = 5;
pickObj.position.y = 0.5;
}
else if(!Horizontal)
{
if(pickObj.position.x != oldPos.x) pickObj.position.x = oldPos.x;
if(pickObj.position.z < 0) pickObj.position.z = 0;
if(pickObj.position.z > 5) pickObj.position.z = 5;
pickObj.position.y = 0.5;
}
}
}
else {
pickObj = null;
}
}