using UnityEngine; using System.Collections; using System; public class SphereController : MonoBehaviour { int layer = 0; Rigidbody rigidbody; void Start() { rigidbody = transform.GetComponent(); } void Update() { changeLayer(); changePosition(); } void changeLayer() { if (Input.GetKey(KeyCode.UpArrow)) { layer = 1; } if (Input.GetKey(KeyCode.DownArrow)) { layer = 0; } float delta = (layer * 2f - 2f) - rigidbody.position.z; Vector3 velocity = rigidbody.velocity; velocity.z = delta * 3f; rigidbody.velocity = velocity; } } void changePosition() { Vector3 direction = Vector3.zero; if (Input.GetKey(KeyCode.LeftArrow)) { direction = -Vector3.forward; } if (Input.GetKey(KeyCode.RightArrow)) { direction = Vector3.forward; } rigidbody.AddTorque(direction * 25f); }