Skip to content

Instantly share code, notes, and snippets.

@WakkyFree
Last active May 27, 2017 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WakkyFree/2a48f26d631252737ca72d5dbcdea38e to your computer and use it in GitHub Desktop.
Save WakkyFree/2a48f26d631252737ca72d5dbcdea38e to your computer and use it in GitHub Desktop.
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