- Home /
 
               Question by 
               SrCienpies · May 16, 2020 at 09:54 PM · 
                2drotationmovement2d game  
              
 
              2D Zig-zag movement and rotation

Hello, I want to create this kind of movement, but i am not sure how to do it, at the momento i am doing the zig zag movement like this exmple
  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;
  }
The zig zag movement is ok, but i can't find the solution for the rotation that i desired.
What would be a googd solution for this issue?
 
                 
                zigzag-movemen.png 
                (59.3 kB) 
               
 
              
               Comment
              
 
               
              Answer by wewewu · May 17, 2020 at 05:59 AM
This might work for you:
     public float speed;
     public float frequency;
     public float magnitude;
     
 
     private void Update()
     {
         Vector3 pos = new Vector3(0f, Mathf.Sin((transform.position.z + Time.deltaTime) * frequency) * magnitude, transform.position.z).normalized;
         transform.LookAt(transform.position + pos);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
Hello. Thanks for the answer. This is very good on 3D games but in 2D it has some problems
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                