Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 22, 2021 at 01:16 AM by GgJmGg for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by GgJmGg · Feb 14, 2021 at 11:55 PM · 2ddistancemousepositionrangedistance check

How to stop an object of going to far

look, I have this scripts, that detects where the cursor is, and teleport the object scope is (its basically to make the player aim in 2d with a sprite of a scope, its in my profile pic. The thing is, my script also detects the distance between the player and the scope,and i would like to make it so that the scope can go further than a certain distance for the player, so basically that distance is the range of the weapon

     scoop.position = Camera.main.ScreenToWorldPoint(new Vector3(
         Input.mousePosition.x,
         Input.mousePosition.y,
         -Camera.main.transform.position.z
         ));
     distance = (scoop.position - gameObject.transform.position).magnitude;

alt text

(Ignore the fact that the player is a chicken) Until now i have been "showing" the player the range with that circle, what i want to do actually is make the scope sorta rotate around the player if it gets to far, but make it so it can move frely if it isn´t too far.

In advance thanks for the answer, I love this community even if I barely know how to program

imagen-2021-02-14-205424.png (3.8 kB)
Comment
Add comment · Show 5
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 GgJmGg · Feb 14, 2021 at 11:56 PM 0
Share

the scope is in the top/right part of the image

avatar image AllTheGoodNamesWereTaken · Feb 15, 2021 at 02:45 AM 0
Share

Just for clarification, the ⊙ is where your cursor is. The big circle is the area you want your duck to be able to teleport. You want the ⊙ to stay inside of the big circle, even if your mouse goes outside of it?

avatar image GgJmGg AllTheGoodNamesWereTaken · Feb 15, 2021 at 04:53 AM 0
Share

yeah, exactly

avatar image GgJmGg GgJmGg · Feb 15, 2021 at 04:54 AM 0
Share

and, its a chicken, have a little respect for him/her

avatar image GgJmGg AllTheGoodNamesWereTaken · Feb 15, 2021 at 05:18 AM 0
Share

@AllTheGoodNamesWereTaken would like your answer too if you don´t $$anonymous$$d ^^

2 Replies

  • Sort: 
avatar image
-1

Answer by ricky_lee_ · Feb 15, 2021 at 03:59 AM

I had a go at the maths for you to try, I'm not that fantastic at coding but I think this should be close.

 Vector2 ScoopVector;            // World position of the Scoop
 Vector2 ChickenVector;          // World position of the chicken          

 float angle;                    // maths
 float realativeX;               // more maths
 float relativeY;                // much more maths
 float distance;
 float weaponRange = 1f;

 void Update()
 {
     // Work out the world position of the mouse (put into scoop vector)
     ScoopVector = new Vector2(
                                 Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                                 Camera.main.ScreenToWorldPoint(Input.mousePosition).y
                              );

     // Work out the world position of the chicken
     ChickenVector = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);


     // Calculate the distance between them
     distance = (ScoopVector - ChickenVector).magnitude;

     // If the distance is too far, change the x and y values
     if (distance > weaponRange)
     {
         // The maths
         realativeX = (ScoopVector.x - transform.position.x);
         relativeY = (ScoopVector.y - transform.position.y);
         angle = Mathf.Atan2(realativeX, relativeY);

         // New x/y coordinates
         ScoopVector.x = weaponRange * Mathf.Sin(angle);
         ScoopVector.y = weaponRange * Mathf.Cos(angle);

         // Finally apply the position offset from your Chicken
         scoop.GetComponent<Transform>().position = ScoopVector + ChickenVector;
     }
     else
     {
         // Apply the position (no adjustment needed)
         scoop.GetComponent<Transform>().position = ScoopVector;
     }
 }


Incase this doesnt help, I was thinking of a vague idea you could maybe try, using 'LookAt'. If the scoop was a child of the chicken, and you used 'lookAt' to rotate it with the mouse The local axis of the scoop should move with the rotation:

alt text

Then all you need is the distance value you already made to set a local y axis offset some thing like:

 if (distance <= Range){
 
 localOffset.y = distance;
 
 }
 else{
 
 localOffset.y = Range;
 
 }

Comment
Add comment · Show 12 · 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 Eno-Khaon · Feb 15, 2021 at 04:36 AM 1
Share

Incidentally, there's also a more efficient way to write part of this, trading Atan2() and a Sqrt() for a multiplication and optional Sqrt().

 // The main from-to vector
 Vector3 playerToTarget = scoopVector - transform.position;

 // sqr$$anonymous$$agnitude, for a cheaper calculation when the condition isn't met
 if(playerToTarget.sqr$$anonymous$$agnitude > weaponRange * weaponRange)
 {
     // normalize the from-to vector and multiply by
     // weapon range to reset the length of the vector
     scoop.position = transform.position + (playerToTarget.normalized * weaponRange);
 }
