Unity:角色移动但不显示

问题描述 投票:0回答:1

我正在制作一个 2D 平台游戏,当我按 D 键时,角色应该向右移动,当我按 A 键时,角色应该向左移动,并且它确实移动,角色的 X 位置在检查器中正确更改。然而,当我放置 BG 图像来测试它时,BG 图像保持静止。空格键也没有任何作用。我对 Unity 还很陌生。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.Serialization.Formatters;
using UnityEngine;
using UnityEngine.UIElements;

public class movement : MonoBehaviour
{
    public Rigidbody2D rigidBody;
   
    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.D) == true)
{
    rigidBody.velocity = UnityEngine.Vector2.right * 5 * Time.deltaTime; 
}  
        if(Input.GetKey(KeyCode.A) == true)
{
    rigidBody.velocity = UnityEngine.Vector2.left * 5 * Time.deltaTime;
}  
        if(Input.GetKeyDown(KeyCode.Space) == true)
{
    rigidBody.velocity = UnityEngine.Vector2.up * 5 * Time.deltaTime; 
}  
       if(Input.GetKey(KeyCode.S) == true)
{
    rigidBody.transform.localScale = new UnityEngine.Vector2(transform.localScale.x,  (float)0.5);
} 
    else{
        rigidBody.transform.localScale = new UnityEngine.Vector2(transform.localScale.x, 1);
    } 
    }
}

我希望它能移动到背景图像,并且空格键可以工作。

如果需要的话,这是相机脚本:

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

public class follow : MonoBehaviour
{
    public Transform player;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(player.position.x + 6, player.position.y, -10);
    }
}

玩家已设置为角色。

c# unity-game-engine 2d game-development
1个回答
0
投票

这对我来说效果很好。我可以在检查器中检查它。但动作太小了

我改变这个

rigidBody.velocity = UnityEngine.Vector2.right * 5 * Time.deltaTime; 

到此

rigidBody.velocity = UnityEngine.Vector2.right * 1000 * Time.deltaTime; 

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.