- Home /
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?
@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!
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!
Your answer
Follow this Question
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