文章目录
- 1、rb.MovePosition方法
- 2、rb.velocity方法
- 3、rb.AddForce 方法:接受一个向量参数,表示要施加的力的大小和方向。
- 4、transform.Translate方法:接受一个向量参数,表示要移动的方向和距离。
- 5、 Mathf.Lerp 函数可以平滑地将一个值从一个范围过渡到另一个范围。
- 6、 Vector3.SmoothDamp 函数:接受四个参数:current:当前值;target:目标值;currentVelocity:当前速度,这个值会在函数中被修改以实现平滑过渡;smoothTime:平滑时间,表示从当前值到目标值所需的时间。
- 7、MoveTowards 方法:接受三个参数:当前位置、目标位置和移动速度。
- 8、Move 方法:接受一个向量参数,表示要移动的方向和距离。
1、rb.MovePosition方法
public class PlayerController : MonoBehaviour
{public float speed = 5.0f;public float smoothing = 0.1f;public float rotationSpeed = 5.0f;private Vector3 targetPosition;private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();targetPosition = transform.position;}void FixedUpdate(){float h = Input.GetAxisRaw("Horizontal");float v = Input.GetAxisRaw("Vertical");Vector3 direction = new Vector3(h, 0, v).normalized;if (direction.magnitude >= 0.1f){targetPosition += direction * speed * Time.deltaTime;Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, smoothing);Vector3 lookDirection = targetPosition - transform.position;Quaternion targetRotation = Quaternion.LookRotation(lookDirection);rb.MoveRotation(Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime));rb.MovePosition(smoothedPosition);}}
}
2、rb.velocity方法
public class PlayerController : MonoBehaviour
{public float speed = 5.0f;public float rotationSpeed = 5.0f;private Vector3 targetPosition;private Quaternion targetRotation;private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();targetPosition = transform.position;targetRotation = transform.rotation;}void FixedUpdate(){float h = Input.GetAxisRaw("Horizontal");float v = Input.GetAxisRaw("Vertical");Vector3 direction = new Vector3(h, 0, v).normalized;if (direction.magnitude >= 0.1f){targetPosition += direction * speed * Time.deltaTime;Vector3 lookDirection = targetPosition - transform.position;targetRotation = Quaternion.LookRotation(lookDirection);rb.velocity = direction * speed;}else{rb.velocity = Vector3.zero;}rb.MoveRotation(Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime));}
}
3、rb.AddForce 方法:接受一个向量参数,表示要施加的力的大小和方向。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;private Rigidbody rb;private float horizontalInput;private float verticalInput;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){horizontalInput = Input.GetAxis("Horizontal");verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);rb.AddForce(movement * moveSpeed * Time.fixedDeltaTime, ForceMode.VelocityChange);}
}
4、transform.Translate方法:接受一个向量参数,表示要移动的方向和距离。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;public float rotateSpeed = 100.0f;void Update(){float horizontalInput = Input.GetAxis("Horizontal");float verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);transform.Translate(movement * moveSpeed * Time.deltaTime);Vector3 rotation = new Vector3(0.0f, horizontalInput, 0.0f);float rotationAngle = Vector3.SignedAngle(transform.forward, rotation, Vector3.up);float rotationSpeed = Mathf.Clamp(Mathf.Abs(horizontalInput), 0.0f, 1.0f) * rotateSpeed;transform.Rotate(0.0f, rotationAngle * rotationSpeed * Time.deltaTime, 0.0f);}
}
5、 Mathf.Lerp 函数可以平滑地将一个值从一个范围过渡到另一个范围。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;public float smoothTime = 0.3f;void Update(){float horizontalInput = Input.GetAxis("Horizontal");float verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);Vector3 targetPosition = transform.position + movement * moveSpeed;transform.position = Vector3.Lerp(transform.position, targetPosition, smoothTime);}
}
6、 Vector3.SmoothDamp 函数:接受四个参数:current:当前值;target:目标值;currentVelocity:当前速度,这个值会在函数中被修改以实现平滑过渡;smoothTime:平滑时间,表示从当前值到目标值所需的时间。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;public float smoothTime = 0.3f;private Vector3 velocity = Vector3.zero;void Update(){float horizontalInput = Input.GetAxis("Horizontal");float verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);Vector3 targetPosition = transform.position + movement * moveSpeed;transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);}
}
7、MoveTowards 方法:接受三个参数:当前位置、目标位置和移动速度。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;void Update(){float horizontalInput = Input.GetAxis("Horizontal");float verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);Vector3 targetPosition = transform.position + movement * moveSpeed;transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);}
}
8、Move 方法:接受一个向量参数,表示要移动的方向和距离。
public class PlayerController : MonoBehaviour
{public float moveSpeed = 5.0f;private CharacterController controller;private float horizontalInput;private float verticalInput;void Start(){controller = GetComponent<CharacterController>();}void FixedUpdate(){horizontalInput = Input.GetAxis("Horizontal");verticalInput = Input.GetAxis("Vertical");Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput);controller.Move(movement * moveSpeed * Time.fixedDeltaTime);}
}