简体   繁体   中英

Unity C# problem with camera, camera goes white

I made a script for my camera for following the player.When I play the game, game view goes white, even if on scene view all is fine

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

public class FollowPlayer : MonoBehaviour
{

    public Transform player;
    public Vector3 playerpos;
    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {

        playerpos.x = player.position.x;

        transform.position = playerpos;
   
    }
}

The problem might be that the player is blocking the camera (because the camera is inside the player). Try adding some offset by adding a Vector3 as a variable and adding it on to the transform.position.

The offset could be used so that the camera is in front of the player, or in a third person angle.

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

public class FollowPlayer : MonoBehaviour
{

    public Transform player;
    public Vector3 playerpos;
    public Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        playerpos.x = player.position.x;
        transform.position = playerpos + offset;
        
    }
}

Hope this helps.

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