2017-12-03 17 views
1

私は部族のようなゲームの小さなプロトタイプを作ろうとしています。主に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))); 

は低速の間trainsitionです高速移動への移動。低速はうまく動作しません。速度を回転させると、少しだけ回転して停止し、カメラを回らない限り、プレーヤーはその方向に移動します(ソースゲームでサーフィンしているのとまったく同じです)。

T:Aと同じように動作します。私は本当に良いrefferenceのビデオを見つけることはできませんが、それのジストは、あなたが横に行くことができますが、遅くなるが、スピードアップしていないということです。

IDの修正方法を知っている人には本当に助かりました。前もって感謝します! :D

+0

代わりに 'rigbod.AddTorque'を使って線速度ではなく角速度のみを変更することができます。詳細については、[Documentation page](https://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html)を参照してください。 – meowgoesthedog

答えて

0

私は問題を解決することができました。そのシンプルな私はばかげている。 私は単純にmovemultiplierを変更:基本的にシンプルな角度にコサインものを捨てる

  float moveMulti =(1 -speedCapMult) +(speedCapMult*Mathf.Clamp01(Vector3.Angle(velCalc, InputVec)/180)); 

。その意図通りの作品です。

関連する問題