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 Petumies · Jan 10, 2017 at 09:43 PM · movementtransformtransform.positionmousepositiontransform.translate

Moving an object on different axis?

So I'm making a game that involves base building and I ran into this problem that I can't solve on my own. So the idea is quite simple, I get my mouse position which then moves an object. The object itself is locked on the Y axis so that leaves only 2 axis so I think it should be possible to track the mouse position and use it to move the object. Now the problem with that is I can't figure a way to make the object move relative to the player. It only moves relative to the world coordinates. I've tried to work with transform.translate but can't get what I want. Any tips or ideas?

Comment
Add comment · Show 1
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 GrayMatterTutorials · Jan 11, 2017 at 01:34 AM 0
Share

@Petumies, could you explain this a little more. I will go ahead and post an answer below for what I think you are trying to accomplish, but I'm a little confused! $$anonymous$$ore explanation would help a great deal!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GrayMatterTutorials · Jan 11, 2017 at 01:47 AM

Ok, so from what I understand is that you want to move the building from where it was, to a new location based on mouse movement. You don't want the object to move relative to where the mouse is pointing, just in the direction the mouse is moving.

So let's say I have a building at position (0, 0, 0) and my mouse is pointing at position (10, 0, 10). Let's say I move the mouse up to position (12, 0, 12). To make it easy, I moved the mouse 2 units on the x-axis and 2 units on the z-axis. Instead of moving the building to (12, 0, 12), you want it to move to (2, 0, 0), correct?

If that is the case, I recommend storing to variables and run through an Update method. The first variable will be Vector3 mousePosition and the second variable will be Vector3 newMousePosition. I recommend only checking the following when moving a building. First you will calculate the newMousePosition in world space and then compare it to the mousePosition. If it is not greater than the desired deadzone on either the x or z, don't do anything. If it is, then translate the building by newMousePosition - mousePosition, then set mousePosition equal to newMousePosition. This will allow you to continue to check this until you place the object. Obviously you are probably going to want the values to increment by a nice rounded number, and this can be achieved by creating a new Vector3 called targetPosition and setting it's values to a rounded value after the subtraction takes place.

Other recommendations: If this is going to be used on Mobile, I understand your reasoning for wanting to do it this way. However, it is much simpler, and probably more of a standard to have it just jump to the mousePosition. You are only going to see a small part of the game world, so the only part it can jump to will be a short distance away, so it really won't make much of a difference between the two methods. Also, if you have gesturing, you may want to block panning the screen when moving buildings like this. If you aren't deploying on Mobile and are sticking with PC, I would just use the keyboard to get the desired effect you are looking for.

If this isn't what you were wanting to know, just let me know and I will change my answer. I left a comment on your original question asking for more details, but if this answers it, feel free not to leave any. Good luck, and I'm here to help!

Comment
Add comment · 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

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

How to shift the player to a definite position in the x-axis using key presses? 1 Answer

Why is rotation offset 1 Answer

How to move an object smoothly while in a Coroutine / Ienumerator? 3 Answers

How to make camera move like it does in the scene viewport? 0 Answers

Character Transform shifting after colliding with objects 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