#Spawn help
1 messages · Page 1 of 1 (latest)
Oh my bad. It says it in the message. I guess this is a function. So you need to use it like Perpendicular().
Just add brackets so that it calls the function
yeah I tried that but still doesn't like it 😦
Oh wait
Second
Okay I opened the documentation https://docs.unity3d.com/ScriptReference/Vector2.Perpendicular.html
Oh it's a static function. Sorry, I haven't used it before.
So
Vector2 v = new Vector2 Perpendicular(firePoint.transform.forward.x, firePoint.transform.forward.z); This was almost correct
But add a dot before Perpendicular
and remove new
So we are just calling a stataic function on Vector2
It's just a function call, but it is in the Vector2 class
Then it tells me that it doesn't exist haha
So we do Vector2 v = Vector2.Perpendicular(firePoint.transform.forward.x, firePoint.transform.forward.z);
Does that work?
it does not
When we look at the documentation we see public static Vector2 Perpendicular(Vector2 inDirection);
Vector2 v = Vector2.Perpendicular(firePoint.transform.forward);
So I guess we need to pass in a vector
Okay then we need to do Vector2 v = Vector2.Perpendicular(new Vector2(firePoint.transform.forward.x, firePoint.transform.forward.z));
that works
ok so now we have a Vector2 that is perpendicular to my firePoint position
which as I am understanding it would mean it looks like this
what are we trying to solve here?
It is perpendicular to the forward vector. So v should be perpendicular to the direction that the gun is pointing.
I draw ugly, but imagine it like this
I am spawning multiple rigidbodies in the same place using a for loop and i want to offset them from each other so they don't cause issues
firePoint*
riiight that makes sense
that looks like just firePos.right?
Oh, I guess that is a quicker way of getting the perpendicular vector 😄
is that all cases or a unspecified amount of projectiles?
so you can upgrade the projectile type to spawn more projectiles in the game and before I was just spawning them all on top of each other and letting unity physics push them apart but it causes unpredictability
so we have 2 "major" cases here: even amount of projectiles meaning nothing goes straight forward and uneven with 1 going forward and the others to the side
that's right yeah
which is where var offset = i - (0.5f * projectileAmount); comes into play
that would offset the first projectile by -0.5 if projectileamount is 1. doesnt sound right
is the problem just the position or also the rotation?
I am trying to just get position working atm
is it a fixed amount of 1, 2 or 3 projectiles or can the amount totally vary?
the total can vary
if I do Math.Ceil(i - (0.5f * projectileAmount))
then it would solve the problem when it is at 1
If the gap between the bullets is gap, then var range = (i - 1) * gap; and var offset = firePoint.position - range/2 + i * gap;?
By range, I mean the length from the leftmost to the rightmost bullet.
But I wrote it like an idea, we need to change it for the direction
but direction is same idea, have an angle similiar to gap and instead of having the position as a starting point you use the forward and rotate it by a calculated angle dependend on index
one of my main hurdles here is I am unsure how to add to the position
how do I add a float to a Vector3
Isn't is just with +?
just make it a vector3 with the other parts 0
at least i assume you want to move on 1 axis?
honestly not really^^
lol when it is rotated it seems like the axis are in world space not local
even though the transform is a child of the gun
firePos.TransformPoint(Vector3.right * offset);
var gap = firePoint.right.normalized;
var startingPos = firePoint.position - gap*(bulletsCount-1)/2;
for(int i = 0; i < bulletsCount; i++) {
var spawnPos = startingPos + i*gap;
}
This should be pretty close to the final code for the position. And it looks pretty clean if it works.
ok so I can just manipulate it on the z-axis in localPosition?
Oh wait, the bullets are children of the gun?
no the firepoint is a child of the weapon
Then yes
now they're just spawning in the middle of the world lol
this works omg I didn't see it
Yayy
thanks a ton