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 /
  • Help Room /
avatar image
0
Question by Alex_lemes · Aug 21, 2018 at 12:55 PM · directionrotation detection

Spawning a series of objects in-front of the player

Hello,

I'm trying to get a series of objects to spawn in front of the player based on the direction they are facing and so far it keeps spawning them on the Z-axis and not based on the player's direction. I've tried everything and I just don't know whats causing it not to work properly.

Currently, im using the transform.forward to get the direction and * it by a separation value + the original player positions but it's just not working.

I've looked into other similar issues and even when applying their solutions it doesn't work.

Any help is great! The code below, thanks!

  private void CalculatePath()
     {
         impactPositions.Clear();
 
         Vector3 initialPos = transform.position;
         Vector3 playerDirection = transform.forward;
 
         int sep = separation;
 
         for (int i = 0; i < amountOfImpacts; i++)
         {
             Vector3 spawnPos = transform.position + playerDirection * sep;
             impactPositions.Add(spawnPos);
             sep += separation;
         }
     }

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 eses · Aug 21, 2018 at 02:25 PM

Hi @alexlem_17

Note: Your "keeps spawning them on the Z-axis and not based on the player's direction." does not make sense based on your code. Your objects do spawn in front of player, but they just stack on each other. Also "sep += separation" does not make sense for me, see the solution below.

Also, I doubt you have used Debug draw ray. Have you at least tried instantiating GameObject and see where they end up?

You should visualize your vectors.

Your spawnPos is transform position.

You add to your spawn pos a "stick" pointing towards player forward direction.

Let's say player looks towards NE (1,0,1). This is normalized so that this vector is of length 1, so the vector tip is at roughly (0.7,0,0.7)

If your player is at 0,0,0 and you add this stick/vector to it:

You'll end up in (0.7,0,0.7)

But now you actually just multiply this by sep. If it is 10, then you'll end up each time at: (7,0,7)

So where is the scattering. If you just add to it, you are moving along line, further away along vector's line.

You should add direction multiplied by distance you want for scatter center to your player position. Then when you have this position, you should add some random position vector offset to this point. That way your new vectors give you random points around this point.

You could also take some other route to scatter items in front of player, but this is close to what you explained I think.

Comment
Add comment · Show 2 · 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 Alex_lemes · Aug 21, 2018 at 03:59 PM 0
Share

Hey, thanks for the reply

I've done a number of debug.rays to help figure things out and also the sep += separation is because I'm spawning multiple objects that have a pre-defined separation gap and this just adds the gap for each of them.

I'll try applying what I've understood from your answer, thanks!

avatar image Alex_lemes · Aug 21, 2018 at 04:14 PM 0
Share

In regards to the first part of your answer, my objects are not spawning on top of one another and do form the line with the amount of separation I want. It's just not going in the correct direction, maybe it's another part of my code if this part looks like it should go in the correct 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

151 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

Related Questions

Tilt/Rotation based on direction of movement? (player follows mouse hover) 0 Answers

move object in a certain direction 0 Answers

How to change the direction of the model? 1 Answer

Controlling Angle of Movement When Holding 2 Arrow Keys - 2d Top Down 0 Answers

Lerp between two direction vector 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