- Home /
Instantiating Weirdly
I have a gun that is shooting bullets that spray the longer you shoot ( this question is the bases for what I'm doing http://answers.unity3d.com/questions/13003/fps-gun-accuracy-bullet-tracers.html). the only problem I have is that when I turn, the bullets start going spawning sideways from the gun until they reach about a 90 degree angle then they start coming back to the front of the gun. If I keep turning the same direction they move farther away from the gun again until they are at about the opposite 90 degrees again. If i keep rotating they move back to center and this keeps repeating. Something similar to a sine or cosine. Here is the script on the gun:
public string weaponName;
public GameObject bullet;
public Rigidbody instantiatedProjectile;
public int speed;
void shoot(float shotSpread ){
float vx = (1f - 2f * Random.value) * shotSpread;
float vy = (1f - 2f * Random.value) * shotSpread;
float vz = 1.0f;
Vector3 direction = transform.TransformDirection(new Vector3(vx,vy,vz));
GameObject bulletClone = Instantiate(bullet, transform.position, transform.rotation) as GameObject ;
bulletClone.SendMessage("direction",direction);
audio.Play();
}
and on the bullet:
public Vector3 dir;
void Update () {
transform.Translate(Vector3.forward + dir);
}
void direction(Vector3 direction ){
dir = direction;
}
the player has a script that all it does is send the shotSpread and calls the shot function on the gun when it shoots and shotSpread is just a float that increases as you hold the trigger. Pretty sure the problem isn't there so I've left it out for now. The gun is also a child of the camera so it follows it around. What is going on? is it with the Vector3.forward on the bullet? Any ideas for a workaround or a fix? Thanks in advance!
Answer by Berenger · Jun 17, 2012 at 03:42 AM
Couple things that need fixing.
First, SendMessage is a little expensive, you can set the function direction as public (or even better, make it a property) and then call bulletClone.GetComponent().direction = ...
Secondly, I doubt that the user is going to see the rotation of the bullet, unless it's reeeeeeeally slow, but for learning purposes you should make it look at that direction (LookAt, LookRotation, or forward =).
Then, you need to travel in that direction every frames. Vector3.forward is (0,0,1) in world coordinate, so when you turn it's lost. It goes like that (not finale version) :
private void Update(){
transform.Translate( tranform.forward ); // If you did the rotation
//transform.Translate( dir); // If you didn't, make sure it's normalized.
}
But this code is dependant of the frame rate, which mean it will travel really fast in good computers and really slow on shitty ones. To fix that, you need to multiply the vector by the delta time and a var to control it.
private void Update(){
transform.Translate( tranform.forward * Time.deltaTime * speed );
}
Finaly, know that you won't have collision detection that way. Do it, and when it looks nice look the doc for rigidbody.
the first thing is very helpful i hadn't realised that. the second thing i'm not sure i explained myself well. the path the bullet travels is what is making a larger angle between the gun and what i think should be forward the more i turn. The third thing I know and had that set up before when the bullet just shot straight but took it out while i was playing with the bullet spread. Thanks for the re$$anonymous$$der though!
Your answer
Follow this Question
Related Questions
Problem with ScreenToWorldPoint and touches (C#) 2 Answers
Distribute terrain in zones 3 Answers
Unity 2D Rouge-Like game tutorial help 0 Answers
Reflect bullets in 3D world space 1 Answer
Multiple Cars not working 1 Answer