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 ImThatGuy · Dec 11, 2015 at 03:15 PM · ai

Trying to get AI to follow who ever is closer

so im trying to get my enemy AI, to follow who ever is closer but all he does is just sit there...

SCRIPT:

JAVASCRIPT

var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player").transform; target1 = GameObject.FindWithTag("Player").transform; }

function Update () {

  var distance = Vector3.Distance(myTransform.position, target.position);
  var distance1 = Vector3.Distance(myTransform.position, target1.position);
  
  if(distance > distance1)
  {     
      if (distance<=range2 &&  distance>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance<=range && distance>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
  
   }
   
       if(distance1 > distance)
  {     
      if (distance1<=range2 &&  distance1>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance1<=range && distance1>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
  
   }
  

}

if any one can please help me it would be very helpful

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dhore · Dec 11, 2015 at 04:57 PM

You only seem to be changing the rotation of the object, if you actually want it to follow the other object then you need to be changing the position.

Note: when changing the position you can use Vector3.Slerp just like you are currently using Quaternion.Slerp for the rotation

Comment
Add comment · Show 6 · 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 ImThatGuy · Dec 11, 2015 at 06:14 PM 0
Share

now it is saying LookRotation is not valid

avatar image dhore ImThatGuy · Dec 11, 2015 at 06:20 PM 0
Share

Sorry, my bad on the wording.

Vector3.Slerp takes 3 parameters:

1) a Vector3 starting position

2) a Vector3 ending position

3) a float for how much to move on that frame (ie. the speed)

avatar image ImThatGuy · Dec 13, 2015 at 03:59 PM 0
Share

I'm not catching on... Could you rewrite this for me because I'm not understanding how to word it

avatar image dhore ImThatGuy · Dec 14, 2015 at 12:48 AM 0
Share

Sorry... I agree that could've been worded a lot better... I've also just noticed that your middle if statement for your range checks does actually include a transform.position change (not sure how I missed that last time..).

Looking at it again though, I think I've found your error:

 function Start() {
     target = GameObject.FindWithTag("Player").transform;
     target1 = GameObject.FindWithTag("Player").transform;
 }

The function GameObject.FindWithTag will return the first GameObject that it finds with that tag. Therefore no matter how many times you call it, it will always return the exact same GameObject. So your Start function has initialized target to be equal to the same object as target1.

This is where your issue begins; because then when you calculate distance and distance1 you are using the same GameObject and therefore both of the distances are equal to each other. And your code does nothing at all because you've set it up to only do something when one distance is greater than the other. Your code never does anything when they are equal.

avatar image ImThatGuy dhore · Dec 14, 2015 at 05:00 AM 0
Share

so should I create my own tags called Player1 and Player 2 and tag target as Player1 and target1 as Player2, would this work?

Show more comments
avatar image
0

Answer by ImThatGuy · Dec 14, 2015 at 04:10 PM

pragma strict

var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player1").transform; target1 = GameObject.FindWithTag("Player2").transform; }

function Update () {

  var distance = Vector3.Distance(myTransform.position, target.position);
  var distance1 = Vector3.Distance(myTransform.position, target1.position);
  
  if(distance > distance1)
   {     
      if (distance<=range2 &&  distance>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance<=range && distance>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
  
   }
   
   if(distance1 > distance)
  {     
      if (distance1<=range2 &&  distance1>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance1<=range && distance1>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
  
   }
   
   if(distance==distance1)
    {
        if (distance1<=range2 &&  distance1>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance1<=range && distance1>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
                 
    }

}

this is my script now but same problem, the enemy does not move

Comment
Add comment · Show 8 · 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 ImThatGuy · Dec 14, 2015 at 05:51 PM 0
Share

i added it where if the 2 players are the same distance apart to just go after player2

avatar image dhore ImThatGuy · Dec 15, 2015 at 04:10 AM 0
Share

Rather than copying and pasting the whole code and putting in another if statement, you could just use greater than or equal to like so: if(distance1 >= distance) and you should also be using else if for the second distance check as you'll never get a case where both evaluate to true.

I also just noticed a little typo that might be causing you some problems:

 if(distance1 > distance)
 {     
     if (distance1<=range2 &&  distance1>=range)
     {
         // rotations
     }
     else if(distance1<=range && distance1>stop)
     {
         // rotations
     }
     else if (distance<=stop) // <<----- wrong distance variable
     {
         // rotations
     }
 }

You are using the wrong distance variable in the last else if. That would be causing either $$anonymous$$or or major problems for you.

Other than those few things though, you're looking pretty good :) Hope it all works now!

avatar image ImThatGuy dhore · Dec 15, 2015 at 06:12 PM 0
Share

new problem now the enemy is looking back and forth at both of the players and staying directly in between us, i assume it has something to do with the if(distance==distance1)

avatar image ImThatGuy · Dec 15, 2015 at 08:58 PM 0
Share

And between if(distance > distance1) and if(distance1 > distance) you want me to place a "else if"

avatar image dhore ImThatGuy · Dec 16, 2015 at 07:00 AM 0
Share

What I meant was:

 if (distance > distance1)
 {
 // do stuff
 }
 else if(distance1 >= distance)
 {
 // do other stuff
 }
avatar image ImThatGuy dhore · Dec 18, 2015 at 08:42 PM 0
Share

pragma strict

var target : Transform; var target1 : Transform; var moveSpeed = 3; var rotationSpeed = 3; var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform;

function Awake() { myTransform = transform; }

function Start() { target = GameObject.FindWithTag("Player1").transform; target1 = GameObject.FindWithTag("Player2").transform; }

function Update () {

  var distance = Vector3.Distance(myTransform.position, target.position);
  var distance1 = Vector3.Distance(myTransform.position, target1.position);
  
  if(distance > distance1)
   {     
      if (distance<=range2 &&  distance>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance<=range && distance>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }     
   }
   
    else if(distance1 >= distance)
  {     
      if (distance1<=range2 &&  distance1>=range)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
 
      else if(distance1<=range && distance1>stop)
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance1<=stop) 
      {
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
          Quaternion.LookRotation(target1.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }     
   }

}

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make AI objects not walk on top of each other? 5 Answers

Enemy AI: Best way for decision handling? 1 Answer

Raycast from NPC to player problem 1 Answer

C# or Javascript for enemy AI 2 Answers

Making the AI move randomly around the player's last known location 1 Answer


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