I am trying to make a game where you move bricks and I dont want it to be able to move in the z axis cause I just get glitches of the brick going so close in the z axis it goes under the camera… Why is this happening and how can I fix it? My script for moving the objects is: Vector3 dist;
float posX;
float posY;
public Rigidbody rigbody;
public float distance = 0.01f;
public static bool SluppetBrikke;
public static bool BrikkeErIGolvet;
void OnMouseDown(){
rigbody.constraints = RigidbodyConstraints.FreezeRotation;
SluppetBrikke = false;
if (ColliderStopper.StoppNed == false || Input.GetAxis("Mouse Y") > 0)
{
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
//posY = distance;
}
if(ColliderStopper.StoppNed == true && Input.GetAxis("Mouse Y") < 0)
{
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
}
}
void Update()
{
transform.position.Set(transform.position.x, transform.position.y
, 0);
}
void OnMouseDrag(){
Vector3 curPos =
new Vector3(Input.mousePosition.x - posX,
Input.mousePosition.y - posY, 0);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
void OnMouseUp()
{
rigbody.constraints = RigidbodyConstraints.None;
SluppetBrikke = true;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Floor")
{
BrikkeErIGolvet = true;
}
}