I’m working on figuring out how to add some additional arrows to my current single shot setup and I’m just not familiar enough with angles and quaternions and all that to finish this off and was hoping someone could help. Below is my thought process. I have player P shooting at target T currently, I instantiate the arrow then I do some initialization stuff on the arrow and it shoots at T along the red line, this works perfectly. I would like to instantiate two more arrows that are offset to the left and right of the initial arrow, these would be on the blue lines below
This is my code for instantiating the arrows, the barrage shot is the new code that has no offset currently, its just firing on the same path as the first arrow.
public void initProjectile(Vector3 position, GameObject source)
{
transform.localScale *= size;
dir = position - transform.position;
dir.y = 0;
rb.velocity = dir.normalized * hSpeed;
}
I tried just adding a Vector3.one to the 2nd arrow, and subtracting a Vector3.one from the 3rd arrow and it looks like it works, but it gets weird based off how close or far I am from the target. Would appreciate any help getting this figured out.
Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.
This sort of coding is to be avoided at all costs unless you know exactly what you are doing.
If you run into an issue with any of these calls, start with the documentation to understand why.
There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.
In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.
It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.
void LaunchProjectiles(int count)
{
for (int i=0;i<count;i++)
{
float gapSize=5; // the amount of space between each projectile
Quaternion startRot=Quaternion.AngleAxis((-(gapSize*0.5f)*(count-1)+i*gapSize),Vector3.up);
GameObject obj=Instantiate(projectile,transform.position,startRot*transform.rotation);
obj.GetComponent<Rigidbody>().AddForce(obj.transform.forward*10,ForceMode.Impulse);
Destroy(obj,3);
}
}
Works great for shooting out of the front of my character, I tried passing in a position for shooting at the mouse cursor (got direction from transform to mouse position) but I’m getting all three arrows stacked now, with no rotation. Is there an easy fix that? Thanks!
void LaunchProjectiles(int count,Vector3 direction)
{
float gapSize=5; // the amount of space between each projectile
Quaternion midRot=Quaternion.LookRotation(direction,Vector3.up);
for (int i=0;i<count;i++)
{
Quaternion startRot=Quaternion.AngleAxis((-(gapSize*0.5f)*(count-1)+i*gapSize),Vector3.up);
GameObject obj=Instantiate(projectile,transform.position,startRot*midRot);
obj.GetComponent<Rigidbody>().AddForce(obj.transform.forward*10,ForceMode.Impulse);
Destroy(obj,3);
}
}
I think that’s it! Everything’s shooting at the cursor, has a nice spread and all. Not sure if its just because its midnight or movement but looks like rotation on the arrow is off a bit as it travels, but i’m probably just seeing things.
@zulo3d Super appreciate the help with this, thanks!