Skip to content

Instantly share code, notes, and snippets.

@WakkyFree

WakkyFree/MovePlayers.cs

Last active May 27, 2017
Embed
What would you like to do?
Unity script to move a player
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour {
private bool d_positive = true;
private float x = 0.05f;
private float x_sum = 0.0f;
private float x_max = 2.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (d_positive && Mathf.Abs(x_sum) < x_max) { // Move positive direction
transform.localPosition = new Vector3(x_sum, 0, 0);
x_sum += x;
} else if (!d_positive && Mathf.Abs(x_sum) < x_max) { // Move negative direction
transform.localPosition = new Vector3(x_sum, 0, 0);
x_sum -= x;
} else {
if(d_positive){
x_sum -= x;
} else {
x_sum += x;
}
d_positive = !d_positive;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment