2017-11-08 5 views
0

Unityでは、私がwを保持しているときに、一方向に進むのではなく、私のカメラどうすればいいですか? (申し訳ありませんが、私は団結に新たなんだ)私のキャラクターをカメラの方向に動かすにはどうすればいいですか(Unity)

EDIT:移動スクリプトは次のとおりです。

using UnityEngine; 
using System.Collections; 

public class Player_Movement : MonoBehaviour { 
    public float speed = 6.0F; 
    public float jumpSpeed = 8.0F; 
    public float gravity = 20.0F; 
    private Vector3 moveDirection = Vector3.zero; 
    void Update() { 
     CharacterController controller = GetComponent<CharacterController>(); 
     if (controller.isGrounded) { 
      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,  Input.GetAxis("Vertical")); 
      moveDirection = transform.TransformDirection(moveDirection); 
      moveDirection *= speed; 
      if (Input.GetButton("Jump")) 
       moveDirection.y = jumpSpeed; 

     } 
     moveDirection.y -= gravity * Time.deltaTime; 
     controller.Move(moveDirection * Time.deltaTime); 
    } 
} 

答えて

0

@lockstockによって示されるようにカメラの前方向がCamera.main.transform.forward

+0

ええ、それに私のコードを直接教えていただけますか?私はよくコードできません.. –

0

を使用することによって得られる、方向カメラは、その変換から後退することができます。使用方法を知りたい場合は、以下の例を参考にしてください。

public class Player_Movement : MonoBehaviour 
{ 
    public float speed = 6.0F; 
    public float jumpSpeed = 8.0F; 
    public float gravity = 20.0F; 
    // Drag & Drop the camera in this field, in the inspector 
    public Transform cameraTransform ; 
    private Vector3 moveDirection = Vector3.zero; 
    void Update() { 
     CharacterController controller = GetComponent<CharacterController>(); 
     if (controller.isGrounded) { 
      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
      moveDirection = cameraTransform.TransformDirection(moveDirection); 
      moveDirection *= speed; 
      if (Input.GetButton("Jump")) 
       moveDirection.y = jumpSpeed; 

     } 
     moveDirection.y -= gravity * Time.deltaTime; 
     controller.Move(moveDirection * Time.deltaTime); 
    } 
} 

エラーなしの作品このスクリプト。私は自分自身でそれをテストしました。

関連する問題