Last active
May 27, 2017 01:01
-
-
Save WakkyFree/2a48f26d631252737ca72d5dbcdea38e to your computer and use it in GitHub Desktop.
Unity script to move a player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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