- Home /
This segment of code is causing problems
if(Input.GetButtonDown("Fire1") && !reloading && shot == "spread") { var i : int = 3;
while(i > 0)
{
clone = Instantiate(bullet, transform.position, transform.rotation);
i--;
}
ammo--;
}
If I remove the while loop the bullets shoot straight like normal. But I want to do a spread shot and this method is causing the bullets to spiral off the screen. Any ideas on how to do a spread shot would be appreciated.
I would assume the bullets themselves have a script that causes them to go forward? That script is probably what's causing the spiraling as there's nothing in this snippet that could cause such an issue.
You can use the &! operator ins$$anonymous$$d of doing && !someVar
&! isn't an operator, it's actually two. See the comments at the bottom of this page: http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx
Answer by Borgo · Feb 02, 2011 at 07:46 PM
Your script are creating 3 bullets instances at same time, position and rotation.
If your bullets have colliders this can be a problem because they will collide thenselves
Try to remove colliders if they have.
Looking at your script, I assume that the acceleration of the bullets is not in this script. Maybe it's in a script attached in the bullets, like Johan posted.
Answer by Ben 20 · Feb 02, 2011 at 08:15 PM
Ok the code segment that controls the acceleration is in another script that is attached to a bullet prefab.
var speed = 10.0; //Can be adjusted if needed
function Start() //Affects bullet accuracy { var acc : int = Random.Range(0, 6); transform.Rotate(0, acc, 0);
}
function Update () { var translation = speed; translation *= Time.deltaTime; transform.Translate(0, 0, translation); }
function OnCollisionEnter(hit : Collision) { if(hit.collider.gameObject.CompareTag("Wall") || hit.collider.gameObject.CompareTag("Enemy") ) Destroy(gameObject); }
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
While loop freezing Unity 3 Answers
Instantiate Code Issue 0 Answers
While Loop Not Assigning Array 1 Answer