avatar image GgJmGg Eno-Khaon · Feb 15, 2021 at 04:59 AM 0
Share

I can´t thank all of you enough, its only been 4 hours and i got my answer, now im going to test the code, and if it works im closing the answer

avatar image GgJmGg GgJmGg · Feb 15, 2021 at 05:23 AM 0
Share

The scope disapears, I thinks this is due to that is behind the background(i mean the main camera background(because i didn´t add one myself yet) any ideas on how to fix this?

Show more comments
avatar image
0

Answer by MUG806 · Feb 16, 2021 at 09:32 AM

Very simple solution for you, as everything posted is overcomplicated:

         //calculate the target scope position as before, but store it in a variable
         Vector3 targetScopePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-Camera.main.transform.position.z));
         //calculate vector from player to scope
         Vector3 toScope = targetScopePosition - transform.position;
         //limit the distance to the scope to the desired weapon range
         toScope = Vector2.ClampMagnitude(toScope, weaponRange);
         //use the limited vector to find the correct limited target position
         targetScopePosition = transform.position + toScope;
         //Set the scope to the target position, but keep its z position so it doesnt get lost behind anything.
         scoop.position = new Vector3(targetScopePosition.x, targetScopePosition.y, scoop.position.z);

Assuming you already have your scope in your "scoop" variable and stored the distance limit in "weaponRange"

Comment
Add comment · Show 12 · 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 GgJmGg · Feb 16, 2021 at 04:36 PM 0
Share

I still have my last problem Since the cursor its separate from the scope, the cursor(even if not visible) keeps going outside of range, it wouldn be a problem except this means that it takes longer to get inside the circle. I will put a gif to show you(you dont have to solve this, you´ve done more than enough and I wouldn´t have any of the code if not for your answers, but just to let you know) https://answers.unity.com/storage/attachments/176048-ezgifcom-crop.gif

avatar image MUG806 GgJmGg · Feb 16, 2021 at 05:43 PM 0
Share

In that case you want to not use the hardware cursor at all, and just add movement to the scope as the player moves the mouse.

You want to remove this line:

 Vector3 targetScopePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-Camera.main.transform.position.z));

and replace it with this:

 //get the current scope position
         Vector3 targetScopePosition = scoop.position;
         //add how far the player moved their mouse this frame, multiply it by a sensitivity value, and multiply it by the deltaTime to account for framerate.
         targetScopePosition += new Vector3(Input.GetAxisRaw("$$anonymous$$ouse X"), Input.GetAxisRaw("$$anonymous$$ouse Y")) * aimSensitivity * Time.deltaTime;

You will also need to add this to the top of your class so you can tweak the sensitivity in the inspector:

     public float aimSensitivity = 1f;


full script ends up looking like this:

 public class ScopeAim : $$anonymous$$onoBehaviour {
     public Transform scoop = null;
     public float weaponRange = 10;
     public float aimSensitivity = 1f;
 
     private void Update() {
         //get the current scope position
         Vector3 targetScopePosition = scoop.position;
         //add how far the player moved their mouse this frame, multiply it by a sensitivity value, and multiply it by the deltaTime to account for framerate.
         targetScopePosition += new Vector3(Input.GetAxisRaw("$$anonymous$$ouse X"), Input.GetAxisRaw("$$anonymous$$ouse y")) * aimSensitivity * Time.deltaTime;
 
         //calculate vector from player to scope
         Vector3 toScope = targetScopePosition - transform.position;
         //limit the distance to the scope to the desired weapon range
         toScope = Vector2.Clamp$$anonymous$$agnitude(toScope, weaponRange);
         //use the limited vector to find the correct limited target position
         targetScopePosition = transform.position + toScope;
         //Set the scope to the target position, but keep its z position so it doesnt get lost behind anything.
         scoop.position = new Vector3(targetScopePosition.x, targetScopePosition.y, scoop.position.z);
     }
 }
avatar image GgJmGg MUG806 · Feb 16, 2021 at 06:53 PM 0
Share

yeah it doesn´t work, the scope doesn´t move with the mouse, it just stays in place

Show more comments

Follow this Question

Answers Answers and Comments

274 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 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

resizing spherecast radius over distance? 4 Answers

Spawning an object attached to one object, drawn to another. 1 Answer

variable++ error... 1 Answer

Problem with Vector3 Distance and Mouse Input, 1 Answer

Extend a line between two Vectors by a distance 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