Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AnimatorOfTheYear · Mar 29, 2016 at 06:41 PM · c#scripting problemenemy

Problem with Enemy AI

This question will probably solve itself as i search through out the internet but ill give it another shot.

So this time, my problem revolves around a Entity that i want to make in my game. This Entity, the Enemy, has basically basic functions on following the target but a bit lacking on its shooting part. It basically is going to follow its target until it goes close enough to it so it shoots. Half the part is done, with the enemy stalking the player until its in range. The problem i am having right now is that when it shoots it does the following:

  1. It doesn't shoot straight.

  2. Its fire-rate is really high. Even with the limitation on the script. (I probably have screwed something up)

(Here is what i deal with.. https://gyazo.com/0ff257d8260c74340fbbce6919188244)

I am not really sure how to solve this considering i am relatively new to scripting in C# but i would like to have some insight on this.

C# Code : using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour {

 public float playerTargetDistance;
 public float shootingRange;
 public float lifetime = 5f;
 public float speed = 20f;
 public float baseSpeed = 20f;
 public Transform playerTarget;
 public Rigidbody EnemyBullet;
 public Transform EnemyChamber;
 public float shotTime;
 public float intervalShots = 1.7f;


 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     if (shotTime > 0) {
         shotTime -= Time.deltaTime;
     }

     if(Vector3.Distance(transform.position, playerTarget.position) <= playerTargetDistance){
         transform.position = Vector3.MoveTowards(transform.position, playerTarget.position, speed);
         transform.LookAt(playerTarget);
         if(Vector3.Distance(transform.position, playerTarget.position) <= shootingRange){
             speed = 0;
             Shoot();
         }
         else
         {
             speed = baseSpeed;
         }
     }
 }

 void Shoot() {

     Rigidbody EnemyShot;
     EnemyShot = Instantiate (EnemyBullet, EnemyChamber.position, EnemyChamber.rotation) as Rigidbody;
     EnemyShot.AddForce (EnemyChamber.forward * 5000f);
     Destroy (EnemyShot.gameObject,lifetime);
 }


}

P.S: I might find the solution on the internet but at this rate, i wont have any luck finding the right post for it. So any helpful sites will help me a hole lot! :D

Comment
Add comment · Show 2
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 iHaveReturnd · Mar 29, 2016 at 06:48 PM 0
Share

In terms of the fire rate, you don't actually have anything limiting it unless I'm missing it. You have the if(shotTime) check, but that's not tied to anything else, so it's just making that comparison and then doing nothing with it. So you'll need to incorporate that into your check for if shooting is valid or not.

For it not shooting straight, I guess you mean it's going sideways? I would add force to the EnemyShot's forward, not the chamber's. Just make sure your EnemyShot's forward is the right direction when it spawns, which it looks like you probably have.

avatar image AnimatorOfTheYear · Mar 31, 2016 at 07:50 AM 0
Share

The script that i compiled is basicly what i used on my player. Basically what i mean is that the shotTimer that im using right now is being used the same way as the player and it works there.

But i might now know the mistake and since this problem was from like weeks ago, im gonna remake the scripts i had so far.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wizardwolfje · Mar 29, 2016 at 07:28 PM

Try this script it works for me but it doesnt shoot but only follow using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour { Transform tr_Player; float f_RotSpeed=3.0f,f_MoveSpeed = 3.0f;

 // Use this for initialization
 void Start () {
     
     tr_Player = GameObject.FindGameObjectWithTag ("Player").transform; }
 
 // Update is called once per frame
 void Update () {
     /* Look at Player*/
     transform.rotation = Quaternion.Slerp (transform.rotation , Quaternion.LookRotation (tr_Player.position - transform.position) , f_RotSpeed * Time.deltaTime);
     
     /* Move at Player*/
     transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;
 }

}

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 AnimatorOfTheYear · Mar 31, 2016 at 07:51 AM 0
Share

Ill try and tweek the script to my liking. Ill reply you with my script when im done. :D

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Score counter not working properly. 1 Answer

Vector3.Lerp not working properly, making the player bounce around 2 Answers

arrow from text to object - complete beginner 0 Answers

videoPlayer Script Help Needed 0 Answers

The laser problems 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