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 Ogi_Magi · May 11, 2020 at 02:54 AM · gameobjectinputmousepositionautomation

Adding an offset to instantiated object based on mouse distance from player

Hello, I've been trying to recreate a skill from the game Path of Exile in Unity, and I'm doing pretty well except one thing:

The skill shoots projectile(s), and if there is more than one projectile, the arc of the projectiles depend on how close of far your cursor to the player, if the cursor is close to player the angle between projectiles is very large, and projectiles get tighter if you aim away from the player.

Another question is how can I automate the offset of Instantiated projectiles based on the number Instantiated, like 45 degrees for 2 projectiles, 30 for 3, 20 for 4 etc. Thanks in advance!

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 UnityToMakeMoney · May 11, 2020 at 04:41 AM 0
Share

Well, I think if you can get the distance of where the mouse and object are, then you can just math, algebra, to figure out the arc (maybe use pi)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ogi_Magi · May 12, 2020 at 11:39 PM

I did it, here's the code with comments:

     public int projCount;
     public float projArcAngle = 100;
     private float minAngle = 50;
     private float maxAngle = 150;
 
 void IceShot()
     {
         var playerPos = transform.position; // get the player position
         Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition); // cast a ray in cursor's position
         RaycastHit hitInfo;
         Physics.Raycast(myRay, out hitInfo, 100);
         Vector3 mousePos = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z); // exact position of mouse in world coordinates
         var dist = Vector3.Distance(mousePos, playerPos); // distance between mouse position and player position
         var finalArcAngle = projArcAngle;
         if ((Mathf.Round(Mathf.Clamp(dist, 1, 20) / 2) > 5)) // distance is clamped between 1 and 20 then halved, so that 0 is player position and 10 is the max distance
         {
             var amount = ((Mathf.Round(Mathf.Clamp(dist, 1, 20) / 2) - 5) * 10); // amount is distance from middle to maximum multiplied by 10
             finalArcAngle = projArcAngle - ((projArcAngle * amount) / 100); // we decrease the arc angle 10% per 1 distance from 5 to 10, so arrows will be tighter if aimed at a distance
             finalArcAngle = Mathf.Clamp(finalArcAngle, minAngle, maxAngle); // finally clamp it to a desired value, in my case 50 to 150
         }
         var angle = (finalArcAngle / (projCount + 1)); // this is the offset angle each projectile will spawn, determined by amount of projectiles
         for (int i = 1; i <= projCount; i++) // repeats until it reaches projCount
         {
             var calculatedAngle = (angle * i) - (finalArcAngle / 2); // finally, the exact position each arrow will spawn depending on i 
             Vector3 arrowRotation = new Vector3(arrowPos.transform.rotation.x, (arrowPos.transform.rotation.y + calculatedAngle), arrowPos.transform.rotation.z);
             Instantiate(arrow, arrowPos.transform.position, arrowPos.transform.rotation * Quaternion.Euler(arrowRotation));
         }


I know it's very messy and needs cleaning up, but i figured it all out by myself and kinda proud of it!

Comment
Add comment · 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 Ogi_Magi · May 13, 2020 at 12:53 AM 0
Share

I fixed it, It now the only thing left is adjusting the angle based on the mouse distance from the player. Here's the code

 public int projCount;
 private float maxAngle = 120f;
 void IceShot()
     {
         var angle = (maxAngle / (projCount + 1) );
         for (int i = 1; i <= projCount; i++)
         {
             var offset = (angle * i) - (maxAngle / 2);
             Vector3 offset3 = new Vector3(arrowPos.transform.rotation.x, (arrowPos.transform.rotation.y + offset), arrowPos.transform.rotation.z);
             Instantiate(arrow, arrowPos.transform.position, arrowPos.transform.rotation * Quaternion.Euler(offset3));
         }
     }

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

220 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

Related Questions

How do I rotate an object using Vector2 inputs? (New Input System) 1 Answer

How to get different Animations when different button combos are pressed 2 Answers

How to click within a game objects collider? 0 Answers

is it Possible to use navigation on non UI elements? 1 Answer

Input.mousePosition incorrect on second monitor 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