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()
{
}
}