Movement Solutions
private float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementSolution1(h, v);
}
private void MovementSolution1(float h, float v)
{
Vector3 dir = new Vector3(h, 0, v);
rb.MovePosition(rb.position + dir * speed * Time.fixedDeltaTime);
}
private void MovementSolution2(float h, float v)
{
Vector3 movement = new Vector3(0, 0, 0);
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
}
private void MovementSolution3(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
transform.Translate(movement);
}
private void MovementSolution4(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
//持續遞增
rb.AddForce(movement);
}
private void MovementSolution5(float h, float v)
{
Vector3 movement = new Vector3(h, 0f, v);
movement = movement.normalized * speed * Time.fixedDeltaTime;
//持續不變
rb.velocity = movement;
}
Rotation Solution
private Rigidbody rb;
private float rotateSpeed;
private float rotateLimit;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate(){
//水準
float mouseX = Input.GetAxis("Mouse X");
//垂直
float mouseY = Input.GetAxis("Mouse Y");
RotationSolution1(mouseX, mouseY);
RotationSolution2(mouseX, mouseY);
}
private float ClampRotation(float rotation, float currentLocalRotation){
float currentRotationY = currentLocalRotation;
currentRotationY += rotation;
currentRotationY = Mathf.Clamp(currentRotationY, -rotateLimit, rotateLimit);
return currentRotationY;
}
//相機上下旋轉,人物左右旋轉
private void RotationSolution1(float mouseX, float mouseY){
//相機垂直旋轉
camera.transform.localRotation = Quaternion.Euler(ClmapRotation(mouseY, camera.transfrom.localRotation.y), camera.transform.localRotation.x , 0);
//人物水準旋轉
transform.localRotation = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
}
//人物上下左右旋轉
private void RotationSolution2(float mouseX, float mouseY){
//設定垂直旋轉範圍
float currentRotationY = transfrom.localRotation.y;
currentRotationY += mouseY;
currentRotationY = Mathf.Clamp(currentRotationY, -rotateLimit, rotateLimit);
transform.localRotation = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
}
private void RotationSolution3(){
//人物左右旋轉
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
//相機上下旋轉
currentCameraRotationX += cameraRotationX;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
camera.transform.localEulerAngles = new Vector3(-currentCameraRotationX, 0f, 0f);
}