- Home /
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()
{
}
}
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);
}
Thanks a lot mate this works excellent. You really helped me! ;)
Your answer
Follow this Question
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