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 vispan · Jun 30, 2016 at 07:00 PM · scripting problemvector math

how to calculate resultant direction vector from a given set of vectors?

Hi, I am making a 2d game in which a player has to save the a character (which is in the middle of screen) from several zombies coming towards the character. I have setted up a triggered collider around the character(the one , a player has to save). if the moving zombie triggers the collider then the zombie chases it. then the character should run in opposite direction from the zombie. if there are several zombies inside the collider then the character should run in the resultant direction (like shown in the image ) (E-> Enemy zombies, P-> character). alt text

vec.jpg (13.9 kB)
Comment
Add comment
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
0

Answer by Habitablaba · Jun 30, 2016 at 07:00 PM

You should spend some time reading up on vector math. What you have is 2 input vectors, one coming from each enemy, and you want to combine them to find a new direction.
Check it out:

To get the vector from the enemy to the player, simply use subtraction.

 var vector = player.transform.position - enemy.transform.position;

This vector is actually telling you two things; direction and magnitude (distance). In your case, it sounds like we don't actually care about distance, just direction. So normalize the vector, which gives us the direction and a magnitude of 1.

 vector.Normalize();

Now we can add together all of the vectors we have, which will give a new vector for the player to move in. You'll want to normalize this one as well, I imagine.

You'll need to keep track of which enemies are in the trigger area, and calculate their to-player vectors accordingly. These will need to be saved to a class variable so you can iterate over them later.

Hope this helps!

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 vispan · Jul 01, 2016 at 08:02 AM 0
Share

@Habitablaba I am calculating this vector in OnTriggerStay2D() method so that as long as the enemy is inside the collider it will update the resultant vector each physics update. Will it update the resultant vector considering all the enemies inside the trigger collider. I have done that, but its seems that the character is not moving in the appropriate direction.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

playerprefs spawning errors. 1 Answer

Making a gameobject inactive and locating a clone. What's wrong with the script? 1 Answer

2D game kit Door isn't opening 3 Answers

Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites 1 Answer

How can I make that the waiting time in StartCoroutine will take effect when changing the value in inspector when the game is running ? 1 Answer


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