• 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
Question by Doghelmer · Mar 17, 2015 at 09:04 AM · 2dmathjoystick

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?

Comment

People who like this

0 Show 0
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

· Add your reply
  • Sort: 
avatar image

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

Comment

People who like this

0 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 Doghelmer · Mar 17, 2015 at 05:01 PM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Fit in squares in a dedicated space 2 Answers

Function to rotate my Player smoothly taking the y and use it to have angle 1 Answer

Introducing joystick to Unity? 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

New Input system mouse returns wrong values 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