2012-05-25 85 views
4

我有一個叫做Ball的對象,並且我爲它添加了鍵盤交互(WASD來移動球) 我需要相機留下來跟着球,但是我收到了錯誤。如何讓攝像頭跟隨unity3d C#中的對象?

using UnityEngine; 
using System.Collections; 
public class ballmain : MonoBehaviour { 
    public bool isMoving = false; 
    public string direction; 
    public float camX; 
    public float camY; 
    public float camZ; 
    // Use this for initialization 
    void Start() { 
     Debug.Log("Can this run!!!"); 
    } 

    // Update is called once per frame 
    void Update() { 
     camX = rigidbody.transform.position.x -=10; 
     camY = rigidbody.transform.position.y -=10; 
     camZ = rigidbody.transform.position.z; 
     camera.transform.position = new Vector3(camX, camY, camZ); 
      //followed by code that makes ball move 
    } 
} 

我得到錯誤「資產/ ballmain.cs(18,44):錯誤CS1612:無法修改的‘UnityEngine.Transform.position’值類型的返回值考慮在臨時變量存儲值」 有誰知道答案?如果我註釋掉關於相機的代碼,球可以移動。

回答

0

在這些線路的-=

camX = rigidbody.transform.position.x -=10; 
    camY = rigidbody.transform.position.y -=10; 

是錯誤的。 -=將嘗試修改rigidbody.transform.position。你只想要-

但是,照相機不會跟蹤目標Z位置的變化,如果相機旋轉,它也不會正確追蹤。爲了得到正確的位置,你需要(向量中): -

cam_pos = target_pos - dist_to_target * cam_look_at 
+0

所以其camera.transform.position = rigidbody.transform.position - 10 * cam_look_at?什麼是cam_look_at? – chesnutcase

+0

@chesnutcase:照相機的位置和方向存儲爲4x4矩陣。矩陣的行(或列,取決於它如何設置)等同於:單位右向量,單位向上向量,向量單位,位置。右,向上和向量是相機認爲正確的方向,在世界座標中向前和向前,向前/在相機正在尋找的方向。這三個矢量是正交的(即,任何兩個的點積是零)。 – Skizz

4

在這裏你去。完整的代碼。

簡單的關注

using UnityEngine; 
using System.Collections; 

public class Follow: MonoBehaviour { 
public Transform target; 
public float smooth= 5.0f; 
void Update(){ 
    transform.position = Vector3.Lerp (
     transform.position, target.position, 
     Time.deltaTime * smooth); 
} 

    } 

高級繼

using UnityEngine; 
using System.Collections; 

public class SmoothFollowScript: MonoBehaviour { 

// The target we are following 
public Transform target; 
// The distance in the x-z plane to the target 
public int distance = 10.0; 
// the height we want the camera to be above the target 
public int height = 10.0; 
// How much we 
public heightDamping = 2.0; 
public rotationDamping = 0.6; 


void LateUpdate(){ 
    // Early out if we don't have a target 
    if (TargetScript.russ == true){ 
    if (!target) 
     return; 

    // Calculate the current rotation angles 
    wantedRotationAngle = target.eulerAngles.y; 
    wantedHeight = target.position.y + height; 

    currentRotationAngle = transform.eulerAngles.y; 
    currentHeight = transform.position.y; 

    // Damp the rotation around the y-axis 
    currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); 

    // Damp the height 
    currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); 

    // Convert the angle into a rotation 
    currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); 

    // Set the position of the camera on the x-z plane to: 
    // distance meters behind the target 
    transform.position = target.position; 
    transform.position -= currentRotation * Vector3.forward * distance; 

    // Set the height of the camera 
    transform.position.y = currentHeight; 

    // Always look at the target 
    transform.LookAt (target); 
} 
} 
} 
+0

每次我嘗試這個時,Unity都會凍結。爲什麼?它什麼都不做,我必須終止這個過程。 – MikkoP

+0

不知道。我從來沒有遇到過這個問題。 你想以什麼方式跟隨你的對象? – 2014-04-13 02:57:42

+0

不知道它有什麼問題,但重新安裝Unity後,它工作:)它也在我的筆記本電腦上工作,我的桌面安裝是越野車或什麼的。 – MikkoP

1

包括標準的移動資產到您的項目。它的腳本部分包含一個SmoothFollow2D.js代碼。將此代碼附加到gameobject並初始化公共變量。這隻會爲你做這項工作。

2

如果你只是單純的想跟隨目標對象對準攝像頭的位置,你想要的方式,使相機的目標對象的孩子,其餘的將做

1

我發現這個簡單而有用的統一2d相機遵循腳本。

using UnityEngine; 
using System.Collections; 

public class FollowCamera : MonoBehaviour { 

public float interpVelocity; 
public float minDistance; 
public float followDistance; 
public GameObject target; 
public Vector3 offset; 
Vector3 targetPos; 
// Use this for initialization 
void Start() { 
    targetPos = transform.position; 
} 

// Update is called once per frame 
void FixedUpdate() { 
    if (target) 
    { 
     Vector3 posNoZ = transform.position; 
     posNoZ.z = target.transform.position.z; 

     Vector3 targetDirection = (target.transform.position - posNoZ); 

     interpVelocity = targetDirection.magnitude * 5f; 

     targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

     transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f); 

    } 
    } 
} 

unity2d camera follow script

+0

嗯工作很好!謝謝 – aflatoon

0

我這裏還有一個劇本,我發現我的遊戲開發過程中非常有用。我沒有創建它們,所以我讚賞wiki.unity3d.com提供這個驚人的腳本。

光滑如下:

using UnityEngine; 
using System.Collections; 

     public class SmoothFollow2 : MonoBehaviour { 
     public Transform target; 
     public float distance = 3.0f; 
     public float height = 3.0f; 
     public float damping = 5.0f; 
     public bool smoothRotation = true; 
     public bool followBehind = true; 
     public float rotationDamping = 10.0f; 

     void Update() { 
       Vector3 wantedPosition; 
       if(followBehind) 
         wantedPosition = target.TransformPoint(0, height, -distance); 
       else 
         wantedPosition = target.TransformPoint(0, height, distance); 

       transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping); 

       if (smoothRotation) { 
         Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up); 
         transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping); 
       } 
       else transform.LookAt (target, target.up); 
     } 
} 

More information about my work