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 Woody666 · Oct 30, 2016 at 06:30 PM · c#lerpmovetowards

Doesn't work MoveTowards and Lerp

Hello guys! Had a problem: got 2 classes: EnemyCreate:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyCreate : MonoBehaviour {
 
     public GameObject enemyObject;
 
     void Start () {
         StartCoroutine(CreatingEnemy());
     }
         
     void Update (){
         
     }
 
     IEnumerator CreatingEnemy()
     {
         while(true)
         {
             CreateEnemy();
             yield return new WaitForSeconds(1);
         }
     }
 
     void CreateEnemy()
     {
             GameObject enemy = (GameObject)Instantiate(enemyObject);
             enemy.transform.parent = this.gameObject.transform;
         }
     }
 }

and Enemy:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {    
     Transform[] wayPoint;
     float enemySpeed = 5.0f;    
     void Start () {
         GetWay();
     }
 
     void GetWay()
     {
         int waysCount = GameObject.FindGameObjectWithTag ("Ways").transform.childCount;
         int wayNumber = Random.Range (1, waysCount+1);
         int wayPoints = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild 
             ("Way" + wayNumber).childCount;
         // array of enemy way points
         wayPoint = new Transform[wayPoints];
         for (int i = 0; i < wayPoints; i++) {
             wayPoint [i] = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild 
                 ("Way" + wayNumber).transform.FindChild ("Point" + (i + 1));
         }
     }
 
     void Update()
     {
         
         transform.position = Vector3.MoveTowards (transform.position, wayPoint [0].position, enemySpeed * Time.deltaTime);
     }
 
 }


This doesn't work: transform.position = Vector3.MoveTowards (transform.position, wayPoint [0].position, enemySpeed Time.deltaTime);*

The code creates enemies with different ways to move, wayPoint[0] exist, all enemies has the same wayPoint[0] and start point(point where enemy instantiate). ATM I want to make enemy move just from start point to wayPoint[0].

Can someone help me?

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

1 Reply

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

Answer by LucianoMacaDonati · Oct 30, 2016 at 07:40 PM

Hello ! First of all, I noticed your EnemyCreate.cs will never stop creating enemies. Intended or not, this may make your debugging harder. I recommend creating only 1, testing it, and multiplying the results when they work.

I also see you have a lot going on in your "GetWay" function. I assume you have an Empty game object named "Ways" and you want your enemies to traverse those waypoints randomly, correct?

I would start by getting a reference to those waypoints on Start(), instead of looking for them every time you want a new waypoint. I would do this by tagging my waypoints, and then using your current MoveTowards code to move towards the next waypoint. When we get too close (you can change this to a bigger number like 0.01f) then we get another random waypoint from our references.

 GameObject[] waypointsRef;
 Vector3 destination;

 void Start()
 {
     waypointsRef = GameObject.FindGameObjectsWithTag("Waypoints");
     GetWaypoint();
 }

 Vector3 GetWaypoint()
 {
     destination = waypointsRef[Random.Range(0, waypointsRef.Length - 1)].trasnform.position;
 }

 void Update()
 {
     Vector3 source = transform.position;
     if ((destination - source).magnitude < Mathf.Epsilon)
         transform.position = Vector3.MoveTowards(source,destination, enemySpeed * Time.deltaTime);
     else
         GetWaypoint();
 }

Let me know how it goes.

Comment
Add comment · Show 4 · 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 Woody666 · Oct 30, 2016 at 10:14 PM 0
Share

@maca Thanks for your answer, I used GetWay() because I use 3 possible ways for enemy to go. It's look like this: Ways Its work properly.

The problem I had is that enemy doesn't move at all. I've add some logs to my code(class Enemy):

 void Start () {
         GetWay();
 
         Debug.Log ("Enemy actual position: " + transform.position);
         Debug.Log ("Position to go to: " + wayPoint [0].position);
     }
 
     void GetWay()
     {
         int waysCount = GameObject.FindGameObjectWithTag ("Ways").transform.childCount;
         int wayNumber = Random.Range (1, waysCount+1);
         int wayPoints = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild 
             ("Way" + wayNumber).childCount;
         // array of enemy way points
         wayPoint = new Transform[wayPoints];
         for (int i = 0; i < wayPoints; i++) {
             wayPoint [i] = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild 
                 ("Way" + wayNumber).transform.FindChild ("Point" + (i + 1));
         }
     }
 
     void Update()
     {
         Vector3 actualPosition = transform.position;
         transform.position = Vector3.$$anonymous$$oveTowards (actualPosition, wayPoint [0].position, enemySpeed); // * Time.deltaTime);
     }

And this logs show me this: Log

As you see, there is coordinates of enemy and coordinates to go to, but $$anonymous$$oveTowards and Lerp doesn't work. I don't understand what the problem can be here.

снимок-экрана-2016-10-30-в-225209.png (23.8 kB)
снимок-экрана-2016-10-30-в-230925.png (20.1 kB)
avatar image LucianoMacaDonati Woody666 · Oct 30, 2016 at 10:57 PM 0
Share

Now I see the problem: $$anonymous$$oveTowards will return the destination position if the difference between the start and destination ((destination - source).magnitude < maxDistanceDelta) is less than the step (more info here). In this case, there is only .5f on the z axis and that's not far away enough to move (in other words, you're already there).

In my example, I showed you how to get the next waypoint when you reached the first one. Your problem is that the enemy is already at the waypoint, and you're not updating that waypoint for your enemy to move to something that is further away.


On a separate note, I advice you to do something different (look the example I gave you) to get the waypoints list. I foresee that what you're currently doing will be a problem further on your development.

avatar image Woody666 LucianoMacaDonati · Oct 30, 2016 at 11:48 PM 0
Share

We don't understand each other, but I find a problem. The problem is that all time when I run the program, float enemySpeed from Enemy class equals "0". Thats why 3rd variable (enemySpeed*Time.deltaTime) in Vector3.$$anonymous$$oveTowards always be "0". Now I just make enemySpeed as public variable, and change the value of it in prefab Enemy in unity, and its work. I don't know exactly why this happens. Thanks @maca one more time for your answers!

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

252 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 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 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 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 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 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 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 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 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 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 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 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 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

How can I make smooth steps around an object? 0 Answers

MoveTowards and Lerp not working 1 Answer

Simple Advice needed - Lerp, SmoothDamp, SmoothStep...... etc. 2 Answers

Problems with Trigonometry and LERPing 0 Answers

Color.lerp for Spriterender is not smooth 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