【问题标题】:Unity3d: clamp magnitude to only x axisUnity3d:将幅度钳制到仅x轴
【发布时间】:2013-11-01 12:34:18
【问题描述】:

使用 Unity3d 4.1.2,C# 单声道。

执行以下脚本:

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour 
{
public float MoveSpeed = 30.0f;

public float maxVel = 0.000000001f;
// Use this for initialization
void Start () 
{
    //Physics.gravity = Vector3(0,-50,0);
}

void FixedUpdate()
{
    maxVel = 20f;
    MoveSpeed = 50.0f;
    if(Input.GetKey(KeyCode.D))
    {
        rigidbody.AddForce(Vector3.right * MoveSpeed);
        Debug.Log("BEFORE = " + rigidbody.velocity.magnitude);
    }

    if(Input.GetKey(KeyCode.A))
    {
        rigidbody.AddForce(Vector3.left * MoveSpeed);
    }

    rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxVel);
    Debug.Log("AFTER = " + rigidbody.velocity.magnitude);
}
// Update is called once per frame
void Update () 
{

}
}

正如您所见,工作正常,夹子有一些问题,但最终可以正常工作。

有一个问题...

在unity的物理引擎中,我将y值改为-30,并将材质“bounce”设置为1,所以它不会损失能量。

问题是我的夹子夹住了反弹效果,因为夹子影响所有轴:( 因此刚体永远不会回到它的 y 位置。

有没有办法改变夹具只影响 x 轴?我无法找到任何有关此的信息。

大家好

【问题讨论】:

  • 我猜现在不是在统一的计算机上,但你看过刚体组件的编辑器界面吗?那里有几个特定于轴的锁。我知道一个是轮换,但不确定另一个。

标签: c# mono unity3d


【解决方案1】:

是的,只限制 x 值:

Vector3 clampVel = rigidBody.velocity;
clampVel.x = Mathf.Clamp(clampVel.x, min, max);

rigidBody.velocity = clampVel;

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多