首页 > 解决方案 > 2D 相机跟随 - 游戏对象在跳跃时闪烁

问题描述

是的,我在发布之前阅读了大约 30 个不同的类似标题。但是,没有任何与我需要的东西相关的东西。我正在尝试将相机的 Y 轴设置为跟随玩家移动通过关卡;但是,我不希望相机在跳跃时上下移动,所以我跟随相机的 transform.position.y 而不是玩家的。

 void Pjump()
{

    if (Input.GetKeyDown(KeyCode.Space) && onFloor==true 
        || Input.GetKeyDown(KeyCode.W) && onFloor==true)
    {


            player.velocity = new Vector2(0, jump);
            onFloor = false;
        isJumping = true;  // Static Variable to pass onto CamFollow script


    }

}

isJumping 在 OnCollisionEnter2d() 内设置为 false,并在 FixedUpdate() 内调用。

现在为 CamFollow 脚本

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

 public class CamFollow : MonoBehaviour
 {


private GameObject p1;

private bool isFollow;

[Header("Camera Offset Values")]
[SerializeField]
private float xOff;
[SerializeField]
private float yOff;

void Start()
{
    p1 = GameObject.FindWithTag("Player");
    isFollow = true;

}

void FixedUpdate()
{
    if (isFollow)
    {

       if (Pmove.isJumping == false) // This code works fine
       {
            transform.position = new Vector3(p1.transform.position.x + xOff, p1.transform.position.y + yOff,
                                                                                                      transform.position.z);
        }

       if(Pmove.isJumping == true) // This is where the problem is: Y-Axis
        {

            transform.position = new Vector3(p1.transform.position.x + xOff, transform.position.y + yOff,
                                                                                                     transform.position.z);

        }


    }
  }
 }

当玩家跳跃时,玩家和所有非 UI 对象都会消失,直到玩家接触到地面。

标签: c#unity3d

解决方案


推荐阅读