- Home /
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?
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?
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().
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!
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.
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.
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).
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);
}
}
@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.
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:
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?
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.
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.
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?
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.
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
![](https://koobas.hobune.stream/wayback/20220613092230im_/https://answers.unity.com/themes/thub/images/avi.jpg)