- Home /
How do I instantiate an object to a rounded position?
This is a GIF illustrating the problem. I am trying to make it so that the bullets are instantiating on a rounded position (to .0 or .5, preferably) At the moment, they spawn based on a rate of time which is okay because it gets it pretty close but it eventually falls out of sync. The meat and potatoes of the turret code is this block that operates in a Coroutine that cycles once every second:
//Instantiates the prefab at the current position and rotation. I assume I will have to do something here.
go = Instantiate(_bullet, (transform.position + new Vector3(0, 0, 0)), transform.rotation);
//This grabs the script component of the bullet and assigns a damage value.
Scr_Bullet sn = go.GetComponent<Scr_Bullet>();
sn.SetDamage(Damage);
//Gets the rigidbody of the instantiated prefab and sends it flying in the direction the turret is facing.
rb = go.GetComponent<Rigidbody2D>();
rb.AddForce(transform.rotation * new Vector2(100, 0));
Answer by Buckslice · Dec 22, 2017 at 03:31 AM
Rounding won't help the root cause of the problem I don't think. Eventually it would still get out of sync when based on time and you would end up just skipping a whole shot or something.
Idea 1: You could make a variable called like nextXSpawnPos or something start it at like 1.0. Once your current transform.position.x is past that, spawn a shot. Then add one to nextSpawnPos and continue. So next time youre past 2.0 then spawn another one etc. Have that checked in Update(). You would have to change once you reverse directions to the opposite of that code basically.
Better Idea 2: You could alternatively just set up little triggers below each platform and in the OnTriggerEnter2D() check if you hit them then spawn a shot like that. Prob easiest solution.
I think you're absolutely right about the turret eventually falling out of sync. I didn't want to string a bunch of triggers along the path of the turrets, though. $$anonymous$$y new code looks like this:
public float Distance = 0.0f;
public Vector3 lastPos;
// Sets lastPos as its point of origin.
void Start()
{
lastPos = transform.position;
}
// Distance increases as the position of the turret changes.
void Update()
{
Distance += Vector3.Distance(transform.position, lastPos);
lastPos = transform.position;
Fire();
}
// Whenever the Distance the turret has traveled becomes at least one game unit, the turret fires and sets Distance back down by one.
void Fire()
{
if (Distance >= 1)
{
Distance--;
// And then my code for firing the projectiles goes here!
}
It's not exact, but it's close enough. It also works backwards!
Answer by Kishotta · Dec 22, 2017 at 04:42 AM
If you double any floating point number, round it to an int, and then divide it by two, you will get a rounding to the nearest half.
For example:
1.2→2.4→2→1
3.14→6.28→6→3
5.6→11.2→11→5.5
You could make this easy with a helper function:
float RoundToHalf (float n) {
n = Mathf.Round (n * 2);
return n / 2f;
}
Your usage might be:
float rx = RoundToHalf(transform.position.x);
float ry = RoundToHalf(transform.position.y);
float rz = RoundToHalf(transform.position.z);
Vector3 instantiatedPosition = new Vector3 (rx, ry, rz);
You could probably simplify this with an extension method:
Vector3 RoundToHalf (this Vector3 v) {
return new Vector3 (RoundToHalf(v.x), RoundToHalf (v.y), RoundToHalf(v.z));
}
Usage:
Vector3 instantiatedPosition = transform.position.RoundToHalf();