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 FunkyShammy · Jul 05, 2018 at 09:57 PM · 2dmovementenemymovement scriptshooter

How can i add simple back and forth movement on the Y-axis to an enemy that has velocity on the X-axis attached to it?

Hello,

I m creating a 2D horizontal side-scroller shooter and I have enemies being spawned, whose rigidbody component is being used to give them a velocity like so:

 public class Mover : MonoBehaviour
 {
     public float speed;
     public new Rigidbody2D rigidbody;
     public bool random;
 
     void Start()
     {
         if(random)
         rigidbody.velocity = Random.value* transform.right * speed;
         {
             rigidbody.velocity =  transform.right * speed;
         }
     }
 }

How can i have these enemies also move up and down constantly on the the Y-axis while they are moving with a velocity on the X-axis? Everything i have tried seems to interfere with the velocity of the objects. I am basically trying to create a simply behavioral pattern so as to make the targets less easy for the player to aim.

Any ideas?

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
0
Best Answer

Answer by FunkyShammy · Jul 06, 2018 at 12:47 PM

I have found the solution by following the answer given under this link: https://gamedev.stackexchange.com/questions/96878/how-to-animate-objects-with-bobbing-up-and-down-motion-in-unity?newreg=ccb2eaf1b725413ca777a68348637990

Here is my updated code:

 public class EnemyMover : MonoBehaviour {
     public float speed;
     public new Rigidbody2D rigidbody;
     public bool random;
     float originalY;
  
     public float floatStrength = 1;  
 
     void Start()
     {
         this.originalY = this.transform.position.y;
         
          if (random) {
               rigidbody.velocity = Random.value * transform.right * speed;
           }else
           {
             rigidbody.velocity = transform.right * speed;
         }
 
        
     }
 
 
     void Update()
     {
         transform.position = new Vector3(transform.position.x,
                originalY + ((float)System.Math.Sin(Time.time) * floatStrength),
                transform.position.z);
     }
 
 }
 
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 Ballisticspork · Jul 05, 2018 at 11:22 PM

The problem is that your x velocity is set to transform.right number which would be the same as new Vector2(number 1, 0). This means that you set the y velocity to 0. I'm assuming what you did is had another line that said velocity = transform.up * number, bu this sets the x velocity to 0, so whichever of the two lines you had on the bottom would be the one that you would see when you run the game. If you want it to move on the y- axis while moving on the x - axis you'll have to do something like:

rb.velocity = new Vector2(Xvelocity, Yvelocity);

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 FunkyShammy · Jul 05, 2018 at 11:31 PM 0
Share

Hi , Thank you for you reply. I think what you re suggesting would make the object move in a diagonal direction. What i basically want is for the object to keep moving on the x-axis, but while it moves, it also moves repeatedly in a loop in the Y-axis, for example from Y=1 to Y=-1 and then from Y=-1 back to Y=1 repeatedly. Does that make sense?

avatar image
0

Answer by TheMannyzaur · Jul 06, 2018 at 07:33 AM

Try this transform.position.y += Mathf.Sin (Time.fixedTime Mathf.PI frequency) * amplitude;

Where frequency and amplitude are your own values

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 TheMannyzaur · Jul 06, 2018 at 07:34 AM 0
Share

There's a star between the Time.fixedTime and $$anonymous$$athf.PI

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

215 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

Related Questions

Player movement script for a stickman 1 Answer

2D player keeps getting dragged to the left for some reason. 0 Answers

2D How to make character slowly stop after moving 1 Answer

Enemy moving left towards player. 1 Answer

Make enemy move up and down by itself in 2D space? 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