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 MegaBomb_Unity · Oct 18, 2021 at 09:00 AM · raycasttransformaienemy

Programming an enemy to shoot upwards

I am trying to program a RayCast enemy that shoots a laser. The problem with this code is that I can't get it to shoot upwards. I know it involves getting the y position of the player, but I just don't know how.

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
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

Answer by MegaBomb_Unity · Nov 07, 2021 at 02:18 AM

Took me a while but figured out the answer all by myself. Apparently, my code was inefficient and the AI would not look at the exact point where the player would be. What I did was I created a Vector3 for the player's position

 [SerializeField] Vector3 characterOffSet = new Vector3(0f, 0.3f, 0);
 [SerializeField] Vector3 aimOffSet = new Vector3(0, 1, 0);
 private LineRenderer laser;

 bool fireLaser;
 Vector3 target;

and after that, I made the vector 3 look for the player's position whenever it's not shooting and used transform.LookAt on the Vector3

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

     if (!alreadyAttacked)
     {
         StartCoroutine(Shoot());
     }
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

270 People are following this question.

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

Related Questions

Enemy raycast detection 0 Answers

Enemy AI Script Acting Abnormal 0 Answers

AI script works, when we add a blocked function it stops working HELP 0 Answers

Enemy AI Running Through Wall With Raycast 1 Answer

[SOLVED] using var from another method? 2 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