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 /
This question was closed Oct 21, 2013 at 02:07 AM by Benproductions1 for the following reason:

Question is off-topic or not relevant. Also a duplicate and asking for code!

avatar image
0
Question by GraviterX · Sep 30, 2013 at 01:18 AM · vector3.slerp

Monster AI chase and collider

Hey all! I recently got this script to make my monster in my horror game wander, but it keeps walking into walls. I want to make it so when it touches a wall, it turns and moves. I also want it to when a player gets close, it attacks and chases them. I am new to Unity so if anyone has anything helpful please say so thanks!

pragma strict

 /// <summary>
 /// Creates wandering behaviour for a CharacterController.
 /// </summary>
 @script RequireComponent(CharacterController)
  
     var speed:float = 5;
     var directionChangeInterval:float = 1;
     var maxHeadingChange:float = 30;
  
     var heading: float=0;
     var targetRotation: Vector3 ;
  
     function Awake (){
  
         // Set random initial rotation
         transform.eulerAngles = Vector3(0, Random.Range(0,360), 0);  // look in a random direction at start of frame.
  
         //StartCoroutine
         NewHeadingRoutine ();
     }
  
     function Update (){
         var controller : CharacterController = GetComponent(CharacterController);
  
         transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
         var forward = transform.TransformDirection(Vector3.forward);
         controller.SimpleMove(forward * speed);
     }
  
     /// <summary>
     /// Repeatedly calculates a new direction to move towards.
     /// Use this instead of MonoBehaviour.InvokeRepeating so that the interval can be changed at runtime.
     /// </summary>
     while (true){
         NewHeadingRoutine();
         yield WaitForSeconds(directionChangeInterval);
     }
  
     /// <summary>
     /// Calculates a new direction to move towards.
     /// </summary>
     function NewHeadingRoutine (){
         var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
         var ceil  = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
         heading = Random.Range(floor, ceil);
         targetRotation = new Vector3(0, heading, 0);
     }
Comment
Add comment · Show 3
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 getyour411 · Sep 30, 2013 at 01:58 AM 0
Share

For the walls, you'll need to figure out how this script will know what a "wall" is. Are you Raycasting or using colliders/triggers on your walls?

avatar image GraviterX · Sep 30, 2013 at 03:26 AM 0
Share

I think colliders... I'm very new to Unity and I'm only 14. The wall has a box collider and I have no idea what ray casting is to be honest

avatar image meat5000 ♦ · Oct 20, 2013 at 09:36 AM 0
Share

http://lmgtfy.com/?q=Unity+raycast

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Vector3.Slerp bumping problem 1 Answer

How can I get the position of Input.touches in World Space? 1 Answer

Vector3.Slerp is not working help. 2 Answers

Make Object smoothly follow player if out of near range 1 Answer

My character isn't moving 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