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 /
avatar image
0
Question by GC1983 · Feb 06, 2013 at 05:18 AM · instantiatemathf.sin

Spawn Objects In Different Locations With Mathf.Sin()

I have a series of spawn points that is randomly spawning my enemy objects.

                     var clone1 = Instantiate(shipObjects[Random.Range(0, 0)], 
                     shipSpawnPoints[Random.Range(0, shipSpawnPoints.Length)].transform.position, transform.rotation);

Im using the Mathf.Sin() to make the objects move in a wave motion. The issue is that is interfering with my instantiate array.

 count -= Time.deltaTime / waveTimer;
                 var offset = Mathf.Sin(Time.time * directionSpeed + timingOffset) * .3f;
                 transform.position = new Vector3(offset, .55f, count);

How do I bring these together so the objects will spawn on from the random spawn points and keep the wave effect?

Comment
Add comment · Show 2
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 robertbu · Feb 06, 2013 at 06:03 AM 0
Share

What do you mean by "interfering with my instantiate array?" Are you clones colliding? Or are the clones moving out of position so the wave effect is broken?

avatar image GC1983 · Feb 06, 2013 at 06:12 AM 0
Share

They spawn in the spawn point array and then immediately go to the transform.position for the $$anonymous$$athf.Sin(). Which is a single location upon wherever the Z location is. The other two are used for the Sin().

3 Replies

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

Answer by GC1983 · Feb 07, 2013 at 09:18 AM

Final answer:

 public GameObject[]                shipSpawnPoints;
 
 Vector3 startPosition;
 
     void Awake()
     {    
         shipSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
         startPosition = shipSpawnPoints[Random.Range(3,7)].transform.position;
     }
 
     void Update() 
     {
         transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
         transform.rigidbody.AddForce(xVect, yVect, zVect);
         
         if(isFanShip)
         {
              zPos -= Time.deltaTime / waveTimer;
                  float offset = Mathf.Sin(Time.time * directionSpeed + timingOffset) * .3f;
             transform.position = startPosition + new Vector3(offset,0, zPos);
         }
     }

Works beautifully!

Comment
Add comment · 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
2

Answer by Berenger · Feb 06, 2013 at 08:58 AM

Save the start position for each of them, then set the position like so :

 transform.position = startPosition + new Vector3(offset, .55f, count);

That way, the result of the sinus is only an offset. I guess that's what you had in mind, considering the variable name.

Comment
Add comment · Show 4 · 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 GC1983 · Feb 06, 2013 at 09:15 AM 0
Share

Im not sure what you are saying. The start positions are random. Another thing is that the spawn point code is in a different script that runs the over all game play. The mathf.sin is a script attached to the player.

avatar image Berenger · Feb 06, 2013 at 12:34 PM 0
Share

I get that. In the script attached to the spawned object (the one with the sin), in the function Awake(), assign the current position to a variable (startPosition).

avatar image GC1983 · Feb 07, 2013 at 07:40 AM 0
Share

I tried what you said and am getting no difference in results. Here's what I did to be sure we were clear.

 Vector3 startPosition ;
 
     void Awake()
     {    
         startPosition = shipSpawnPoints[Random.Range(3,7)].transform.position;
     }
 
 void Update() 
     {    
         if(isFanShip)
         {
              zPos -= Time.deltaTime / waveTimer;
                  float offset = $$anonymous$$athf.Sin(Time.time * directionSpeed + ti$$anonymous$$gOffset) * .3f;
             transform.position = startPosition + new Vector3(offset, .55f, zPos);
         }
     }
avatar image Sanjay1987 · Aug 13, 2020 at 10:02 AM 0
Share

@Berenger Thanks brother. Awesome answer. There couldn't have been a more better answer to the question that I was searching for. $$anonymous$$y instantiated prefab didn't position itself according to where I wanted it to instantiate. It always instantiated at (0,0). Been working on this for almost 5 hours and now glad I found this one.

avatar image
0

Answer by robertbu · Feb 07, 2013 at 08:12 AM

I'm trying to understand both what you want and what your code is doing or not doing. I took the snippets of code you have above and dropped them into Unity. I had to make up values for several of the variables because you did not post the code. So my result is:

 public class Spawner : MonoBehaviour {
 
     Vector3 startPosition ;
     float directionSpeed = 11.0f;
     float timingOffset = 1.0f;    
     float waveTimer = 0.5f;
     float zPos = 0.0f;
  
     void Awake() {  
        startPosition = Vector3.zero;
     }
  
     void Update() {  
         InvokeRepeating("Creator", 0.01f, 0.5f);
     }
     
     void Creator()
     {
         //if(isFanShip)
            {
         zPos -= Time.deltaTime / waveTimer;
            float offset = Mathf.Sin(Time.time * directionSpeed + timingOffset) * 3.0f;
          //transform.position = startPosition + new Vector3(offset, .55f, zPos);
             Vector3 v3Pos = startPosition + new Vector3(offset, .55f, zPos);
             GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
             go.transform.position = v3Pos;
             
            }
     }
 }

As you can see in the code, I'm creating a cube at each of the points you set the transform for the game object this script is attached to. When I run this script I get:

alt text

This is looking down from above. So with these values, the game object that has this script attached is walking the path in the image. Is this what you are looking for? This looks like what you described you wanted in the original question. If its not what you are looking for, what should happen? If is what you are looking for, what is happening instead?


wave.png (20.1 kB)
Comment
Add comment · Show 5 · 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 GC1983 · Feb 07, 2013 at 08:18 AM 0
Share

The sine wave is working fine as I wanted. The object moves downward and moves left and right like the picture above. What I want it to do is spawn from the array of spawn points. Ins$$anonymous$$d, it places it directly in the middle of the scene through the x axis. Ins$$anonymous$$d of spawning directly down the middle, want them to spawn randomly just above the camera view where I have the row spawn point objects.

avatar image GC1983 · Feb 07, 2013 at 08:21 AM 0
Share

I have a separate script that instantiates randomly to one of the several spawn points, but immediately after, it will move it to the middle because of the transform.position = new Vector(offset, .55f, zPos);. I want it to override that and make it pick one of the spawn points and then move in the downward sine motion from the random spawnpoint.

avatar image robertbu · Feb 07, 2013 at 08:52 AM 0
Share

The code above adds in the start position:

 Vector3 v3Pos = startPosition + new Vector3(offset, .55f, zPos);

If I make this change to my code above:

 void Awake() {  
    startPosition = Vector3.right*6.0f;
 }

The spawn point is moved to the right. And you were randomly generating startPosition in the code above. So I don't think the bug is in this routine but perhaps to logic elsewhere? Or perhaps it is how you generate your random spawn points? Perhaps you generated the relative to another object but are using them in world coordinates?

avatar image GC1983 · Feb 07, 2013 at 09:08 AM 0
Share

Vector3 v3Pos = startPosition + new Vector3(offset, .55f, zPos); breaks the sine motion and lets them spawn from their random points, but they are flying in a straight line.

avatar image GC1983 · Feb 07, 2013 at 09:16 AM 0
Share

Berenger was totally right from the start. I found the issue. I totally forgot to assign the array upon game start. It wasnt finding the index. I was overlooking that. Jeesh! Thanks all for your assistance.

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

11 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

Related Questions

Checking if object intersects? 1 Answer

Referencing instantiated player in camera's Start() 2 Answers

Instantiate 3 guiTextures at the same 1 Answer

Instantiate GameObject at Random time is not working 1 Answer

Instantiated Bullet Force not being applied 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