- Home /
Small math question - Attempting to have targeting reticle controlled by joystick return to an offset instead of center
In my top-down game, I have a target reticle on my screen similar to the one in this screenshot, to the right of the main character: http://cdn.pcgamesn.com/sites/default/files/nuclear%20throne%20named.png
Here's how it works: When I move the right analog stick, the target moves along with it. When the stick returns to center, so does the target. Here is my current code:
target.transform.position = new Vector2(mainCharacter.transform.position.x + Input.GetAxis("JoyHorizontal") * 3, mainCharacter.transform.position.x + Input.GetAxis("JoyVertical") * 3);
Instead of returning to center, I'd like the target to be slightly offset from my character. In other words, if I were to move the analog stick to the left and let go, it would return to a position slightly to the left of my character, instead of at my character's 0,0 point.
As I am somewhat bad with math, I'd like to know if there is a simple way to accomplish this?
Answer by NerdRageStudios · Mar 17, 2015 at 09:55 AM
create a variable for your offset and simply add it to the position.
For example
float offset = 1f;
Then you can add this to your position
transform.position.x + offset
Simply adding an offset to the position wouldn't quite work-- the way I want this to work, the exact offset would need to vary based on the last position that I'd pointed the joystick. For example, using your method, if I added an offset of 1 to X and 1 to Y, the target would always revert to a position to the northeast of the main character.
What I'd need is this: If I had just pointed my joystick south, I'd want the target to revert to a position slightly south of the main character, etc.
Your answer
Follow this Question
Related Questions
SidescrollControl X and Y Axis Movement Problem 1 Answer
Rotate a point around another point? 2 Answers
Rotating game object using UI Joystick 0 Answers
Joystick problems 1 Answer
Moving 2D Sprite Player 0 Answers