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 /
  • Help Room /
This question was closed May 06, 2020 at 05:40 AM by boss2070301 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by boss2070301 · May 04, 2020 at 08:47 PM · mouse position

Help with turning this into only x

I need someone to turn this code into so it only moves on the x-axis and not the y-axis. { public float speed = 10f; private Vector3 targetPosition; private bool isMoving; public GameObject clickAnimation; // Use this for initialization void Start() {

     }
 
     // Update is called once per frame
     void Update()
     {
             targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             targetPosition.z = transform.position.z;
             if (isMoving == false)
             {
                 isMoving = true;
                 Instantiate(clickAnimation, targetPosition, Quaternion.identity);
         }
         if (isMoving == true)
         {
 
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
         }
         if (targetPosition == transform.position)
         {
             isMoving = false;
             Destroy(GameObject.Find("Click Animation(Clone)"));
         }
     }
 }
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

1 Reply

  • Sort: 
avatar image
1

Answer by streeetwalker · May 04, 2020 at 09:16 PM

@boss2070301, you can't directly set the transform.position.x, so you have to store your move position in a Vector3 first. then you want to use the x position from that result together with the current y and z position of your transform.

There are lots of ways you can cut this and end up with the same pie, so here is one way in pseudo code

  Vector3 newPos = your move pos function call
  newPos = new Vector3( newPos.x, transform.pos.y, transform.pos.z )
  transform.pos = newPos


That will keep the same y and z but x will change

Also this line of code will probably never be true no matter how you move your objects, so better to check the distance between your target and your transform against some threshold, again in pesudo code:

   // won't be ever true unless you explicitly set their x, y, and z
   //      exactly the same non decimal floats
   if (targetPosition == transform.position)

   // do this instead
   if(  Vector3.Distance( targetPos , trans.pos ) <= 0.001f  )
   // will be true if they are within 1mm of each other
   //  change the threshold to the value you like
   
Comment
Add comment · Show 7 · 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 boss2070301 · May 04, 2020 at 09:43 PM 0
Share

@streeetwalker can you put the correct version of the code and implement it into my code please I am new so I don't know how to do so.

Thanks

avatar image boss2070301 · May 05, 2020 at 09:01 PM 0
Share

@streeetwalker please help me I didn't understand your explanation can you just tell me what line to put the first code in the second one I understand but first I didn't so please help me.

avatar image streeetwalker boss2070301 · May 05, 2020 at 09:27 PM 0
Share

I did answer your question! You have to learn how to take pseudo code and and make it work. If you pasted my 3 lines of code into your editor, it would basically tell you what you need to fix. br>

so 1) take this your line of code:

transform.position = **Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, speed Time.deltaTime);*

and replace it with my 3 lines of code.

2) then where I stated "your move pos function call" - replace that with your $$anonymous$$oveTowards function all bold text above.

3) Then every place you see transform.pos in my code, make sure to spell position completely, because there is no "transform.pos" - there is 'transform.position'

We write that pseudo code because it is faster to explain things, and we assume you should be able to figure all that out!

avatar image boss2070301 streeetwalker · May 05, 2020 at 09:55 PM 0
Share

I assigned everything but the game still does the same thing. @streeetwalker

  if (is$$anonymous$$oving == true)
         {
             Vector3 newPos;
             transform.position = newPos = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, speed * Time.deltaTime);
             newPos = new Vector3(newPos.x, transform.position.y, transform.position.z);
             transform.position = newPos;
         }
         if (Vector3.Distance(targetPosition, transform.position) <= 0.001f)
         {
             is$$anonymous$$oving = false;
         }

Show more comments

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Click area in Game 1 Answer

How to zoom in correctly to a certain part of an object with mouse scroll? 0 Answers

how to differentiate touch and swipe 2 Answers

how to make flashlight follow mouse pointer in 2D? 1 Answer

Move Camera 2D Towards Mouse Position 0 Answers


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