using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class SphereControl2 : MonoBehaviour { public Rigidbody rigidbody; [Range(0.01f, 10.0f)] public float speed = 1.0f; [Range(0.01f, 1000.0f)] public float jumpForce = 4.0f; public bool isOnGround = true; void OnCollisionEnter() { isOnGround = true; } // Update is called once per frame void Update() { // if (Input.GetKey(KeyCode.RightArrow)) // { // SetVelocityX(speed); // } // if (Input.GetKey(KeyCode.LeftArrow)) // { // SetVelocityX(-speed); // } // if (Input.GetKey(KeyCode.UpArrow)) // { // SetVelocityZ(speed); // } // if (Input.GetKey(KeyCode.DownArrow)) // { // SetVelocityZ(-speed); // } // if (Input.GetKey(KeyCode.Space) && isOnGround) // { // SetVelocityY(jumpForce); // isOnGround = false; // } if (Input.GetKey(KeyCode.RightArrow)) { rigidbody.AddForce(speed, 0, 0); } if (Input.GetKey(KeyCode.LeftArrow)) { rigidbody.AddForce(-speed, 0, 0); } if (Input.GetKey(KeyCode.UpArrow)) { rigidbody.AddForce(0, 0, speed); } if (Input.GetKey(KeyCode.DownArrow)) { rigidbody.AddForce(0, 0, -speed); } if (Input.GetKeyDown(KeyCode.Space) && isOnGround) { rigidbody.AddForce(0, jumpForce, 0, ForceMode.Impulse); isOnGround = false; } } void SetVelocityX(float v) { Vector3 currentVelocity = rigidbody.velocity; currentVelocity.x = v; rigidbody.velocity = currentVelocity; } void SetVelocityY(float v) { Vector3 currentVelocity = rigidbody.velocity; currentVelocity.y = v; rigidbody.velocity = currentVelocity; } void SetVelocityZ(float v) { Vector3 currentVelocity = rigidbody.velocity; currentVelocity.z = v; rigidbody.velocity = currentVelocity; } }