【问题标题】:Smoothing Vector3.normalized?平滑 Vector3.normalized?
【发布时间】:2014-07-21 23:35:24
【问题描述】:

我在 Unity 3D 中使用角色控制器创建了一个玩家,我将他全部配置为行走、环顾四周等。因此,当使用法线向量 3 时,玩家开始平稳,直到达到最大速度。当我不正常化时,你可以同时按 W 和 D/A 走得更快,你走得比平时快 50%。但是当我正常化它时,播放器立即处于最大速度。这看起来很奇怪。

我该如何做才能让播放器在达到最大速度之前开始平滑?

这是我的完整脚本,以防你需要它:

using UnityEngine;
using System.Collections;

public class FirstPersonController : MonoBehaviour {

    CharacterController cc;

    public float    baseSpeed = 3.0f;
    public float    mouseSensitivity = 1.0f;

    public bool     inverted = false;
    public bool     gravityOn = true;
    public bool     lockMouse = true;

    float           mouseRotX = 0,
                    mouseRotY = 0;

    float           curSpeed = 3.0f;
    float           invertion = 1.0f;
    float           gravitySpeed = 0;

    string          h = "Horizontal";
    string          v = "Vertical";



    void Start () {
        cc = gameObject.GetComponent<CharacterController>();

        if (lockMouse && Debug.isDebugBuild)
            Screen.lockCursor = true;

        if (!Debug.isDebugBuild)
            Screen.lockCursor = true;
    }

    void FixedUpdate () {

        curSpeed = baseSpeed;

        if (inverted)
            invertion = -1.0f;
        else
            invertion = 1.0f;

        mouseRotX = Input.GetAxis("Mouse X") * mouseSensitivity;
        mouseRotY -= invertion * Input.GetAxis("Mouse Y") * mouseSensitivity;;
        mouseRotY = Mathf.Clamp(mouseRotY, -90.0f, 90.0f);

        float forwardMovement = Input.GetAxis(v);
        float strafeMovement = Input.GetAxis(h);

        Vector3 speed = new Vector3(strafeMovement, 0, forwardMovement);
        speed = transform.rotation * speed;
        Debug.Log(speed.normalized);

        //Applying Gravity
        if (cc.isGrounded)
        {
            gravitySpeed = 0.0f;
        }
        else if (gravityOn)
        {

            if (gravitySpeed >= 20.0f)
            {
                gravitySpeed = 20.0f;
            }

            if (cc.isGrounded == false)
            {
                gravitySpeed += Physics.gravity.y * Time.deltaTime;
            }

            cc.Move(new Vector3(0, gravitySpeed, 0) * Time.deltaTime);
        }

        //Applying movement and rotation.
        cc.Move(speed.normalized * curSpeed * Time.deltaTime);
        transform.Rotate(0, mouseRotX, 0);
        Camera.main.transform.localRotation = Quaternion.Euler(mouseRotY, 0 ,0); 

    }
}

【问题讨论】:

  • 你读过 normalize 的实际作用吗?
  • 是的,这就是我问如何平滑它的原因。
  • 既然您的问题是:如何缓慢增加 curSpeed - 要么实现真正的加速,要么在一段时间内简单地在初始值和最大值之间进行插值。
  • 注意:平滑归一化向量短语听起来很奇怪,因为您似乎不希望改变方向并且归一化向量的长度始终为一 - 所以没有什么可以改变的。
  • 啊,是的,这就是所谓的..加速!谢谢,我想我可以更进一步。

标签: c# unity3d


【解决方案1】:

你想要得到的特征是匀加速运动。

你需要这样的东西:

Vector3 _speed = Vector3.zero;

const float KDrag = 0.25f;

void FixedUpdate () 
{

float forwardMovement = Input.GetAxis(v);
float strafeMovement = Input.GetAxis(h);

Vector3 acceleration = cc.transform.right * strafeMovement + cc.transform.forward * forwardMovement;

_speed += acceleration * Time.fixedDeltaTime;
_speed -= _speed * KDrag * Time.fixedDeltaTime;

cc.Move( _speed  * Time.fixedDeltaTime );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多