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 steviemelts · Apr 02, 2015 at 09:51 PM · 2dmovement

How to create a simple enemy that moves vertically up and down

Hi all,

I am a student who has to create a simple game as part of an assignment. I really need help in creating and scripting an enemy that will move up and down causing difficulty for the player to jump from one platform to another. I know it may be a simple request but so far I cannot find any help online. I really would appreciate any help 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
5

Answer by lordlycastle · Apr 02, 2015 at 10:09 PM

You really should be doing your homework on your own. But who am I to judge, I never did mine.

 float maxMoveDistance = 10;
 //Set this to your objects initial position when game starts.
 Vector3 origin;
 float speed = 10;

 void Update(){
     Vector3 destination = origin;
     destination.y = (transform.position.y > origin.y + maxMoveDistance) ? origin.y : origin.y + maxMoveDistance;
     transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
 }

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 steviemelts · Apr 02, 2015 at 10:51 PM 0
Share

Thanks Lordlycastle.

I set that up and you have it nearly spot on which I really appreciate. One problem I get is that the enemy instantly fly's over to my player on starting the game. How do i set the enemy up that they do not rush over to the player and just stay at various stages of the level where jumping from platform to platform takes place.

avatar image lordlycastle · Apr 05, 2015 at 07:33 PM 0
Share

@steviemelts I think it’s because you didn’t set the origin to a value so it defaults to Vector3.zero, and I’m guessing that’s where your player is. In the Awake or Start function set the origin to the transforms position. This way it’ll remember it’s start position. Also I made a mistake in the second line, it should be destination.y = … and not *=. I’ll fix it now.

If you did, can you post your movement code. Don’t forget to use @ symbol with my name otherwise I won’t be notified.

avatar image lordlycastle · Apr 08, 2015 at 05:50 PM 0
Share

@steviemelts Don’t forget to accept the answer if it answered the question.

avatar image
2

Answer by tanoshimi · Apr 05, 2015 at 07:46 PM

An alternative using MathF.PingPong:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {

     Vector3 _startingPos;
     Transform _trans;

     void Start() {
         _trans = GetComponent<Transform>();
         _startingPos = _trans.position;
     }

     void Update() {
         _trans.position = new Vector3(_startingPos.x, _startingPos.y + Mathf.PingPong(Time.time, 3), _startingPos.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

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

2 People are following this question.

avatar image avatar image

Related Questions

aquarium effect for 2d fish 2 Answers

2D Top-Down Movement: Transform.up and right don't change 2 Answers

How to I get my 2d gameobject to move towards another gameobject when it is clicked? 0 Answers

Help Wanted. Switching localscale starts unwanted animation state. 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 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