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 /
avatar image
0
Question by Chocolade · Mar 01, 2017 at 10:46 PM · c#scripting problemscript.

When the enemy move between waypoints if a waypoint is too big he will stuck on it how can i solve it ?

The problem is if there are two waypoints and the enemy need to get to the second waypoint so he stuck in the first one and it's all shaking the enemy and it's taking him some time until he can pass the waypoint and continue to the target waypoint.

For example if it's cubes the waypoints if i set the cubes size to 0.1 or 0.5 or to 1 it's all fine. But if i set the cubes size to 10-20-30 so if there are two waypoints on same route and the enemy need to get to the second waypoint he can't pass the first one to get to the second one. He stuck on it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 using UnityStandardAssets.Characters.ThirdPerson;
 
 public class WayPoints : MonoBehaviour {
 
     public GameObject[] waypoints;
     public Transform target;
     public float moveSpeed = 10f;
     public float moveSpeed1 = 10f;
     public float slowDownSpeed = 3f;
     public float reverseSlowDownSpeed = 3f;
     public float rotationSpeed = 1f;
     private Transform myTransform;
     private int targetsIndex = 0;
     private Vector3 originalPosition;
     private GameObject[] robots;
 
     public Transform reverseTarget;
     private int reverseTargetsIndex = 0;
     private Vector3 reverseOriginalPosition;
 
     void Awake()
     {
         myTransform = transform;
     }
     // Use this for initialization
     void Start()
     {
         waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
         robots = GameObject.FindGameObjectsWithTag("Robots");
 
         AddColliderToWaypoints();
         originalPosition = robots[0].transform.position;
         reverseOriginalPosition = robots[1].transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (MyCommands.walkbetweenwaypoints == true)
         {
             WayPointsAI();
             ReverseWayPointsAI();
         }
 
         DrawLinesInScene();
     }
 
     private void WayPointsAI()
     {
         if (targetsIndex == waypoints.Length)
             targetsIndex = 0;
         target = waypoints[targetsIndex].transform;
         float distance = Vector3.Distance(robots[0].transform.position, target.transform.position);
         robots[0].transform.rotation = Quaternion.Slerp(robots[0].transform.rotation, Quaternion.LookRotation(target.position - robots[0].transform.position), rotationSpeed * Time.deltaTime);
 
         //move towards the player
         if (distance < 30)
         {
             robots[0].transform.position += robots[0].transform.forward * slowDownSpeed * Time.deltaTime;
         }
         else
         {
             robots[0].transform.position += robots[0].transform.forward * moveSpeed * Time.deltaTime;
         }
         if (distance < target.transform.localScale.magnitude)
         {
             targetsIndex++;
         }
     }
 
     private void ReverseWayPointsAI()
     {
         if (reverseTargetsIndex == 0)
             reverseTargetsIndex = waypoints.Length -1;
         reverseTarget = waypoints[reverseTargetsIndex].transform;
         float distance = Vector3.Distance(robots[1].transform.position, reverseTarget.transform.position);
         robots[1].transform.rotation = Quaternion.Slerp(robots[1].transform.rotation, Quaternion.LookRotation(reverseTarget.position - robots[1].transform.position), rotationSpeed * Time.deltaTime);
 
         //move towards the player
         if (distance < 30)
         {
             robots[1].transform.position += robots[1].transform.forward * reverseSlowDownSpeed * Time.deltaTime;
         }
         else
         {
             robots[1].transform.position += robots[1].transform.forward * moveSpeed1 * Time.deltaTime;
         }
         if (distance < target.transform.localScale.magnitude)
         {
             reverseTargetsIndex--;
         }
     }
 
     void RandomWayPointsAI()
     {
         
     }
 
     void DrawLinesInScene()
     {
         // draw lines between each checkpoint //
         for (int i = 0; i < waypoints.Length - 1; i++)
         {
             Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue);
         }
 
         // draw a line between the original transform start position 
         // and the current transform position //
         Debug.DrawLine(originalPosition, robots[0].transform.position, Color.red);
         Debug.DrawLine(reverseOriginalPosition, robots[1].transform.position, Color.red);
 
         // draw a line between current transform position and the next waypoint target
         // each time reached a waypoint.
         if (target != null)
             Debug.DrawLine(target.transform.position, robots[0].transform.position, Color.green);
         if (reverseTarget != null)
             Debug.DrawLine(reverseTarget.transform.position, robots[1].transform.position, Color.green);
     }
 
     void AddColliderToWaypoints()
     {
         foreach (GameObject go in waypoints)
         {
             SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
             sc.isTrigger = true;
         }
     }
 }
 

I tried to solve it using magnitude but it didn't solve it. I tried before to use the target size / 2 but it didn't solve it either.

This is how i'm doing it now with magnitude

 if (distance < target.transform.localScale.magnitude)

But i'm not sure if this is the right way to solve it and it's not working good yet. Not solved the problem.

I want that if there are two waypoints on same line and he need to get to the second waypoint so he will go around the first waypoint. Instead he stuck on it's wall of the cube and then shaking stuttering and moving to the side then continue to the right waypoint.

And it's only happening when the waypoints(cubes) are very big.

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

298 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Why when using EditorWindow script type and MenuItem it's not add it to Hierarchy right click mouse context menu ? 0 Answers

Multiple Cars not working 1 Answer

How can i stop animation clip from being playing ? 1 Answer

How can i change the camera view when clicking a button ? 1 Answer

How can i prevent from moving the camera view under the terrain ? 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