c# - 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 - k&r fopen 和 fillbuf
- outlook - Outlook最多可以发送多少封邮件
- tornado - Pymysql通过tornado websocket从表中返回相同的结果而不是最后一行
- django - Django:将 DetailView 与表单相结合
- python - pipenv:如何强制 virtualenv 目录?
- java - Java KeyBindings 在几秒钟后停止工作
- python - 超参数估计的随机搜索和网格搜索的比较
- javascript - 在线时将数据加载到服务
- javascript - 未捕获的 ReferenceError:未定义 jqXHR
- api - Watson Assistant (Conversation) 能否将 API 作为对话的一部分?