如何使相机按照unity3d C#的对象?(How do I make a camera follo

2019-09-17 12:03发布

我有一个叫做球的对象,我增加了键盘交互它(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
    }
}

我得到错误“资产/ ballmain.cs(18,44):错误CS1612:不能修​​改‘UnityEngine.Transform.position’的值类型返回值考虑在临时变量中存储的值。”有谁知道答案吗? 如果我注释掉关于相机的代码,球可以左右移动。

Answer 1:

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

简单关注

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


Answer 2:

如果你只是单纯的想跟随目标对象对准摄像头的位置,你想要的方式,使相机的目标对象的孩子,其余的都行



Answer 3:

包括标准的移动资产到您的项目。 它包含了脚本部分一SmoothFollow2D.js代码。 与游戏物体装上此代码和初始化公共变量。 这将只是做的工作适合你。



Answer 4:

我发现这个简单而有用的团结的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相机跟踪脚本



Answer 5:

我这里还有一个剧本,我发现我的游戏开发过程中非常有用。 所以我给信贷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);
         }
}

对我的工作的更多信息



Answer 6:

-=在这些行:

   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


文章来源: How do I make a camera follow an object in unity3d C#?