• 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
Question by Praks87 · Apr 04, 2012 at 06:39 PM · aienemyshootingheightfire

Get enemy to fire at me from a high position

I am trying to get a enemy to fire at me from a watch tower however i do not know how to go about this. I have enemies on the ground that fire at me fine but I can seem to do this for the one on the watch tower. This is the code for the enemies who are firing at me on the ground-

// speed of the AI player public var speed:int = 4;

// speed the ai player rotates by public var rotationSpeed:int = 3;

// the waypoints public var waypoints:Transform[];

// current waypoint id private var waypointId:int = 0;

// the player private var player:GameObject;

// firing toggle private var firing:boolean = false;

// the Mesh Renderer of the Muzzle Flash GameObject private var muzzleFlash:GameObject;

/** Start */ function Start() { // retrieve the player player = GameObject.Find("First Person Controller");

 // retrieve the muzzle flash
 muzzleFlash = GameObject.Find("muzzleFlashAgent");
 
 // disable the muzzle flash renderer
 muzzleFlash.active = false;

}

/** Patrol around the waypoints */ function Patrol() { // if no waypoints have been assigned if (waypoints.Length == 0) { print("You need to assign some waypoints within the Inspector"); return; }

 // if distance to waypoint is less than 2 metres then start heading toward next waypoint
 if (Vector3.Distance(waypoints[waypointId].position, transform.position) < 2)
 {
     // increase waypoint id
     waypointId++;
     
     // make sure new waypointId isn't greater than number of waypoints
     // if it is then set waypointId to 0 to head towards first waypoint again
     if (waypointId >= waypoints.Length) waypointId = 0;
 }
 
 // move towards the current waypointId's position
 MoveTowards(waypoints[waypointId].position);

}

/** Move towards the targetPosition */ function MoveTowards(targetPosition:Vector3) { // calculate the direction var direction:Vector3 = targetPosition - transform.position;

 // rotate over time to face the target rotation - Quaternion.LookRotation(direction)
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 
 // set the x and z axis of rotation to 0 so the soldier stands upright (otherwise equals REALLY bad leaning)
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
 
 // use the CharacterController Component's SimpleMove(...) function
 // multiply the soldiers forward vector by the speed to move the AI
 GetComponent (CharacterController).SimpleMove(transform.forward * speed);
 
 // play the walking animation
 animation.Play("walk");

}

/** Update */ function Update() { // calculate the distance to the player var distanceToPlayer:int = Vector3.Distance(transform.position, player.transform.position);

 // calculate vector direction to the player
 var directionToPlayer:Vector3 = transform.position - player.transform.position;
 
 // calculate the angle between AI forward vector and direction toward player
 // we use Mathf.Abs to store the absolute value (i.e. always positive)
 var angle:int = Mathf.Abs(Vector3.Angle(transform.forward, directionToPlayer));
 
 // if player is within 30m and angle is greater than 130 (IN FRONT) then begin chasing the player
 if (distanceToPlayer < 30 && angle > 130)
 {
     // move towards the players position
     MoveTowards(player.transform.position);
     
     // if not firing then start firing!
     if (!firing) Fire();
 }
 // if player is within 5m and BEHIND then begin chasing
 else if (distanceToPlayer < 5 && angle < 130)
 {
     // move towards the players position
     MoveTowards(player.transform.position);
     
     // if not firing then start firing!
     if (!firing) Fire();
 }
 else
 {
     // patrol
     Patrol(); 
     
     // stop firing
     firing = false;
 }

}

/** Fire at the player */ function Fire() { // toggle firing on firing = true;

 // check if still firing
 while (firing)
 {
     // hit variable for RayCasting
     var hit:RaycastHit;
     
     // range of weapon
     var range:int = 30;
     
     // fire the ray from our position of our muzzle flash, forwards "range" metres and store whatever is detected in the variable "hit"
     if (Physics.Raycast(muzzleFlash.transform.position, transform.forward, hit, range)) 
     {
         // draw a line in the scene so we can see what's going on
            Debug.DrawLine (muzzleFlash.transform.position, hit.point);
         
         // if we hit the player
         if (hit.transform.name == "First Person Controller")
         {
             // inform the player that they have been shot
             player.GetComponent(PlayerShot).Shot();  
             
             // play gunshot sound
             audio.PlayOneShot(audio.clip); 
             
             // show muzzle flash for X seconds
             muzzleFlash.active = true;
             yield WaitForSeconds(0.05);
             muzzleFlash.active = false; 
             
             // wait a second or two before firing again
             yield WaitForSeconds(Random.Range(1.0, 2.0));
         }
     }
     
     // wait till next frame to test again
     yield;
 }

}

Comment

People who like this

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

i want to add fire shot at enemy gun explosion 1 Answer

How to make Enemy shoot at Player Top Down 1 Answer

Enemy fire script 1 Answer

When the enemy character shoots, the bullet won't go to the position of my player! 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