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 ecequalsm2 · Apr 14, 2016 at 06:48 AM · 2d gamepathfindingenemy aitransform.translatespeed issues

Adjusting enemy speed

Okay, so I think I've finally found a working script for 2D pathfinding. The only problem is that the enemy prefab I've attached it to is moving ridiculously too fast. Would appreciate any assistance on this one.

Here's the script:

 using UnityEngine;
 using System.Collections;
 
 
 
 public class Practice : MonoBehaviour {
 public Transform[] wayPointPath1 = new Transform[6]; //an array to store each waypoint
 
 int currentWayPoint = 0; //starting waypoint in the path
 
 float rotationSpeed = 6.0f; //waypoint rotation speed
 
 private float speed = 1f; //likely the speed variable to be deleted upon integration with EnemyControl script
 
 void Start () 
     {
     
     //find each waypoint object in the scene when the game is loaded
     wayPointPath1[0] = GameObject.Find("wp1").transform;
     wayPointPath1[1] = GameObject.Find("wp2").transform;
     wayPointPath1[2] = GameObject.Find("wp3").transform;
     wayPointPath1[3] = GameObject.Find("wp4").transform;
     wayPointPath1[4] = GameObject.Find("wp5").transform;
     wayPointPath1[5] = GameObject.Find("wp6").transform;
     
     }
   void patrol()
     {   
       Quaternion rotation = Quaternion.LookRotation(wayPointPath1[currentWayPoint].position - transform.position);
         
        //control how fast enemy can rotate when face the next 
       transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
         
         //movement
         Vector3 wayPointDirection = wayPointPath1[currentWayPoint].position - transform.position;
         float speedElement = Vector3.Dot(wayPointDirection.normalized, transform.forward);
         float accelerate = speed * speedElement;
         transform.Translate(transform.position.x, transform.position.y, Time.deltaTime * 0f);
         
     }
 }

Thanks again!

Comment
Add comment · Show 3
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 Le-Pampelmuse · Apr 14, 2016 at 11:40 AM 0
Share

Hi. Please edit your question and format your code using the 101010 button.

avatar image ecequalsm2 Le-Pampelmuse · Apr 14, 2016 at 05:26 PM 0
Share

Sorry about that. I just realized the 101010 button was how you paste code properly.

avatar image Le-Pampelmuse ecequalsm2 · Apr 17, 2016 at 09:26 AM 0
Share

Not a problem at all ;) Just a re$$anonymous$$der.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ecequalsm2 · Apr 15, 2016 at 05:44 AM

I actually did a little more searching after figuring out that the rotation aspect of my enemy movement was causing the biggest speed issues. Here's the new code that I came up with after combining a few other scripts, until I found what I really needed. For anyone doing 2d waypoint creation from scratch and rotation along the z-axis, this might be for you. FYI: I commented out the OnDrawGizmos function, but if you need to see where you placed your waypoints, the code is there and you can uncomment it. Also, FYI: You'll first want to create an empty GameObject (name it "Path" or something like that), then create empty children GameObjects and you can label them in order, as you please. Move the points wherever you want and then just drag them from the hierarchy into the slots on whatever GameObject you attach this script to. public Transform[] path; public float speed = 5.0f; public float reachDist = 1.0f; public int currentPoint = 0; private float rotationSpeed = 6.0f;

     // Use this for initialization
     void Start () {
     // I named each waypoint 'wp' + the corresponding number
     path[0] = GameObject.Find("wp1").transform;
     path[1] = GameObject.Find("wp2").transform;
     path[2] = GameObject.Find("wp3").transform;
     path[3] = GameObject.Find("wp4").transform;
     path[4] = GameObject.Find("wp5").transform;
     path[5] = GameObject.Find("wp6").transform;
     }
     
     // Update is called once per frame
     void Update () 
     {
             EnemyMove();
     }
     
     void EnemyMove()
     {
         
         Quaternion rotation = Quaternion.LookRotation(path[currentPoint].position - transform.position, transform.TransformDirection(Vector3.forward));
         transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
         
          float dist = Vector3.Distance (path[currentPoint].position, transform.position);
     
     transform.position = Vector3.MoveTowards (transform.position, path[currentPoint].position, Time.deltaTime * speed);  
     
         
     if(dist <= reachDist)
       {
         currentPoint++;
         
       }
     
     if(currentPoint >= path.Length)
          {
       Destroy(this.gameObject);
          }
         
     
     }
     /*
     void OnDrawGizmos ()
     {
         if(path.Length > 0)
         {
             for(int i = 0; i < path.Length; i++)
             {
                 if(path[i] != null)
                 {
                     Gizmos.DrawSphere(path[i].position, reachDist);
                 }
             }
         }
         
     } */
     
 }

Hope this helps!

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

Answer by carlqwe · Apr 14, 2016 at 11:09 AM

Firstly paste the code in the "code section" so the viewers can read it easier (its located above the texfield and is a button with numbers on.

And from what i can see you should change the speedElement float. Otherwise add an other float called speed that you use in the code INSTEAD of Time.deltaTime And change it after your prefrence

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 ecequalsm2 · Apr 14, 2016 at 05:27 PM 0
Share

Thanks @carlqwe I'll try that and let you know if it works. $$anonymous$$uch appreciated!

avatar image ecequalsm2 · Apr 14, 2016 at 05:37 PM 0
Share

Also, thanks for the heads up about the 101010 button.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy pathfinding script for a 2D top down shooter game 0 Answers

2d Pathfinding in Unity 5.6, how it works? 0 Answers

How create a Tunnel for gameobject to follow the tunnel path. 0 Answers

A* 2D Pathfinding not working with prefabs? 0 Answers

2D platformer AI 0 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