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 Saravia95 · May 22, 2014 at 05:42 AM · rotationraycastai

Find a new target when raycast triggers

I have an AI that finds a target location and walks to it then finds another, etc. This is so it can have a wandering effect. My problem I am having is that once the ray cast triggers for the AI being too close to a wall terrain, it doesn't find a new path. All it does it turn a 180 and the turns around again to continue the same path which is into a wall. I am trying to get the AI to 180 and at the same time find another path to walk, as if it were getting too close to a wall then changing direction to avoid the wall.

Any help would be greatly appreciated!

I need help with finding out what to put in my 'if' Statement where the raycast is being called.

Here is the code I am using for the AI:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class AIScript : MonoBehaviour
 {
 
 
 
     
     
     Transform _player;
 
     Transform _Raptor;
     [SerializeField]
     public float
         _moveSpeed = 5.0f;
     /*[SerializeField]
         float
                 Distance = 0.0f;*/
     [SerializeField]
     public float
         _gravity = 2.0f;
     public float _yVelocity = 0.0f;
 
 
     // Cached variables
     CharacterController _controller;
     protected Animator RaptorAnimator;
     Transform _transform;
     
     ChangeDirection _ChangeDirection;
         //Movement variables;
 
     float speed = 5f;
     float gravity = 100f;
     Vector3 moveDirection;
     Vector3 _target;
     float maxRotSpeed = 200.0f;
     float minTime = 0.1f;
     float _velocity;
     bool change;
     float range;
     bool chase = false;
     int RandomAnimation = 1;
     int OldRandomAnimation = 0;
 
     int choice = 0;
 
     
      
     
  
     RaycastHit hit;
     
     void Start()
     {
         _controller = GetComponent<CharacterController>();
         _transform = GetComponent<Transform>();
 
 
         RaptorAnimator = GetComponent<Animator> ();
         GameObject playerGameObject = GameObject.FindGameObjectWithTag ("Player");
         _player = playerGameObject.transform;
 
 
     
 
 
         range = 2f;
         _target = GetTarget();
         InvokeRepeating("NewTarget", 0.01f, 2.0f);
     }
 
 
 
     void OnTriggerStay (Collider inrange)
     {
 
         if (inrange.tag == "Player"&& _controller.isGrounded==false) 
         {
             //Debug.Log ("Not Touching");
             RaptorAnimator.SetBool ("RunDistance", true);
             _moveSpeed = 25;
             chase = true;
         }
 
 
 
     
     }
     void OnTriggerExit(Collider outofrange)
     {
         if (outofrange.tag == "Player"&& _controller.isGrounded==false) 
         {
             RaptorAnimator.SetBool ("RunDistance", false);
             _moveSpeed = 5;
             chase = false;
         }
     }
     
     void Update()
     {
         var fwd = transform.TransformDirection (Vector3.forward);
     
         Debug.DrawLine(transform.position, fwd , Color.red);
 
         if (Physics.Raycast (transform.position, fwd, out hit, 3) ) {
             //Debug.DrawLine(transform.position, fwd * 100, Color.red);
             //Debug.Log (hit);
             Debug.Log ("The line works");
 
             transform.Rotate(0,180,0);
             change = true;
             GetTarget();
 
             //Move();
         }else{
             
 
         }
 
         if(chase == false){
             if (change) _target = GetTarget();
             
             if (Vector3.Distance(_transform.position, _target) > range )
             {
                 Move();
                 RaptorAnimator.SetBool ("Idle", false);
 
             }else{
                 RaptorAnimator.SetBool ("Idle", true);
                 RandomAnimation = Random.Range(0, 10);
             //    RaptorAnimator.SetBool ("Animation Running", true);
 
                 if(RandomAnimation != OldRandomAnimation){
                 RaptorAnimator.SetInteger ("RandomAnimationIdle", RandomAnimation);
                 }else{
                 RandomAnimation =     Random.Range(0, 10);
                 }
             }
                 
 
             
             
             
 
 
         }else{
             RaptorAnimator.SetBool ("Idle", false);
             Vector3 direction = _player.position - transform.position;
             
             direction.Normalize ();
             
             Vector3 velocity = direction * _moveSpeed;
             
             
             
             
             if (!_controller.isGrounded) {
                 _yVelocity -= _gravity;
             }
             
             velocity.y = _yVelocity;
             direction.y = 0;
             transform.rotation = Quaternion.LookRotation (direction);
             _controller.Move (velocity * Time.deltaTime);
 
         }
 
     }
     
     void Move()
     {
         // Movement
         moveDirection = _transform.forward;
         moveDirection *= speed;
         moveDirection.y -= gravity * Time.deltaTime;
         _controller.Move(moveDirection * Time.deltaTime);
         //Rotation
         var newRotation = Quaternion.LookRotation(_target - _transform.position).eulerAngles;
         var angles = _transform.rotation.eulerAngles;
         _transform.rotation = Quaternion.Euler(angles.x,
                                                Mathf.SmoothDampAngle(angles.y, newRotation.y, ref _velocity, minTime, maxRotSpeed),
                                                angles.z);
     }
     
     Vector3 GetTarget()
     {
         return new Vector3(Random.Range(0, 300), 0, Random.Range(0, 300));
     }
     
     void NewTarget()
     {
 
         choice = Random.Range (0, 3);
         switch (choice)
         {
         case 0:
             change = true;
             break;
         case 1:
             change = false;
             break;
         case 2:
             _target = transform.position;
             break;
         }
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Jitter on Slerp - Raycast 0 Answers

How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer

How to get a vector rotation? 1 Answer

Enemy raycast detection 0 Answers

Finding RayCastHit's Origin Position 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