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
1
Question by cagudo · Apr 21, 2014 at 07:22 AM · c#movementsine-wave

Bat sine movement 2d Game help

I have been trying to get one of the enemies in my 2d game to make a movement similar to the one described in the picture below:

alt text

Any idea on how to make a C# script for this movement?

Thanks in advance,

new doc_1 (16).jpg (460.5 kB)
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

3 Replies

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

Answer by vexe · Apr 21, 2014 at 07:59 AM

Hi - So I wrote this movement script in one of our jams. It's object-oriented so you could easily create and plug-in other types of movements like Linear, Circular, etc.

Here's the base movement class, from which you could inherit, override GetY to get your custom movement behaviours:

 using UnityEngine;
 
 public abstract class BaseMovement : MonoBehaviour
 {
     public float minSpeed = 20f;
     public float maxSpeed = 100f;
     public float minInitialOrientation;
     public float maxInitialOrientation;
 
     protected float speed;
     protected Vector3 velocity;
     protected Vector3 direction;
     protected Transform mTransform;
     protected Transform cachedTransform { get { if (!mTransform) mTransform = transform; return mTransform; } }
     
     protected virtual void Move()
     {
         velocity.y = GetY();
         cachedTransform.localPosition += (velocity + direction) * Time.deltaTime * speed;
         ReactToEdges();
     }
     
     protected virtual void ReactToEdges()
     {
         Vector3 viewPos = Camera.main.WorldToViewportPoint(cachedTransform.position);

         // Ping pong between the edges of the screen. If we touched an edge, we reverse the movement direction
         if (viewPos.x <= 0 || viewPos.x >= 1) // left/right edges, reverse the x coord for the direction vector
             direction.x = -direction.x;
             
         if (viewPos.y <= 0 || viewPos.y >= 1) // bottom/upper edges, reverse the y coord of the direction vector
             direction.y = -direction.y;
     
         cachedTransform.position = Camera.main.ViewportToWorldPoint(viewPos);
     }
     
     protected virtual void Start()
     {
         speed = Random.Range(minSpeed, maxSpeed);
         InitDirection();
     }
     
     protected virtual void Update()
     {
         Move();
     }
     
     protected virtual void InitDirection()
     {
         // give random orientation
         float randZ = Random.Range(minInitialOrientation, maxInitialOrientation);
         direction = Quaternion.Euler(0, 0, randZ) * cachedTransform.right;
     }
     
     protected abstract float GetY();
 }

In your case, you want sine movement, so (attach this to your bats):

 using UnityEngine;
 using System.Collections;
 
 public class SineWaveMovement : BaseMovement
 {
     [SerializeField]
     private GameObject trail; // just for fun
     [SerializeField]
     private float frequency = 1f;
     [SerializeField]
     private float wavelength = 1f;
 
     protected override float GetY()
     {
         // try and mess with some of the values here to see what dif results you get...
         return Mathf.Sin(2 * Mathf.PI * Time.time * frequency) * wavelength;
     }
 
     protected override void Move()
     {
         base.Move();
         StartCoroutine(CreateTrail());
     }
     
     private IEnumerator CreateTrail()
     {
         if (trail != null)
         {
                 var tObj = Instantiate(trail, cachedTransform.position, cachedTransform.rotation);
                 yield return new WaitForSeconds(3f);
                 Destroy(tObj);
         }
     }
 }

Resulting movement:

alt text

Linear movement is as easy as:

 public class LinearMovement : BaseMovement
 {
     protected override float GetY()
     {
         return velocity.y;
     }
 }

For a circular movement, check this out.


sin.png (96.2 kB)
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 cagudo · Apr 30, 2014 at 12:28 AM 0
Share

Thanks that helped me a lot

avatar image Aziz1989 · Oct 17, 2015 at 02:49 PM 0
Share

Hey thank you but how to control trail instantioation? please

avatar image
0

Answer by Aziz1989 · Oct 18, 2015 at 09:05 AM

Thank you it is very helpfull, but the Trail Trapped me :p my Unity Is now bugging X_X.

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 usama-qureshi · Jun 26, 2016 at 06:03 AM

Thanks A lot , it work like a champ.

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

23 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Issues with C# script (using tutorial) 1 Answer

The Sims-like movement script? 1 Answer

I want make a box colleder for top, botton left and roght in c# but dont work 0 Answers

Camera Script - Bird's Eye View 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