如何正确使用 CharacterController.Move() 移动角色 [英] How to properly use CharacterController.Move() to move the character

查看:179
本文介绍了如何正确使用 CharacterController.Move() 移动角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基本的第一人称控制器,但我的问题是当我向前和向侧面移动时,我移动得更快.

如何在 1 Vector3 中添加 moveDirectionForwardmoveDirectionSide 以便能够使用 CharacterController.Move() 而不是 CharacterController.SimpleMove()?

void MovePlayer() {//获取水平和垂直输入浮动水平输入 = Input.GetAxis("水平");浮动垂直输入 = Input.GetAxis("垂直");//根据玩家的变换计算移动方向Vector3 moveDirectionForward = transform.forward * verticalInput * walkSpeed * Time.deltaTime;Vector3 moveDirectionSide = transform.right * horizo​​ntalInput * walkSpeed * Time.deltaTime;//将移动应用到玩家myCharacterController.SimpleMove (moveDirectionForward + moveDirectionSide);

解决方案

解决方案是使用归一化向量表示运动方向.

//获取水平和垂直输入浮动水平输入 = Input.GetAxis("水平");浮动垂直输入 = Input.GetAxis("垂直");//根据玩家的变换计算移动方向Vector3 moveDirectionForward = transform.forward * verticalInput;Vector3 moveDirectionSide = transform.right * horizo​​ntalInput;//找到方向Vector3 方向 = (moveDirectionForward + moveDirectionSide).normalized;//求距离Vector3 distance = direction * walkSpeed * Time.deltaTime;//将移动应用到玩家myCharacterController.Move(距离);

<小时>

性能说明:

<块引用>

vector.normalized 是从 vector/vector.magnitudevector.magnitude 是从 sqrt(vector.sqrMagnitude) 获得的处理起来很繁重.要减少处理重量,您可以使用vector/vector.sqrMagnitude 而不是 vector.normalized 而是ware 结果不完全一样,但仍然是一样的方向.

<小时><块引用>

现在我只需要施加重力

减去 moveDirection.y 乘以 Time.deltaTime 的重力.您还可以使用 Vector3TransformDirection 来简化和减少 MovePlayer 函数中的代码.

public float walkSpeed = 10.0f;private Vector3 moveDirection = Vector3.zero;公共浮动重力 = 20.0F;CharacterController myCharacterController = null;无效开始(){myCharacterController = GetComponent();}无效的移动播放器(){moveDirection = new Vector3(Input.GetAxis("Horizo​​ntal"), 0, Input.GetAxis("Vertical"));moveDirection = transform.TransformDirection(moveDirection);移动方向 *= 步行速度;moveDirection.y -= 重力 * Time.deltaTime;myCharacterController.Move(moveDirection * Time.deltaTime);}

I created a basic First Person Controller but my problem is when i move both forward and sideways i move faster.

How can i add moveDirectionForward and moveDirectionSide in 1 Vector3 to be able to use CharacterController.Move() instead of CharacterController.SimpleMove()?

void MovePlayer() {

    // Get Horizontal and Vertical Input
    float horizontalInput = Input.GetAxis ("Horizontal");
    float verticalInput = Input.GetAxis ("Vertical");

    // Calculate the Direction to Move based on the tranform of the Player
    Vector3 moveDirectionForward = transform.forward * verticalInput * walkSpeed * Time.deltaTime;
    Vector3 moveDirectionSide = transform.right * horizontalInput * walkSpeed * Time.deltaTime;

    // Apply Movement to Player
    myCharacterController.SimpleMove (moveDirectionForward + moveDirectionSide);

解决方案

The solution is to use a normalized vector for the direction of the movement.

// Get Horizontal and Vertical Input
float horizontalInput = Input.GetAxis ("Horizontal");
float verticalInput = Input.GetAxis ("Vertical");

// Calculate the Direction to Move based on the tranform of the Player
Vector3 moveDirectionForward = transform.forward * verticalInput;
Vector3 moveDirectionSide = transform.right * horizontalInput;

//find the direction
Vector3 direction = (moveDirectionForward + moveDirectionSide).normalized;
//find the distance
Vector3 distance = direction * walkSpeed * Time.deltaTime;

// Apply Movement to Player
myCharacterController.Move (distance);


Performance note:

vector.normalized is obtained from vector/vector.magnitude and vector.magnitude is obtained from sqrt(vector.sqrMagnitude) which is heavy to process. To reduce the processing weight you can use vector/vector.sqrMagnitude instead of vector.normalized but be ware the result is not exactly the same, but still is in the same direction.


now i just need to apply gravity

Subtract moveDirection.y by the gravity multiplied by Time.deltaTime. You can also simplify and reduce the code in the MovePlayer function by using Vector3 and TransformDirection.

public float walkSpeed = 10.0f;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0F;
CharacterController myCharacterController = null;

void Start()
{
    myCharacterController = GetComponent<CharacterController>();
}

void MovePlayer()
{
    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= walkSpeed;

    moveDirection.y -= gravity * Time.deltaTime;
    myCharacterController.Move(moveDirection * Time.deltaTime);
}

这篇关于如何正确使用 CharacterController.Move() 移动角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