如何让相机跟随unity3d C#中的对象?

che*_*ase 5 c# camera object unity-game-engine

我有一个名为Ball的对象,我添加了键盘交互性(WASD移动球)我需要相机留在后面并跟随球,但我得到错误.

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
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误"Assets/ballmain.cs(18,44):错误CS1612:无法修改'UnityEngine.Transform.position'的值类型返回值.考虑将值存储在临时变量中"有人知道答案吗?如果我注释掉关于相机的代码,球可以移动.

小智 6

干得好 。一个完整的代码。

简单跟随

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);
} 

    } 
Run Code Online (Sandbox Code Playgroud)

高级关注

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);
}
}
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您只是想跟随目标对象,请按照您想要的方式对齐相机的位置并使相机成为目标对象的子对象,其余的就可以了


Ski*_*izz 0

-=这些行中:

   camX = rigidbody.transform.position.x -=10;
   camY = rigidbody.transform.position.y -=10;
Run Code Online (Sandbox Code Playgroud)

是错的。将-=尝试修改rigidbody.transform.position. 你只是想要-

然而,就目前情况而言,相机不会跟踪目标 Z 位置的变化,如果相机旋转,也无法正确跟踪。要获得您需要的正确位置(以向量形式):-

cam_pos = target_pos - dist_to_target * cam_look_at
Run Code Online (Sandbox Code Playgroud)