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 Vekosava · Dec 23, 2016 at 08:47 PM · charactercontrollercharacter controllerlimitsimplemove

Character Controller keeps going even if I limit it

I'm working on simple enemy AI, and now I'm forced to use Character Controller Component because terrain is not flat so It will prevent enemy from doing stutter movement.

Now problem is even when I limit it, so when enemy movement is on desired distance from player, it should stop, but character controller forces its way through player. Only way to really stop it is to limit calling that function, which is not what I want.

Code:

 using UnityEngine;
 
 public class Enemy : Entity {
 
     [SerializeField]
     private Transform target;
 
     public float moveSpeed;
 
     public float rotationSpeed;
 
     public float combatRadius = 10.0f;
     public float minDistance = 5.0f;
 
     private float distance;
 
     public bool isOnMinDistance;
     public bool seePlayer;
 
     private Transform myTransform;
 
     private CharacterController controller;
 
     private void Start () {
 
         GameObject target = GameObject.FindGameObjectWithTag ("Player");
         this.target = target.transform;
 
         myTransform = transform;
 
         controller = GetComponent<CharacterController> ();
     }
 
     private void Update () {
 
         if (reaction == Reaction.Hostile) {
             ChasePlayer ();
         }
     }
 
     private void ChasePlayer () {
 
         distance = Vector3.Distance (target.position, myTransform.position);
 
         if (distance <= minDistance) { //attack
             isOnMinDistance = true;
             seePlayer = true;
         }
 
         else if (distance >= minDistance && distance <= combatRadius) {
 
             Debug.DrawLine (new Vector3 (target.position.x, target.position.y + 1f, target.position.z), myTransform.position, Color.red);
 
             LookAtTargetYAxis ();
 
             FollowTarget ();
     
             isOnMinDistance = false;
             seePlayer = true;
         }
 
         else {
             isOnMinDistance = false;
             seePlayer = false;
         }
     }
 
     private void FollowTarget () {
         Vector3 forward = transform.TransformDirection(Vector3.forward * moveSpeed * Time.deltaTime);
 
         controller.SimpleMove (forward);
     }
 
     private void LookAtTargetYAxis () {
         
         transform.LookAt (target);
 
         Vector3 angles = myTransform.rotation.eulerAngles;
         angles.x = 0;
         angles.z = 0;
 
         transform.rotation = Quaternion.Euler(angles);
     }
 
     private void OnDrawGizmosSelected () {
         Gizmos.DrawWireSphere (transform.position, combatRadius);
         Gizmos.color = Color.red;
     }
 }

I would appreciate if someone could help me. :)

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 Fieora · Dec 24, 2016 at 11:42 PM

If they are within the min distance have a boolean change to true and use that to reduce the velocity.

I'm assuming here that the script is attached to each enemy and they have a RigidBody (2D or 3D don't really matter you just have to change the Vector type)

in your update wrap the enemy speed you are using to move to the player with this if:

 if(!isOnMinDistance && seePlayer){
 //move full speed ahead
 }

Add this else after that:

     else if(isOnMinDistance){ //target is in striking range
     GetComponent<RigidBody>().velocity = .8f * GetComponent<RigidBody>().velocity;
     //This line takes the velocity on the rigid body and reduces it by 80% each frame, giving a smooth playback.
     //do strike logic
     }
 

The only thing left is to flip the boolean based on if the enemy is in range where the check is for the distance

It looks like you just have a speed variable running the show here, so you can use the same strategy here, Just take the speed and set it to max if they are outside of min distance, and if they are in min distance, reduce it by a percent. Since it runs every frame you want to use a higher number for a smoother stop.

 if(isOnMinDistance)
 {
        speed = speed * .8;
 }
 else
 {
  if(speed < maxSpeed)
        speed = speed * acceleration;
 else
       speed = maxSpeed;

}

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

70 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

Related Questions

Character Controller move overrides transform.position 0 Answers

Boat Controller Help 0 Answers

Help with exception to DontDestroyOnLoad 2 Answers

Hinge joint limits not working properly 1 Answer

CharacterController getting stuck on slope corners 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