简体   繁体   中英

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. 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. I am fairly new to C# and have tried a few solutions such as Vector2.MoveTowards and 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

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. Thanks.

Unity's Input.GetKeyUp only returns true on the frame when you release the spacebar. 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.

Also, this is unrelated, but you don't want to call GameObject.Find(string) every time you move the character. Instead, call it once in the Start() method and then store the result to a field.

你有没有尝试过

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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