简体   繁体   中英

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.

However, it does not work...

What did I do wrong? I have attached image of my script (C#) here.

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? Are you getting any exceptions? Also add Debug.Log (player.transform.position.ToString ()) to see if it is showing the right values. Are you sure you player object name is cat and not Cat, it is case sensitive. Check on those things and let me know if you figured it out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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