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 Cheatsrichter2 · Apr 07, 2015 at 12:41 AM · raycastshooting

Make an Enemy Shoot through Raycast

Hello fellow developers,

How do I make an enemy shoot via Raycast? I tried for hours and I didn't came to a solution, even using allmighty Google I haven't found anything. I had some ideas, but I don't know how to realise these. The enemy should turn towards the player, then "shoot" a ray. Has anyone a better approach on doing that, or any helpful documentation?

Thank you in advance, everyone!

-Cheatsrichter-

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 Cheatsrichter2 · Apr 08, 2015 at 03:28 PM 0
Share

nvm solved it by using not this ray Ray ray = new Ray(transform.position, target.position); Ins$$anonymous$$d this: Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));

avatar image Cherno · Apr 08, 2015 at 04:35 PM 0
Share

... Yes? Is there any kind of question co$$anonymous$$g? Please give more information, what are you trying to do? Do you need help with using the Physics.Raycast function?

avatar image Cheatsrichter2 · Apr 09, 2015 at 07:01 AM 0
Share

No, there are no new questions, but thanks for your reply! Solved it through some trial and error.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MegaBomb_Unity · Oct 18, 2021 at 04:54 PM

How convenient. I almost successfully programmed my enemy raycast was planning to ask people how to add an ability to shoot upwards. Alright, so how about this, copy my code and see if you can find a way to shoot upwards using UnityEngine; using System.Collections; using UnityEngine.AI;

public class EnemyAiTutorial : MonoBehaviour { [SerializeField] Transform enemy;

 [SerializeField] NavMeshAgent agent;

 [SerializeField] Transform player;

 [SerializeField] LayerMask whatIsGround, whatIsPlayer;

 [Space]

 //Patroling
 [SerializeField] Vector3 walkPoint;
 bool walkPointSet;
 [SerializeField] float walkPointRange;

 //Attacking
 [SerializeField] float timeBetweenAttacks;
 [SerializeField] bool isShooting;
 bool alreadyAttacked;

 [Space]

 //States
 [SerializeField] float sightRange, attackRange;
 [SerializeField] bool playerInSightRange, playerInAttackRange;

 [Space]


 [SerializeField] float delaySeconds = 1.5f;
 [SerializeField] bool fire;
 [SerializeField] float range = 100f;
 [SerializeField] float damage = 20f;
 [SerializeField] Vector3 characterOffSet = new Vector3(0f, 0.3f, 0);
 [SerializeField] Vector3 aimOffSet = new Vector3(0, 1, 0);
 private LineRenderer laser;

 bool fireLaser;
 #region Awake & Update functions

 private void Awake()
 {
     player = GameObject.Find("Player").transform;
 }

 public void Start()
 {
     laser = GetComponent<LineRenderer>();
 }


 private void Update()
 {
     //Check for sight and attack range
     playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
     playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);

     if (!playerInSightRange && !playerInAttackRange) Patroling();
     if (playerInSightRange && !playerInAttackRange) ChasePlayer();
     if (playerInAttackRange && playerInSightRange) AttackPlayer();

     laser.SetPosition(0, enemy.transform.position + characterOffSet);
     if (!fireLaser)
     {
         laser.SetPosition(1, enemy.transform.position + characterOffSet);
     }
 }

 #endregion

 #region States
 private void Patroling()
 {

     if (!walkPointSet) SearchWalkPoint();

     if (walkPointSet)
         agent.SetDestination(walkPoint);

     Vector3 distanceToWalkPoint = transform.position - walkPoint;

     //Walkpoint reached
     if (distanceToWalkPoint.magnitude < 1f)
         walkPointSet = false;
 }
 private void SearchWalkPoint()
 {
     //Calculate random point in range
     float randomZ = Random.Range(-walkPointRange, walkPointRange);
     float randomX = Random.Range(-walkPointRange, walkPointRange);

     walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

     if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
     {
         walkPointSet = true;
     }
 }

 private void ChasePlayer()
 {
     agent.SetDestination(player.transform.position);
 }

 private void AttackPlayer()
 {
     //Make sure enemy doesn't move
     agent.SetDestination(transform.position);

     if (!isShooting)
     {
         Vector3 aimPlayer = player.transform.position + aimOffSet;
         transform.LookAt(aimPlayer);
     }

     if (!alreadyAttacked)
     {
         StartCoroutine(Shoot());
     }
 }
 private void ResetAttack()
 {
     alreadyAttacked = false;
 }

 #endregion


 IEnumerator Shoot()
 {
     float totalTime = delaySeconds + timeBetweenAttacks;

     isShooting = true;
     yield return new WaitForSeconds(delaySeconds);
     fireLaser = true;

     RaycastHit Hit;
     while (!alreadyAttacked)
     {
         Vector3 onward = transform.TransformDirection(Vector3.forward) * (range);
         Debug.DrawRay(enemy.transform.position, onward, Color.red, 1f);

         if (Physics.Raycast(enemy.transform.position, enemy.transform.forward, out Hit, range))
         {
             laser.SetPosition(1, Hit.point);
             Debug.Log(Hit.collider.name);

             if (Hit.collider.GetComponent<Damagable>() != null)
             {
                 Hit.collider.GetComponent<Damagable>().TakeDamage(damage, Hit.point, Hit.normal);
             }
             else
             {
                 Hit.collider.GetComponent<mapCollision>().TakeDamage(damage, Hit.point, Hit.normal);
             }

         } else
         {
             laser.SetPosition(1, onward);
         }

         alreadyAttacked = true;
     }
     isShooting = false;

     yield return new WaitForSeconds(totalTime);
     fireLaser = false;
     alreadyAttacked = false;
 }

}

Comment
Add comment · 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

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

Raycast damage script 2 Answers

change to raycast shooting instead of rigidbody shooting 1 Answer

Raycast Shooting problem 2 Answers

Raycast Shooting Help 1 Answer

NEED HELP WITH RAYCAST SHOOTING !!! 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