Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eirikamb · Aug 11, 2016 at 09:24 AM · mousepositionglitchmouse-dragclick and drag

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;
       }
   }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eirikamb · Aug 12, 2016 at 09:35 AM 0
Share

Thank you very much, I would`ve never figured that out

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eirikamb · Aug 11, 2016 at 10:44 AM 0
Share

@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

avatar image gibbie_learnersedge eirikamb · Aug 11, 2016 at 10:46 AM 0
Share

void On$$anonymous$$ouseUp() { rigbody.constraints = RigidbodyConstraints.None; <----- maybe remove this line? SluppetBrikke = true; }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Click-and-drag to rotate camera around point at centre 1 Answer

Move mechanical arm with mouse 0 Answers

How to I mouse Drag sprite diagonally only? 0 Answers

Dragging object with a mouse from one point to another point, Have a big problem 1 Answer

How would I make the mouse drag move smoothly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges