【发布时间】:2021-01-04 10:41:48
【问题描述】:
我正在统一创建一个 2D 平台游戏,这是我的所有代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 0.5f;
public float gravity;
private float x;
private float y;
private bool isLeft = false;
private bool isRight = false;
private bool isUp = false;
private bool isDown = false;
public SpriteRenderer sp;
public Sprite player_Right;
public Sprite player_Left;
void Start()
{
Debug.Log(Screen.width + "," + Screen.height);
}
// Update is called once per frame
void Update()
{
x = transform.position.x;
y = transform.position.y;
if (Input.GetKeyDown("right"))
{
isRight = true;
}
if (Input.GetKeyDown("left"))
{
isLeft = true;
}
if (Input.GetKeyDown("up"))
{
isUp = true;
}
if (Input.GetKeyDown("down"))
{
isDown = true;
}
if (Input.GetKeyUp("right"))
{
isRight = false;
}
if (Input.GetKeyUp("left"))
{
isLeft = false;
}
if (Input.GetKeyUp("up"))
{
isUp = false;
}
if (Input.GetKeyUp("down"))
{
isDown = false;
}
if (isRight)
{
x += speed * Time.deltaTime;
sp.sprite = player_Right;
}
if (isLeft)
{
x -= speed * Time.deltaTime;
sp.sprite = player_Left;
}
if (isDown)
{
y -= speed * Time.deltaTime;
}
if (isUp)
{
y += speed * Time.deltaTime;
}
transform.position = new Vector2(x, y);
}
}
这是我的编辑图层屏幕:
背景图像附加到背景层,播放器附加到播放器层,所以播放器应该始终绘制在背景之上,对吗? 好吧,我是这么想的,当我向左走时按播放键,播放器就会消失在背景下。为什么会发生这种情况,我该如何解决?如果您需要查看编辑器的某些部分,请不要犹豫。
编辑:出于某种原因,标签会自动从统一 2d 切换到统一 3d
【问题讨论】:
标签: unity3d