- Home /
Can't seem to make object move to other object.
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?
Answer by drex150 · Aug 04, 2014 at 08:46 PM
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: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
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.
You missed the point dude. Raycast doesn't work here for me and yes the bullet is moving slow and i prefer using rigidbody. So how can i make the bullet move to specific object, not just forward?