Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JokerMartini · Jul 24, 2012 at 06:53 PM · speedprojectiletarget

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)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BeHappy · Jul 25, 2012 at 04:36 AM

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(); 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BeHappy · Jul 25, 2012 at 04:51 AM 0
Share

// 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);

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Indirect Missile Path 4 Answers

How do I make a projectile arc and always hit a moving target? 4 Answers

Move object from A to B on sphere 2 Answers

How to ensure multiple weapons fire towards the same target? 1 Answer

Trying to destroy a projectile and "teleport" to another area of the screen when it hits a target. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges