unity3d - Position wall of arrows in front of player -
i having difficulty positioning wall of objects(arrows) in front of player. want solid wall of arrows shot in front of player perpendicular view. far have spawning of objects correct , y-axis placement correct. need z , x-axis alignment correct. code follows:
void run() { vector3 pos = transform.position; quaternion angle = transform.rotation; gameobject clone; float startx = pos.x; pos.y += 0.7f; pos.z += 2f; for(int y = 0; y < maxarrows; y++) { pos.y += 0.5f; for(int x = 0; x < maxarrows; x++) { pos.x -= 0.5f; clone = arrowpool.getarrowold(); if(clone != null) { clone.transform.position = pos; clone.transform.rotation = angle; clone.rigidbody.velocity = clone.transform.forward*force; } } pos.x = startx; } }
you're not taking account orientation of player when calculate positions of arrows. work in player's local coordinate space , transform world space.
first choose points in player's local coordinate space.
vector3[] points = { vector3(-1, 1, 0), // upper-left vector3(1, 1, 0), // upper-right vector3(-1, -1, 0), // lower-left vector3(1, 1, 0), // lower-right vector3(0, 0, 0) // centre };
now need transform these points world space. can using unity's transform::transformpoint function, multiplies passed point transform's localtoworldmatrix
:
for (int = 0; < 5; ++i) { points[i] = transform.transformpoint(points[i]); }
now have spawn positions arrows in world space.
Comments
Post a Comment