Hi guys!
Here I want to make automatic shooting system and the bullet must move straight to the set direction. I tried lots of things i found around here but nothing worked… Here is the script im trying to make work now:
var dir = playerTarget.transform.position - transform.position
dir.Normalize();
var clone1 : Rigidbody;
clone1 = Instantiate(Bullet, Muzzle1.position, Muzzle1.rotation);
clone1.transform.Translate(dir * Speed * Time.deltaTime, Space.World);
So when i use that the object just spawns at random position, depending on the speed ,between the transform vector and target vector and freezes there. I tried Vector3.MoveTowards but i got the same effect. Any clues?
I wouldn’t use physical objects to shoot with. Even when you get this working, you’re going to run in to other problems such as collision with other objects as the bullet flies through the air as a high rate of speed. If you’re planning for the bullets to move really slowly, this will work, but aside from that, don’t use a physical object to shoot with.
Instead use raycasting. Raycasting basically shoots an invisible ray from a start point and moves straight to an end point. Whatever intersects the ray is the target when you press the button to shoot.
It’s a little more complex than just shooting an actual bullet and making it move forward, but rays are better. Bullets can move so fast they can miss collision entirely and pass through objects. Also, instantiating a bunch of bullets is a lot more resource intensive than using a ray.
For more information on raycasting, check out this: Unity - Scripting API: Physics.Raycast
If you really really want to just use bullets, you need to use:
clone1.transform.position += clone1.transform.forward * Time.deltaTime;
Then just make sure the bullet’s forward (pretty sure it’s the blue arrow on your move tool when using the Local option) is facing the way you want the bullet to move.