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 /
This question was closed Oct 28, 2015 at 10:58 AM by lengzai96 for the following reason:

Other

avatar image
1
Question by lengzai96 · Oct 28, 2015 at 03:49 AM · c#characterbehaviour

Charging Enemy behaviour?

So basically I'm trying to make an enemy that chargers at you and it if detects a change in your x direction he's supposed to pause and recalculate the direction you're in and charge that way instead.

here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class ChargingEnemyAI : MonoBehaviour {
 
     public GameObject Player;
     public float Velocity = 0;
     
     public Vector3 movement;
     bool isCharging = false;
     bool ischargeLeft = false;
     bool ischargeRight = false;
     // Use this for initialization
     void Start () {
     
     }
     
 
 
     // Update is called once per frame
     void Update () {
 
         Charge();
 
 
 
     }
 
     public void Charge()
     {
        if(isCharging == false)
         {
             if(Velocity >= 1)
             {
                Velocity -= 1;
             }
             else
             {
                 Velocity = 0;
             }
 
 
             Invoke("CalculateDirection",2);
         }
         else
         {
             if (ischargeLeft)
             {
                 if (Player.transform.position.x > this.transform.position.x || Player.transform.position.x == this.transform.position.x)
                 {
                     isCharging = false;
                 }
                 else
                 {
                     Velocity += 1;
                     movement.x = -0.5f * Velocity;
                     movement.y = this.transform.position.y;
                     movement.z = this.transform.position.z;
                     this.transform.position = movement;
                 }
 
            
             }
 
             else if (ischargeRight)
             {
                 if (Player.transform.position.x < this.transform.position.x || Player.transform.position.x == this.transform.position.x)
                 {
                     isCharging = false;
                 }
                 else
                 { 
                     Velocity += 1;
                     movement.x = 0.5f * Velocity;
                     movement.y = this.transform.position.y;
                     movement.z = this.transform.position.z;
                     this.transform.position = movement;
                 }
 
             }
 
         }
 
 
     }
 
     public void CalculateDirection()
     {
         if (Player.transform.position.x < this.transform.position.x)
         {
             ischargeLeft = true;
             ischargeRight = false;
             isCharging = true;
         }
         else if (Player.transform.position.x > this.transform.position.x)
         {
 
             ischargeRight = true;
             ischargeLeft = false;
             isCharging = true;
         }
 
     }
 
 }

Using this, the enemy seems to go towards my player and then start flickering left and right. Can anyone tell me what's wrong with my code?

Comment
Add comment · Show 1
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 Tolufoyeh · Dec 29, 2016 at 04:00 PM 1
Share

I see this post is really old, but do you still need a reply? Or if you already sorted it out, could you post your solution here?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Keeping an NPC character in front of a player character. 0 Answers

How to crouch a FPS Controller? 2 Answers

FPS Character not moving in relation to direction/rotation 0 Answers

MoveTowards is curving for no reason 0 Answers

How i do fix this error? error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement 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