【发布时间】:2017-12-03 15:55:47
【问题描述】:
我正在尝试制作一个类似于部落的游戏的小型原型,主要灵感来自 Tribes:Ascend。问题是这个动作很难做。 在地面上无摩擦滑动以保持速度的滑雪是没有问题的。另一方面,空气/滑雪控制是。 我正在尝试做的是让玩家在高速时改变方向或减速而不以任何方式加速。
这是我为此使用的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Transform cameraObj;
public Transform cameraRefObj;
public Transform velObj;
public Camera cameraCam;
public Rigidbody rigbod;
public CapsuleCollider cap;
public float maxVel;
public float walkForce;
public float defDynFric;
public float defStatFric;
void Start () {
rigbod = this.GetComponent<Rigidbody>();
cap = this.GetComponent<CapsuleCollider>();
cameraObj = this.GetComponentInChildren<Camera>().transform;
cameraCam = this.GetComponentInChildren<Camera>();
}
void Update () {
Vector3 vel = rigbod.velocity;
Debug.DrawRay(this.transform.position, vel);
Vector3 velCalc = new Vector3(vel.x, 0, vel.z);
float speedCapMult =Mathf.Clamp01(vel.magnitude / maxVel);
velObj.rotation = Quaternion.LookRotation(velCalc);
cameraRefObj.transform.rotation = Quaternion.EulerAngles(0, Quaternion.ToEulerAngles(cameraObj.rotation).y, 0);
if (Input.GetButton("Skii"))
{
cap.material.dynamicFriction = 0;
cap.material.staticFriction = 0;
} else
{
cap.material.dynamicFriction = defDynFric;
cap.material.staticFriction = defStatFric;
}
if (Input.GetAxis("Vertical") !=0 || Input.GetAxis("Horizontal") != 0)
{
Vector3 InputVec = Quaternion.EulerAngles(0, Quaternion.ToEulerAngles(cameraObj.rotation).y, 0) * Vector3.ClampMagnitude(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") ),1);
float moveMulti =(1 -speedCapMult) +(speedCapMult*Mathf.Clamp01(1-Mathf.Cos(Vector3.Angle(velCalc, InputVec)/2)));
InputVec = Vector3.ClampMagnitude(InputVec, moveMulti);
Vector3 resVec = InputVec * (moveMulti * walkForce);
Debug.DrawRay(this.transform.position, resVec, Color.red);
// Debug.Log("SPD*: "+speedCapMult + " moveMulti: " + moveMulti + " VecAng: "+ Mathf.Clamp01(Mathf.Cos((Vector3.Angle(velCalc, InputVec)/2)-1)));
rigbod.AddForce(resVec);
}
}
}
我遇到的问题主要来自我对这一行的理解:
float moveMulti =(1 -speedCapMult) +(speedCapMult*Mathf.Clamp01(1-Mathf.Cos(Vector3.Angle(velCalc, InputVec)/2)));
它所做的是从低速运动到高速运动的过渡。低速很好,高速运动没有按预期工作。当转动速度方向时只转动一点然后它停止并且玩家继续朝那个方向移动,除非我转动相机(有点类似于源游戏中的冲浪)。
我希望它像在 T:A 中一样工作。我真的找不到任何好的参考视频,但它的要点是你可以走到一边,减速而不是加速。
【问题讨论】:
-
我想您可以使用
rigbod.AddTorque来仅更改角速度而不更改线速度。有关详细信息,请参阅Documentation page。
标签: c# unity3d physics game-physics