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 davidflynn2 · Aug 21, 2013 at 05:12 PM · c#aireturn

Return to origional position.

I am working on an AI script and when the item ths spaceship is chasing leaves its collider I want the AI to return back to its original position. Also I am trying to find out if there is a way to make it not chase after it goes so far from original position.

Here is my script so far.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; 
 
 public class EnemyAi : MonoBehaviour 
 {
     //public Transform target;//Targets.
     public int moveSpeed;//Speed enemy can move.
     public int rotationSpeed;//Speed enemy can rotate.
     public List<Transform> targets = new List<Transform>();//List of targets.
     public int maxDistance;//Max distance it should get to us.
     public bool movingEnemy;
     public GameObject ThrusterLeft;
     public GameObject ThrusterRight;
     
     
     
     private Transform myTransform;//Save for the transform.
     
     void Awake()
     {
         myTransform = transform;//This saves our transform so we dont have to look it up all the time.    
         
     }
     
     
     void Update ()
     {
         if(targets.Count < 1) 
         {
             if(movingEnemy)
                     {
                         ThrusterLeft.SetActive(false);//If there is no more enemys turn off thruster.
                         ThrusterRight.SetActive(false);//If there is no more enemys turn off thruster.
                     }
             return;//If there is no targets sets targets to 0.
             
         }
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targets[0].position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
         
         if(movingEnemy)
             {
                         
             if(Vector3.Distance(targets[0].position, myTransform.position) > maxDistance)//Prevents the enemy from siting on top of us.
                 {
                     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;//Move the enemy towards us.
                     ThrusterLeft.SetActive(true);//Turn on thrusters for chase.
                     ThrusterRight.SetActive(true);//Turn on thrusters for chase.
                 }
                 else
                     {
                     ThrusterLeft.SetActive(false);//turn off thrusters we are not chasing anything.
                     ThrusterRight.SetActive(false);//turn off thrusters we are not chasing anything.
                     }
             }
     }
     
      void OnTriggerEnter(Collider other) //When somthing enters the trigger.
     {
         if(other.tag == "Player")//Check if it was tagged player.
         {
             targets.Add(other.transform);//If so add it to the targets list.
 
         }
         
         
     }
     void OnTriggerExit(Collider other) //When somthing exits the trigger.
     {
         if(other.tag == "Player")//Check if it was tagged player.
         {
             targets.Remove(other.transform);//If so remove it from the targets list.
                 
             
         }
         
         
     }
 }
 
Comment
Add comment · Show 1
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 davidflynn2 · Aug 21, 2013 at 06:25 PM 0
Share

I am trying to do it be telling it to go to an empty game object but it does it instantly ins$$anonymous$$d of over time.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · Aug 21, 2013 at 06:28 PM

Look at the example code here: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

I'm not saying you have to use Lerp, but look at how they make it move over time.

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 davidflynn2 · Aug 21, 2013 at 07:10 PM 0
Share

For some reason it is still just jumping back instantly.

myTransform.position = Vector3.Lerp (myTransform.position, oldPosition.transform.position, moveSpeed * Time.deltaTime);

avatar image flaviusxvii · Aug 21, 2013 at 09:00 PM 0
Share

Read the docs again...

The 3rd argument takes a value between 0 and 1. 0 for the start position. 1 for the end position. speed * deltaTime is not even CLOSE to what you want...

avatar image
0

Answer by Alpha_Guac · Oct 19, 2015 at 10:36 AM

have you tried making a transform that you set as the initial transform and set the enemy transform as the initial when you want it to return. as for not letting the AI move to far, have you tried using an if statement that compares the enemies transform to the initial transform and if it's too far it goes back to the initial position

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I more optimize My AI Script 0 Answers

How can I get a character to patrol and follow terrain? 1 Answer

Basic 2d AI follow? 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