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 xanderdagr8 · Feb 17, 2014 at 12:43 AM · movetowards

How to make a character zig-zag while moving towards a moving player

I am working on a 2D shooter and I want one of the types of enemies to spawn and move towards the player while zig-zagging back and forth. The player could be moving, so it needs to dynamically change its direction and the direction of the zig-zagging. It shouldn't be simply zig-zagging on the x or y axis, but rather side to side relative to the player. How would I do this?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ZouBi27 · Feb 17, 2014 at 02:11 AM

A smarter way is to use the cos function.

 Vector3 position = new Vector3();
 float t = 0f;
 
 void Update {
    position.x = AMPLITUDE * Mathf.Cos(t);
    position.y += SPEED * Time.deltaTime;
 
    t += 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 erick_weil · Feb 17, 2014 at 12:50 PM 0
Share

this use $$anonymous$$us code for zigzag but no derivates the player rotation...

avatar image
0

Answer by erick_weil · Feb 17, 2014 at 02:05 AM

you need to derivate the local x axes of your player, or the local y axe... you do this by calling the transform.right for x axis, or transform.up to y axis

try this:

 var zigzag :Vector2;
 var timer : float;
 var player : GameObject;
 function Update()
 { 
 timer += Time.deltaTime;
 if(timer<0.5)
 zigzag = 3*player.transform.right;
 if(timer>=0.5&&timer<1)
 zigzag = -3*player.transform.right;
 if(timer>1)
 timer=0;
 transform.position += zigzag*Time.deltaTime; 
 
 }

or if you need the zigzaging follow the x axis of the enemy, use "transform.right" in the zigzag multiplier.

Comment
Add comment · Show 3 · 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 BenRiga · Feb 17, 2014 at 04:08 PM 0
Share

But if the object you are moving towards is also moving then it could be at any point in 2d space so you can't just choose x or y. The object could be below or above, left or right or at any angle in between. How would you calculate the X or Y if that is the case? Seems like a tough problem. Curious to see how this is solved.

avatar image erick_weil · Feb 17, 2014 at 08:53 PM 0
Share

I have not just choose x or y, I choose the local x of the object, this CHANGE with object rotation, and to the zigzag object moves towards too, only need to add this line to script:

 transform.position += speed*transform.forward*Time.deltaTime;

to the object follow the player, add this line :

 transform.LookAt(player);
avatar image BenRiga · Feb 17, 2014 at 09:04 PM 0
Share

Thanks, Erick. That is very cool. I'm new at Unity so was not even aware that there was an x and y local to an object. Of course it makes total sense now that I know. I need to go read up on that. Thanks for sharing.

avatar image
0

Answer by jakirhossain · Feb 03, 2020 at 05:38 PM

I was able to do it like this way:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class VirusMove : MonoBehaviour
 {
 
     public float speed = 2;
     float frequency = 10.0f; // Speed of sine movement
     float magnitude = 0.5f; //  Size of sine movement
 
     Vector3 pos;
     Vector3 axis;
 
     private void Awake()
     {
         pos = transform.position;
         axis = transform.up;
 
     }
 
     void Update()
     {
         pos += Vector3.left * Time.deltaTime * speed;
         transform.position = pos + axis * Mathf.Sin(Time.time * frequency) * magnitude;
     }

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 RabieF · Feb 21, 2021 at 07:17 PM

for those who want to enable collision use this instead:

     pos += Vector3.up * Time.deltaTime * speed;
     rb.MovePosition(pos + axis * Mathf.Sin(Time.time * frequency) * magnitude);
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

22 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

Related Questions

moving a platform 2 Answers

Moving GameObject Towards Spot when clicking a GUI Function 1 Answer

raycast isn't working in my movetowards script. 1 Answer

Does MoveTowards accept Physic Material? 1 Answer

Moving an UI element from point A to point B 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