gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
using System.Collections; using System.Collections.Generic; using UnityEngine; public…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class moving : MonoBehaviour { public float speed ; public float turnSpeed; private bool isMoving = false; public bool isGround; public float jumpForce;
void Update() { Move(); HandleMovementInput() ; HandleJumpInput(); } void Move() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); transform.position += movement * speed * Time.deltaTime; transform.position += transform.forward * movement.z * speed * Time.deltaTime; transform.Rotate(new Vector3(0f, movement.x, 0f) * turnSpeed * Time.deltaTime); } void Turn() { if (Input.GetKeyDown(KeyCode.A)) { transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime); } if (Input.GetKeyDown(KeyCode.D)) { transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime); } if (Input.GetKeyDown(KeyCode.W)) { transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime); } if (Input.GetKeyDown(KeyCode.S)) { transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime); } } void HandleMovementInput() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); if (moveHorizontal != 0 || moveVertical != 0) { Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), turnSpeed * Time.deltaTime); transform.position += transform.forward * speed * Time.deltaTime; if (movement.magnitude > 0.1) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), turnSpeed * Time.deltaTime); transform.position += transform.forward * speed * Time.deltaTime; } } } void HandleTurningInput() { if (Input.GetKey(KeyCode.A)) { transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime); } if (Input.GetKey(KeyCode.W)) { transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime); if (!isMoving) { isMoving = true; } if (isMoving) { transform.position += transform.forward * speed * Time.deltaTime; } } if (Input.GetKey(KeyCode.S)) { transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime); if (!isMoving) { isMoving = true; } if (isMoving) { transform.position += transform.right * speed * Time.deltaTime; } } } void HandleJumpInput() { if (Input.GetKeyDown(KeyCode.Space) && isGround) { // 给物体一个向上的力 GetComponent<Rigidbody>().AddForce(new Vector3(0f, jumpForce, 0f), ForceMode.Impulse); isGround = false; } } // 检测是否在地面上 private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Terrain")) { isGround = true; } } - Assistant