简体   繁体   English

精灵未在unity2D C#中移动

[英]Sprite not moving in unity2D C#

I'm creating a game in unity 2D and in this game a GameObject called Moving_Truck is required to smoothly move into the scene from that left side. 我正在创建一个统一的2D游戏,在此游戏中,需要一个名为Moving_Truck的GameObject才能从左侧顺利移入场景。 as this will be required later, I tried to make the method run from an other code on another object, the object is called scene control and the script is called opening scene. 因为稍后将需要这样做,所以我尝试使该方法从另一个对象的另一个代码运行,该对象称为场景控制,而脚本称为打开场景。

the problem is when I push the space button the Moving_Truck game object does not move. 问题是当我按下空间按钮时,Moving_Truck游戏对象不会移动。 I am fairly new to C# and have tried a few solutions such as Vector2.MoveTowards and Vector2.Lerp. 我对C#相当陌生,并尝试了一些解决方案,例如Vector2.MoveTowards和Vector2.Lerp。 I have also modified my code multiple times trying to get this to work. 我也多次修改我的代码,以使其正常工作。 here is the most recent version of the codes: 这是代码的最新版本:

CharacterBase 角色库

using UnityEngine;
using System.Collections;

public class CharacterBase : MonoBehaviour {
    private float SprSize, HalfSprSize, Distance;
    public int run = 1;

    public void CharMove(int Dir, string Char, float speed, string BackgroundName)
    {
        var CharOBJ = GameObject.Find(Char);
        var BGOBJ = GameObject.Find(BackgroundName);
        SprSize = CharOBJ.GetComponent<Renderer>().bounds.size.x;
        HalfSprSize = SprSize / 2;
        Vector2 EndPos = new Vector2(BGOBJ.transform.position.x, CharOBJ.transform.position.y);
        Debug.Log(EndPos);
        CharOBJ.transform.position = Vector2.MoveTowards(CharOBJ.transform.position, EndPos, speed * Time.deltaTime);
    }
}

OpeningScene OpeningScene

using UnityEngine;
using System.Collections;

public class OpeningScene : CharacterBase {
    int Advance = 0, Run = 0;

    void Start ()
    {

    }

    void FixedUpdate()
    {
    if (Input.GetKeyUp("space"))
        {
            Run = 1;
            Debug.Log("Space Pressed");
        }
    if (Run == 1)
        {
            Run = 0;
            Advance += 1;
            switch (Advance)
            {
                case 1:
                    CharMove(-1, "Moving_Truck", 0.05f, "House_Front");
                    break;
                case 2:
                    CharMove(1, "Moving_Truck", 0.05f, "House_Front");
                    break;
            }
        }
    }
}

This is driving me nuts, I've been trying to fix it for about an hour or Two now, can someone please help, also sorry for the long question, just comment if you need more info. 这真让我发疯,我已经尝试将其修复大约一个小时或两个小时,有人可以帮忙吗?对于较长的问题也很抱歉,如果需要更多信息,请发表评论。 also please ignore the Dir Argument for now. 也请暂时忽略Dir Argument。 Thanks. 谢谢。

Unity's Input.GetKeyUp only returns true on the frame when you release the spacebar. 释放空格键时,Unity的Input.GetKeyUp仅在框架上返回true。 Because of this, CharMove will only be called that one frame you press the spacebar, and then only move 0.05f * timeDelta, which is probably going to be less than a pixel. 因此,按空格键仅将CharMove调用一帧,然后仅移动0.05f * timeDelta,这可能会小于一个像素。

Also, this is unrelated, but you don't want to call GameObject.Find(string) every time you move the character. 同样,这是无关的,但是您不想每次移动角色时都调用GameObject.Find(string)。 Instead, call it once in the Start() method and then store the result to a field. 而是在Start()方法中调用一次,然后将结果存储到字段中。

你有没有尝试过

 GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM