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 keating99 · May 11, 2015 at 06:11 AM · raycastaibullet

Need Help with Bullets

Hi all. I have a simple game where my enemy chases my player and tries to catch the player. I have created a weapon script and, have a raycast that is always pointing at my player. So I'm shooting a ray cast at the player all the time? What I would like to know is instead of having a raycast how can I have a bullet hitting my player instead? I think I have something almost working but even before the bullet reaches the player the player dies because I'm destroying the player with the raycast? Any suggestions?

At the moment my Ai shoots the player if I click the mouse something I need to get rid of I need the Ai to shoot independently? Any Suggestions?

 public class Weapon : MonoBehaviour {
 
     public float fireRate = 0;
     public float Damage = 10;
     public LayerMask whatToHit;
     public GameObject rabbit;
 
     float timeToFire = 0;
     Transform firePoint;
 
 
     // Use this for initialization
     void Awake () {
 
     rabbit = GameObject.FindWithTag("Player");
 
 
         firePoint = transform.FindChild ("FirePoint");
         if (firePoint == null) {
             Debug.LogError("No FirePoint");        
         }
 }
 
     void Update () {
 
     if (Input.GetMouseButtonDown (0)) {
 
                         if (fireRate == 0) {
                      
                                 Shoot ();
                         } else {
             
                                 return;
 
                         }
 
                 }
         }
 
 
         void Shoot(){
         Vector2 rabbitPosition = new Vector2 (rabbit.transform.position.x, rabbit.transform.position.y);
         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, rabbitPosition - firePointPosition, 100, whatToHit);
 
         Debug.DrawLine (firePointPosition, (rabbitPosition - firePointPosition) * 100, Color.cyan);
         if (hit.collider != null) {
             Debug.DrawLine (firePointPosition, hit.point, Color.red);
             
 
 
 }
 }
Comment
Add comment · Show 3
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 melkorinos · May 11, 2015 at 08:52 AM 1
Share

if you want your AI to shoot independently, create a timer so it shoots once every second or something like that? was that your question ?

avatar image keating99 · May 11, 2015 at 11:53 AM 0
Share

Hi $$anonymous$$elkorinos yes that is a nice Idea I will give that a go thanks. Can I ask toy one more small question.

I have applied an effect to the raycast so it makes a sort of bullet trail.

I can only get it to go to the right have you any ideas? How can I get it to fire in the direction the raycast is pointing?

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$oveTrail : $$anonymous$$onoBehaviour {
 
     public GameObject rabbit;
 
 
     public int moveSpeed = 230;
 
 
     void Awake(){
         rabbit = GameObject.FindWithTag("Player");
 
     }
 
     // Update is called once per frame
     void Update () {
 
         float rabbitPosition = rabbit.transform.position.x; // I want to shoot the bullets at the player
 
         transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
         Destroy (gameObject, 1);
     
     }
 
 
 }


avatar image melkorinos · May 12, 2015 at 01:25 PM 1
Share

the best way to send something towards a direction is to do Vector3 dir = (mytransform.positon - target.position) that vector 3 should be used ins$$anonymous$$d of Vecotr3.right

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by barbe63 · May 11, 2015 at 12:16 PM

Hello, you should make a gameobject bullet with a rigidbody and a collider and in a script attached on it, you can set somthing like:

 void OnCollisionEnter(collision other)
 {
     //make a way to recognize it's the player, that can also be setting your bullet to only collide the player with layers. Here I use the tag("Player")
     if(other.CompareTag("Player")
     {
         //your function to kill or damage the player
     }
 }

Then when you fire the bullet you want to call something like this

 //attach this script to the enemy or preferably the gun.
 //...
 public GameObject bulletPrefab;
 //...
 
 void FireBullet()
 {
     //before you need to assign the bulletPrefab value to your prefabed bullet in the inspector
     GameObject bullet = GameObject.Instantiate(bulletPrefab, transform.position + transform.forward, transform.rotation) as GameObject;
     float speed = 1000; // you might need to adjust this value for the desired speed
     bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward*speed);
 }
 

Keep also in mind that you should actually use pooling instead of instatiate your bullets. There are some tutorials about that. Watch this one: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

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 keating99 · May 12, 2015 at 06:05 PM 0
Share

Thank you

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding RayCastHit's Origin Position 2 Answers

2D Sending Object From One Place To Mouse Position. 1 Answer

Implementing BUG2 algorithm 0 Answers

AI Field of vision 1 Answer

Another Raycast Issue. 0 Answers


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