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 jelenkovic90 · Oct 19, 2015 at 08:49 PM · 2d2d game2d-platformer2d-physics2d platformer

Problem with 2D movement. My character is moving by himself between two points... :)

I want to make 1 small guy to move like this: my game will have 5 columns (positions on X axis -4, -2, 0, 2, 4) and when you press right arrow character will jump on position 2; If you press left arrow 2 times character will jump on position -2; you press one more time left arrow he goes on -4 and so on. And there is no ground in the game so only moving in air.

I'm new in programming so my code is probably like salad but... Please help me about this and if you think that some things in code are excess let me know. ;) Btw I'm working in C# Thanks a lot in advance!!!

using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour {

 public float moveForce = 365f;
 public float maxSpeed = 5f;

 public Vector3 pos1 = new Vector3(-4, 0, 0);
 public Vector3 pos2 = new Vector3(4, 0, 0);


 public void Update() {

     transform.position = Vector3.Lerp(pos1, pos2, (Mathf.Sin(maxSpeed * Time.time) + 1.0f) / 2.0f);

     float h = Input.GetAxis("Horizontal");

     if (h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
        
         GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);

     if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)

         GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) *
             maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

     if (Input.GetKey(KeyCode.RightArrow))
         MoveRight();

     if (Input.GetKey(KeyCode.LeftArrow))
         MoveLeft();

        }

 public void MoveRight()
 {
 }

 public void MoveLeft()
 {
 }

}

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

1 Reply

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

Answer by Tuncer · Oct 19, 2015 at 10:37 PM

     public int[] xPositions; // add your values( -4,-2 ..) smaller -> bigger xValues
     private int currentIndex;
     private bool isMoving;
 
     void Start()
     {
         currentIndex = 0;
         setPosition();
         isMoving = false;
     }
 
     void Update()
     {
         if (!isMoving)
         {
             if (Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 moveLeftIfPossible();
             }
             else if (Input.GetKeyDown(KeyCode.RightArrow))
             {
                 moveRightIfPossible();
             }
         }
     }
 
     private void moveLeftIfPossible()
     {
         if (currentIndex == 0)
             return;
         isMoving = true;
         currentIndex --;
         setPosition();
         isMoving = false;       
     }
 
     private void moveRightIfPossible()
     {
         if (currentIndex == xPositions.Length - 1)
             return;
         isMoving = true;
         currentIndex ++;
         setPosition();
         isMoving = false;
     }
 
     private void setPosition() // you can change this method with animation or coroution whatever you need
     {
         Vector3 currentPosition = this.gameObject.transform.position;
         this.gameObject.transform.position = new Vector3(xPositions[currentIndex], currentPosition.y, currentPosition.z);
     }
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 jelenkovic90 · Oct 20, 2015 at 08:56 AM 0
Share

Thanks a lot mate this works excellent. You really helped me! ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with Falling Platform Script 3 Answers

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

How can I make an object stop all momentum and hold it's position in air? 1 Answer

Why didnt my Collider works? 0 Answers

Why is there a gap between my player and the wall during wall slide? SOLVED 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