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 MousePods · Jul 19, 2012 at 02:17 AM · charactercontrollersimplemove

SimpleMove with Chasing

I am trying to have the enemy chase the player. I am using SimpleMove with the enemy. Of course, if you do not use

"characterController.SimpleMove ();"

the collisions will not work.

How do I set the enemy to chase a player with that function with collision?

I do not want to use a rigidbody.

This is the code I use to attack, but the enemy goes right into the player even though the player has a charactercontroller. I move the character and he pops out and the collision works...?

transform.position = Vector3.Lerp ( transform.position, target.position, attackSpeed * Time.deltaTime );

Note: When I push the character up against the enemy, the collision works, but when I move the enemy against the player, it doesn't, why is that?

Thanks!

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
2
Best Answer

Answer by aldonaletto · Jul 19, 2012 at 02:53 AM

Chasing the player is the easiest part: just rotate the enemy in the player's horizontal direction and make it move forward. But any AI must do more than this: usually the enemy must stay in an idle state when the player is too far or not visible, doing nothing or walking on a path defined by waypoints; when the player enters the chase range and/or becomes visible, chase him; when reaching a minimum distance from the player (attack range), stop and attack him. The basic script is as follows:

var speed: float = 4.5; var turnSpeed: float = 5; var chaseRange: float = 80; var attackRange: float = 2.5; private var target: GameObject; private var controller: CharacterController;

function Start(){ controller = GetComponent(CharacterController); // cache the CharacterController target = GameObject.FindWithTag("Player"); // make sure the player is tagged "Player" }

function Update(){ // find the player direction: var dir = target.transform.position-transform.position; dir.y = 0; // make it strictly horizontal var dist = dir.magnitude; // measure the distance if (dist > chaseRange){ // player too far: // idle state: do nothing or make the enemy follow a path // based on waypoints } else if (dist > attackRange){ // player in chase range but still out of attackRange: // chase state: var rot = Quaternion.LookRotation(dir); // find target direction... // and rotate gradually to it transform.rotation = Quaternion.Slerp(transform.rotation, rot, turnSpeed*Time.deltaTime); controller.SimpleMove(transform.forward*speed); // chase him! } else { // attack state: // attack the player } } You can start with this basic script and gradually implement the idle and attack modes.

NOTE: About your last question: the CharacterController only check collisions when it's moving with Move or SimpleMove; if you move it by altering its position (setting transform.position directly) or move any other object over it, collisions are ignored.

Comment
Add comment · Show 2 · 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 MousePods · Jul 19, 2012 at 01:33 PM 0
Share

Thanks man! I really appreciate you and this community! I hope one day I will be good enough at program$$anonymous$$g to help others...but that will be awhile =/

avatar image kunalxigxag · Feb 02, 2015 at 12:41 PM 0
Share

Thanks. I had issues with rigidbody and wasnt able to find a character controller after searching a lot. yours worked.

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

Applying force to character inside trigger 2 Answers

CharacterController Move vs. SimpleMove 1 Answer

Character Controller Won't SimpleMove() 0 Answers

Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer

CharacterController.SimpleMove() doesn't work 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