简体   繁体   English

为什么我不能在 (Unity)C# 中移动我的相机?

[英]Why cant I move my camera in (Unity)C#?

camera transform相机变换

I am trying to move my camera based on the players' movements on Y axis in Unity.我正在尝试根据玩家在 Unity 中 Y 轴上的移动来移动我的相机。

However, it does not work...然而,它不起作用......

What did I do wrong?我做错了什么? I have attached image of my script (C#) here.我在这里附上了我的脚本 (C#) 的图像。

and, Yes, I did attach this script with Main Camera.而且,是的,我确实在主摄像头上附加了这个脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    GameObject player;
    // Use this for initialization
    void Start () {
    this.player = GameObject.Find("cat");
    }

    // Update is called once per frame
    void Update () {
        Vector3 playerPos = this.player.transform.position;
        transform.position = new Vector3(
            transform.position.x, playerPos.y, transform.position.z);
    }
}

Make the player GameObject public and just drag and drop your player in the inspector in unity see if that works?将播放器 GameObject 设为公开,然后将您的播放器统一拖放到检查器中,看看是否有效? Are you getting any exceptions?你有任何例外吗? Also add Debug.Log (player.transform.position.ToString ()) to see if it is showing the right values.还要添加 Debug.Log (player.transform.position.ToString ()) 以查看它是否显示正确的值。 Are you sure you player object name is cat and not Cat, it is case sensitive.您确定您的玩家对象名称是 cat 而不是 Cat,它区分大小写。 Check on those things and let me know if you figured it out!检查这些事情,如果你想通了,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM