Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Exalia · Sep 03, 2013 at 12:58 PM · aifollowartificial intelligence

Problems with simple AI script

Hi there I'm planning to put AI into my game so after some research I got something similar to what I want but it's still very buggy. The AI seems to only want to face the target's ass, so when I do a 180 turn with my player the AI spins around the player at the same distance. It's hard to explain without running it yourself but here it is.

 using UnityEngine;
 using System.Collections;
 
 public class AIScript : MonoBehaviour 
 {
     public GameObject target;
     public float moveSpeed = 20;
     public float rotationSpeed =5;
     private float distance;
     private float finalmove;
     private float finalrot;
 
     void Start()
     {
         finalmove = Random.Range(1, moveSpeed);
         finalrot = Random.Range(1, rotationSpeed);
         target = GameObject.FindGameObjectWithTag("Player");
     }
 
     void Update () 
     {
         distance = Vector3.Distance(target.transform.position, transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation,
         Quaternion.LookRotation(target.transform.position - transform.position), finalrot * Time.deltaTime);
         if (distance > 1)
         {
             transform.position += transform.forward * finalmove * Time.deltaTime;
         }
     }
 }

Any help would be appreciated, even if it involves writing a new script

Thanks in advance

Comment
Add comment · Show 5
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 Nexum1 · Sep 06, 2013 at 06:52 AM 0
Share

Hmmm.. Just to clarify, do you want the AI to remain static but keep looking at the player, and move forward toward him?

Also, for the looking-at-the-ass problem, you might consider placing an empty object that is parented to your player, to deter$$anonymous$$e the position where the AI should look. You should then give it a custom tag.. $$anonymous$$aybe "PlayerLookPoint" or whatever, just not the same as the player, then look for that tag when starting the game.

avatar image Exalia · Sep 06, 2013 at 10:06 AM 0
Share

I'm sorry If I misunderstood but if the AI is looking at the player and moving towards it doesn't that make it dynamic not static? :|

In any case I would like the AI to follow the player and look at the player.

I'll try that empty object thing when I get a chance to work on it :)

A side note, would you recommend creating an empty game object for most things like that? It sounds like a good way to avoid unnecessary errors.

avatar image Nexum1 · Sep 06, 2013 at 10:15 AM 0
Share

Sorry, yes, what I meant to ask.. Is it suppose to move in relation to the player or independently?

Also, Is the AI parented to the player? If the AI is parented to the player, the AI will move in relation with any movements that are happening on the player.

Yes, I do recommend it. I have been doing it for a few years now, with no performance problems, and if there is overhead I'm guessing it is decimal. You can do it that way, or you could set the vector's y axis up a bit. The reason I always pick this method is because you can set the exact position that the AI will look within the GUI and don't need to tweak the axis till it's perfect, plus if you choose to scale the object or rotate it or whatever, the Look Point will remain at the same relative position.

avatar image Exalia · Sep 09, 2013 at 11:00 AM 0
Share

So far it only moves in relation to the player.

The AI is not parented to the player.

Yeah it did sound like a good Idea when I first read it, Thanks for that :)

I'll try out some of the methods you suggested right now ^^

avatar image Exalia · Sep 16, 2013 at 10:10 AM 0
Share

Unfortunately none of these worked :(

I'd like the AI to slowly approach the player at all times.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Exalia · Sep 17, 2013 at 11:37 AM

Thanks for all the help everyone I eventually fixed it by using the script :

 gameObject.transform.LookAt(target.transform.position);
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
Comment
Add comment · Show 1 · 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 robhuhn · Sep 17, 2013 at 11:51 AM 1
Share

You could tweek the move speed and rotation speed values

If you're not slerping the rotation anymore, then that is exactly what I said :) So removing the slerp is just like an infinity speed value.

Good that it's working now.

avatar image
0

Answer by robhuhn · Sep 16, 2013 at 10:49 AM

The AI can only turn while going forward (like a car) and can't turn on the spot?

Not sure if I get you right but slerping the rotation with a given rotation/move speed ratio could result in a large turn radius, then it will never reach the target in some cases.

You could tweek the move speed and rotation speed values or set the rotation and position independently.

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 Exalia · Sep 16, 2013 at 10:54 AM 0
Share

Sorry I don't understand a lot of this, First I'm not sure if you're answering or asking a question. Secondly you mention that it is possible to result in a large turn radius, could you explain this a bit more? :)

I'm not sure if I'm tired or missing something but

I have a player I have an enemy

I want to slowly reduce the distance between the player and the target I want to make the enemy rotate and look at the player

That's all

avatar image robhuhn · Sep 16, 2013 at 11:18 AM 0
Share

I asked a question and gave a possible solution ;)

Did you try changing the move speed while the enemy gets closer?

 //distance replaces move speed in this case
 transform.position += transform.forward * distance * Time.deltaTime; 

  
avatar image Exalia · Sep 17, 2013 at 09:16 AM 0
Share

I like the change of speed that occurs but it doesn't stop the AI not wanting to be looked at, If I turn my character all the AI spin round and look at it's ass constantly, and If I walk too close to them they spin round to the back.

avatar image Exalia · Sep 17, 2013 at 09:23 AM 0
Share

None of the suggestions work I'll repost the script I am using.

AI will get to a certain distance and then flash or spin round my Player GameObject :(

     void Update () 
     {
         distance = Vector3.Distance(target.transform.position, transform.position);
         transform.rotation = Quaternion.LookRotation(target.transform.position - transform.position);
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
     }
avatar image robhuhn · Sep 17, 2013 at 09:38 AM 0
Share

I tested the script with my solutions and it works fine. $$anonymous$$ay be you have another scene set-up.

Show more comments
avatar image
0

Answer by ibrahim091234 · Sep 22, 2021 at 06:50 PM

man this is so amazing we dont even need a nav mesh agent cool man

i support this

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

19 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

Related Questions

Follow script Error NAN? 0 Answers

Horror Game AI script recommendation? 1 Answer

AI Script Problem, Horror Game 0 Answers

Unity3D AI using animations 1 Answer

Enemy Follow Player 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