x


Shoot bullet and control its speed

I've attached an image to help explain show what I'm doing. So far I have the ray being cast when the user "fires" and whatnot. I have it then instantiate a prefab in the direction of the cast ray. How do i get the prefab to move in the direction of the ray/shot but with the ability to control its speed? Below are the snippets of code I'm using to generate my bullets. Aside from this my other question is how do I make the bullet know whether it hits and object or not?

I'm excited to hear back and get this going. Thank you guys for all your help so far.

This javascript is on my camera.

var Bullet : Transform;
var projectile :  Rigidbody;

function Start () {
}

function Update () {
 if(Input.GetButtonDown("Fire1"))
 {
 var shotDir = Input.mousePosition;
 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); // Construct a ray from the current mouse coordinates

 var instantiatedProjectile = Instantiate(Bullet,transform.position,transform.rotation);


        instantiatedProjectile.rotation = Quaternion.LookRotation(ray.direction);

     Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
     Debug.Log(shotDir);
   }
}

This javascript is on my prefab(bullet).

var speed = 300.0;

function Start () {

}

function Update () {
 var dir = speed * Time.deltaTime;
 transform.Translate(0, 0, dir);
}

alt text

target.jpg (47.2 kB)
more ▼

asked Jul 24 '12 at 06:53 PM

JokerMartini gravatar image

JokerMartini
15 4 9 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Simple example given in FPS Tutorial may answer your question. you can find the answers in the code snippet below.

function Fire () {
 if (bulletsLeft == 0)
 return;

 // If there is more than one bullet between the last and this frame
 // Reset the nextFireTime
 if (Time.time - fireRate > nextFireTime)
 nextFireTime = Time.time - Time.deltaTime;

 // Keep firing until we used up the fire time
 while( nextFireTime < Time.time && bulletsLeft != 0) {
 FireOneShot();
 nextFireTime += fireRate;
 }
}

function FireOneShot () {
 var direction = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;

 // Did we hit anything?
 if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
 if (hit.rigidbody)
 hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

 // Place the particle system for spawing out of place where we hit the surface!
 // And spawn a couple of particles
 if (hitParticles) {
 hitParticles.transform.position = hit.point;
 hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
 hitParticles.Emit();
 }

 // Send a damage message to the hit object 
 hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }

 bulletsLeft--;

 // Register that we shot this frame,
 // so that the LateUpdate function enabled the muzzleflash renderer for one frame
 m_LastFrameShot = Time.frameCount;
 enabled = true;

 // Reload gun in reload Time 
 if (bulletsLeft == 0)
 Reload(); 
}
more ▼

answered Jul 25 '12 at 04:36 AM

BeHappy gravatar image

BeHappy
238 14 22 30

// Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

Jul 25 '12 at 04:51 AM BeHappy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x358
x255
x243

asked: Jul 24 '12 at 06:53 PM

Seen: 774 times

Last Updated: Jul 25 '12 at 04:51 AM