---
# System prepended metadata

title: 腳本

---

ChatGPT Generated Unity3D Character Controller 1

# 腳本

```C#
using UnityEngine;

public class MouseCharacterController : MonoBehaviour
{
    public float moveSpeed = 10.0f;
    public float rotationSpeed = 10.0f;
    public float gravity = 10.0f;
    private float verticalSpeed = 0.0f;
    private float verticalVelocity = 0.0f;
    private Vector3 moveVector;
    private CharacterController characterController;
    private Animator animator;
    private float rotation = 0.0f;
    private float angle = 0.0f;
    private float horizontalVelocity = 0.0f;
    private float horizontalSpeed = 0.0f;
    private Vector3 horizontalVector;
    private Vector3 mousePosition;
    private float mouseRotation = 0.0f;
    private float mouseRotationSpeed = 10.0f;
    private bool isGrounded = false;
    private bool isJumping = false;
    private bool isMoving = false;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // 計算角色的水平位移
        horizontalVelocity = Input.GetAxis("Horizontal") * moveSpeed;
        horizontalVector = transform.forward * horizontalVelocity;

        // 計算角色的垂直速度
        if (isGrounded)
        {
            verticalSpeed = 0.0f;

            if (Input.GetButton("Jump"))
            {
                isJumping = true;
                verticalSpeed = moveSpeed * 2.0f;
            }
        }
        else
        {
            verticalSpeed -= gravity * Time.deltaTime;
        }

        // 計算角色的垂直位移
        verticalVelocity = verticalSpeed * Time.deltaTime;
        moveVector = new Vector3(0, verticalVelocity, 0);

        // 移動角色
        characterController.Move(horizontalVector * Time.deltaTime);
        characterController.Move(moveVector);

        // 計算角色的旋轉角度
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, Camera.main.transform.position.y - transform.position.y));
        angle = AngleBetweenVector2(transform.position, mousePosition);
        rotation = angle - transform.eulerAngles.y;

        // 旋轉角色
        transform.Rotate(0, rotation, 0);

        // 計算角色是否在地面上
        isGrounded = (characterController.collisionFlags & CollisionFlags.Below) != 0;

        //
        // 計算角色是否正在移動
        isMoving = horizontalVelocity > 0.1f || horizontalVelocity < -0.1f;

        // 更新角色的動畫
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isJumping", isJumping);
        animator.SetBool("isMoving", isMoving);
        animator.SetFloat("verticalVelocity", verticalVelocity);
        animator.SetFloat("horizontalVelocity", horizontalVelocity);

        // 更新角色的滑鼠旋轉
        if (Input.GetMouseButton(1))
        {
            mouseRotation = Input.GetAxis("Mouse X") * mouseRotationSpeed;
            transform.Rotate(0, mouseRotation, 0);
        }
    }

    // 計算兩個向量之間的角度
    float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
    {
        Vector2 diference = vec2 - vec1;
        float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
        return Vector2.Angle(Vector2.right, diference) * sign;
    }
}
```






# 產生過程：
我：
> 幫我產生一個Unity3D角色控制器腳本

ChatGPT：
> 您好，我可以幫您產生一個Unity3D角色控制器腳本的概念架構，但實際寫出腳本需要您自己來進行。
> 
> 首先，請在Unity3D中新建一個C#腳本並命名為"CharacterController"。接著，請在腳本中加入以下程式碼：
> 
```C#
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public float moveSpeed = 10.0f;
    public float rotationSpeed = 10.0f;
    public float gravity = 10.0f;
    private float verticalSpeed = 0.0f;
    private float verticalVelocity = 0.0f;
    private Vector3 moveVector;
    private CharacterController characterController;
    private Animator animator;
    private float rotation = 0.0f;
    private float angle = 0.0f;
    private float horizontalVelocity = 0.0f;
    private float horizontalSpeed = 0.0f;
    private Vector3 horizontalVector;
    private Vector3 mousePosition;
    private float mouseRotation = 0.0f;
    private float mouseRotationSpeed = 10.0f;
    private bool isGrounded = false;
    private bool isJumping = false;
    private bool isMoving = false;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // 計算角色的水平位移
        horizontalVelocity = Input.GetAxis("Horizontal") * moveSpeed;
        horizontalVector = transform.forward * horizontalVelocity;

        // 計算角色的垂直速度
        if (isGrounded)
        {
            verticalSpeed = 0.0f;

            if (Input.GetButton("Jump"))
            {
                isJumping = true;
                verticalSpeed = moveSpeed * 2.0f;
            }
        }
        else
        {
            verticalSpeed -= gravity * Time.deltaTime;
        }

        // 計算角色的垂直位移
        verticalVelocity = verticalSpeed * Time.deltaTime;
        moveVector = new Vector3(0, verticalVelocity, 0);

        // 移動角色
        characterController.Move(horizontalVector * Time.deltaTime);
        characterController.Move(moveVector);

        // 計算角色的旋轉角度
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, Camera.main.transform.position.y - transform.position.y));
        angle = AngleBetweenVector2(transform.position, mousePosition);
        rotation = angle - transform.eulerAngles.y;

        // 旋轉角色
        transform.Rotate(0, rotation, 0);

        // 計算角色是否在地面上
        isGrounded = (characterController.collisionFlags & CollisionFlags.Below) != 0;

        //
```

我：
> 請從
> isGrounded = (characterController.collisionFlags & CollisionFlags.Below) != 0;
> 開始往下列出腳本

ChatGPT：
好的，接下來的程式碼如下：
```C#
// 計算角色是否正在移動
isMoving = horizontalVelocity > 0.1f || horizontalVelocity < -0.1f;

// 更新角色的動畫
animator.SetBool("isGrounded", isGrounded);
animator.SetBool("isJumping", isJumping);
animator.SetBool("isMoving", isMoving);
animator.SetFloat("verticalVelocity", verticalVelocity);
animator.SetFloat("horizontalVelocity", horizontalVelocity);

// 更新角色的滑鼠旋轉
if (Input.GetMouseButton(1))
{
    mouseRotation = Input.GetAxis("Mouse X") * mouseRotationSpeed;
    transform.Rotate(0, mouseRotation, 0);
}
}

// 計算兩個向量之間的角度
float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
    Vector2 diference = vec2 - vec1;
    float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
    return Vector2.Angle(Vector2.right, diference) * sign;
}
```
> 希望這些程式碼能夠幫助您。
