2016-11-24 11 views
0

私はゲーム開発においてかなり新しいです。私は、プレーヤーの動きに基づいてカメラを動かすコードを持っています。 プレイヤーの動きスクリプト:unity3d:カメラがプレーヤーに従わない

using UnityEngine; 
using System.Collections; 

public class PlayerMovement : MonoBehaviour 
{ 

public float speed = 6f; 
//to store movement 
Vector3 movement; 
Rigidbody playerRigidbody; 
int floorMask; 
float camRayLenghth = 100f; 

//gets called regardless if the script is enabled or not 
void Awake(){ 
    floorMask = LayerMask.GetMask ("Floor"); 
    playerRigidbody = GetComponent<Rigidbody>(); 
    //Input.ResetInputAxes(); 
} 

//unity calls automatically on every script and fire any physics object 
void FixedUpdate(){ 
    float h = Input.GetAxisRaw ("Horizontal"); 
    float v = Input.GetAxisRaw ("Vertical"); 
    Move (h, v); 
    //Rotate(); 

    Turning(); 
} 

void Move(float h, float v){ 
    movement.Set (h,0f,v); 
    movement = movement.normalized * speed * Time.deltaTime; 
    playerRigidbody.MovePosition (transform.position + movement); 
} 

void Turning(){ 
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition); 
    RaycastHit floorHit; 
    if (Physics.Raycast (camRay,out floorHit,camRayLenghth,floorMask)) { 
     Vector3 playerToMouse = floorHit.point - transform.position; 
     playerToMouse.y = 0f; 
     Quaternion newRotation = Quaternion.LookRotation (playerToMouse); 
     playerRigidbody.MoveRotation (newRotation); 
    } 
} 
} 

、ここでは、メインカメラに取り付けたスクリプトです:

using UnityEngine; 
using System.Collections; 

public class CameraFollow : MonoBehaviour { 
// a target for camera to follow 
public Transform player; 

// how fast the camera moves 
public float smoothing = 4f; 

//the initial offset from the target 
Vector3 offset; 

void start(){ 
    //calculation of initial offset (distance) between player and camera 
    offset = transform.position - player.position; 
    Debug.Log ("offset is " + offset); 
} 

void FixedUpdate(){ 
    //updates the position of the camera based on the player's position and offset 
    Vector3 playerCameraPosition = player.position + offset; 

    //make an smooth transfer of location of camera using lerp 
    transform.position = Vector3.Lerp(transform.position, playerCameraPosition, smoothing * Time.deltaTime);    

} 
} 

が、私はすぐに私がテストゲームカメラをプレイすると、私のメインカメラにスクリプトを添付する場合プレイヤーはまだ移動していないにもかかわらず、移動を開始し、地面に向かって移動します。カメラからスクリプトを削除して、カメラをプレイヤーの子にすると、再生を開始するとすぐに、カメラがオブジェクトの周りを回転し始めます。

私は間違って何をしているのですか?

+0

void start() { ... } 

を添付しましたか? –

+0

はい、インスペクタを使って添付しました –

答えて

0

startメソッドの小文字のため、カメラのオフセットが設定されていません。これは、オフセットがデフォルト値の0,0,0のままになっていることを意味します。これは、カメラをファンキーな場所に移動させる原因となります。

変更:あなたはインスペクタを介してカメラスクリプトにプレーヤーを `

void Start() { 
... 
} 
+0

同じ動作ですが、今回はカメラの位置が地面に向かってゆっくりと変化します。 –

+0

スタート機能は実行されていますか?オフセット位置は私にとってはうまくいくようです。私はプレーヤーの位置とオフセットをデバッグし、それらが正しいかどうかを確認します。 – CaTs

+0

カメラのスクリプトでStart()をStart()に変更してみてください。カメラが地面に向かって移動していてオフセットが設定されていないように見える場合は0,0,0のままです。 – CaTs

関連する問題