2017-12-17 14 views
1

私のスクリプト剛体のコントローラがある -回転剛体コントローラ

public float Speed = 5f; 
public float JumpHeight = 2f; 
public float GroundDistance = 0.2f; 
public float DashDistance = 5f; 
public LayerMask Ground; 

private Rigidbody _body; 
private Vector3 _inputs = Vector3.zero; 
private bool _isGrounded = true; 
private Transform _groundChecker; 


void Start() 
{ 
    _body = GetComponent<Rigidbody>(); 
    _groundChecker = transform.GetChild(0); 
} 

void Update() 
{ 
    _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore); 


    _inputs = Vector3.zero; 
    _inputs.x = Input.GetAxis("Horizontal"); 
    _inputs.z = Input.GetAxis("Vertical"); 
    if (_inputs != Vector3.zero) 
     transform.forward = _inputs; 

    if (Input.GetButtonDown("Jump") && _isGrounded) 
    { 
     _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange); 
    } 
    if (Input.GetButtonDown("Sprint")) 
    { 
     Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new Vector3((Mathf.Log(1f/(Time.deltaTime * _body.drag + 1))/-Time.deltaTime), 0, (Mathf.Log(1f/(Time.deltaTime * _body.drag + 1))/-Time.deltaTime))); 
     _body.AddForce(dashVelocity, ForceMode.VelocityChange); 
    } 
} 


void FixedUpdate() 
{ 
    _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime); 
} 

は、どのようなカメラの方向Yにターンを作るための最善の方法?つまり、プレイヤーはマウスが回っている側に向かっていますか? fixedUpdateか更新中ですか?私は通常、回転プレーヤーを立っているとき、私は移動を開始したときに

void FixedUpdate() 
{ 
    _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime); 
    transform.rotation = Quaternion.Euler(0, MainCamera.CurrentCoord.y, 0); 
} 

、 -

public float Smoothness = 0.3F; 
public Vector2 Sensitivity = new Vector2(4, 4); 
public Vector2 LimitX = new Vector2(-70, 80); 

private Vector2 NewCoord; 
public Vector2 CurrentCoord; 
private Vector2 vel; 
public GameManager GameMangerS; 

public Transform Target; 
public float TransformSpeed; 

public Animator CameraAnimator; 

void Update() 
{ 
    NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y); 
    NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x; 
    NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y; 
    CurrentCoord.x = Mathf.SmoothDamp(CurrentCoord.x, NewCoord.x, ref vel.x, Smoothness/2); 
    CurrentCoord.y = Mathf.SmoothDamp(CurrentCoord.y, NewCoord.y, ref vel.y, Smoothness/2); 
    transform.rotation = Quaternion.Euler(CurrentCoord.x, CurrentCoord.y, 0); 
} 

そして、コントローラスクリプトに次の行を追加:

これはカメラのスクリプトですすべての回転がリセットされ、プレーヤーは動いていません。

+1

あなたは 'FixedUpdate'機能で物理学を移動します。この場合、 '_body'は' Rigidbody'なので、 'FixedUpdate'関数で使うべきです。 – Programmer

+0

どうすればいいですか?プレーヤーを回転させる方法は? – evelyn

+0

transform.Rotate()またはtransform.LookAt()を使用すると、 –

答えて

0

更新

単純な回転がtransform.Rotate()を使用して達成することができます。

例:

this.transform.Rotate(Vector3.up, 30); 

この例ではつもり上向きに指すベクトルの周りに30度回転変換されます。

LookAtMouse:

は、あなたのオブジェクトはマウスの方に向けるようにするには、あなたのカメラからScreenToWorldSpace()メソッドが必要になります。 ScreenCoordinatesをWorldCoordinatesに変換するため。

例:

ご注意:

  1. をあなたがカメラのインスタンスを設定する必要があります!それを追加しないと、NullReferenceExceptionが発生します。
  2. このスニペットは、あなたが望むふるまいを達成するために必要なステップのみを表示するものとします。
  3. このコード行をコードに統合して動作させる方法を自分自身で見つけ出す必要があります。プログラマがそれを統合するときにコメントであなたに語ったことを考えてみましょう。
Vector3 mousePosition = Input.mousePosition; //get the screenSpaceMousePosition 
Vector3 worldPosition = this.camera.ScreenToWorldPoint(mousePosition); //convert it into worldSpacePosition 
Vector3 calculatedDirection = worldPosition - transform.position; //calucate the looking direction 
calculatedDirection.y = 0; 
Quaternion rotation = Quaternion.LookRotation(calculatedDirection); 
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime); 
関連する問題