Skip to content

Instantly share code, notes, and snippets.

@yasuyuki-kamata
Created August 10, 2014 01:54
Show Gist options
  • Save yasuyuki-kamata/afc298742100489516f4 to your computer and use it in GitHub Desktop.
Save yasuyuki-kamata/afc298742100489516f4 to your computer and use it in GitHub Desktop.
球体をとある範囲で操作する
using UnityEngine;
using System.Collections;
public class SpherePlayer : MonoBehaviour {
public float speed = 5f;
public float range = 5f;
void Update () {
float z = Input.GetAxisRaw ("Vertical");
float x = Input.GetAxisRaw ("Horizontal");
Vector3 direction = new Vector3 (x, 0, z).normalized;
transform.Translate (direction * speed * Time.deltaTime);
Clamp ();
}
void Clamp() {
Vector3 pos = transform.position;
pos.x = Mathf.Clamp (pos.x, -range, range);
pos.z = Mathf.Clamp (pos.z, -range, range);
transform.position = pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment