- Home /
Why is my Gameovject not at 0 in the z acis?
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;
       }
   }
Answer by LK84 · Aug 11, 2016 at 10:44 AM
@gibbie_learnersedge answer is an option, but note that with rigidbody constraints for position you are restricted to world coordinates. If you want to be more flexible:
Substitute
      transform.position.Set(transform.position.x, transform.position.y
          , 0);
with
 transform.position=new Vector3(transform.position.x,transform.position.y,0);
in the Update() Function. The reason your solution didn't work is that transform.position apparently only returns a copy, so Set() only sets the position of the copy.
Note: It's now still set in world coordinates. If you don't want the object to get closer to the camera, you have to use WorldToScreenPoint of course.
Answer by gibbie_learnersedge · Aug 11, 2016 at 10:16 AM
You could try to constraints your z-axis in your rigidbody. It is under constraints. See if that helps.
@gibbie_learnersedge I have tried, but it seems like the script is overriding it anyway how I do it, so it seems like I must fix the script
void On$$anonymous$$ouseUp() { rigbody.constraints = RigidbodyConstraints.None; <----- maybe remove this line? SluppetBrikke = true; }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                