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 AlejandroBoss10 · Apr 26, 2018 at 04:38 PM · 2denemymovement scriptyaxis

Make enemy move up and down by itself in 2D space?

Well the question says it all pretty much. So if you have played the Mario games, there is a turtle that flies up and down making it difficult for Mario to get through. Well that is what I am trying to accomplish, for the enemy to move up and down. As of right now, I have a way-point script that accomplishes this but I don't want to make several game objects for the enemies to follow. If I could set the position that the enemy would move to in the inspector that would be great. An example being, set the distance that the enemy has to move is 3 units up and then 3 units down, and it repeats. Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Apr 26, 2018 at 07:19 PM

If you want the effect of a "Mario-turtle" that should be pretty simple. Assuming that you have some Vector3 with your starting position:

 public void Update()
     {
         transform.position = startingPosition + Vector3.up * Mathf.Sin(Time.realtimeSinceStartup) * maxDistanceFromStart;
     }

If this is not enough and you need a linear movement please comment below.

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 AlejandroBoss10 · Apr 30, 2018 at 09:05 PM 0
Share

Hey @Captain_Pineapple thanks for your script, it works pretty well. One thing that I would like to comment on is how to get "maxDistanceFromStart" to not be the speed that the object has to travel? According to what you put, if I set the maxDistanceFromStart to 5, it travels slower than if I were to set it to 10. how would I make it go 5 units up and down but at different speeds without having to change how many units it moves? What I did, is that I made a public Vector 3 named startingPosition and set those coordinates, and then I made a public int named maxDistanceFromStart and that is how I got what you gave me to work. Thanks a bunch!

avatar image
0

Answer by SBrooks75 · May 08, 2018 at 03:43 PM

Hey @AlejandroBoss10 , this script should give you something similar to what your looking for. I tested it. If you have questions setting it up in inspector let me know.

 public Vector3 startPosition;
     public Vector3[] moveToPoints;
     public Vector3 currentPoint;
 
     public float moveSpeed;
 
     public int pointSelection;
 
     // Use this for initialization
     void Start () {
         
         //Sets the object to your starting point
         this.transform.position = startPosition;
 
     }
     
     // Update is called once per frame
     void Update () {
         
         Move ();
 
     }
 
 
     void Move(){
 
         //Starts to move the object towards the first "moveToPoint" you set in inspector
         this.transform.position = Vector3.MoveTowards (this.transform.position, currentPoint, Time.deltaTime * moveSpeed);
 
         //check to see if the object is at the next "moveToPoint"
         if (this.transform.position == currentPoint) {
             
             //if so it sets the next moveTo location
             pointSelection++;
 
             //if your object hits the last "moveToPoint it sends the object back to starting position to start the sequence over
             if (pointSelection == moveToPoints.Length){
                 pointSelection = 0;
 
             }
 
             //sets the destination of the "moveToPoint" destination
             currentPoint = moveToPoints[pointSelection];
         }
     }
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

165 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

Related Questions

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? 3 Answers

How can I make my Enemy look/flip towards my player (2D TOP-DOWN) 1 Answer

How to move 2D enemy AI's along the x-Axis? 0 Answers

Player movement script for a stickman 1 Answer

Why is my gameObject falling so slow? 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