简体   繁体   English

平稳移动角色

[英]Moving a character smoothly

I would like to get a coroutine to move an uncontrolled unit across the screen.我想让一个协程在屏幕上移动一个不受控制的单元。 I built in some coroutines that wait for 5 seconds and then flip the npc so that the npc is facing the other way.我内置了一些等待 5 秒然后翻转 npc 的协程,使 npc 面向另一边。

The routine will do this every 5 seconds, however my unit remains stuck and does not move in the direction I wanted the npc to move in eg move right for 5 seconds and move left for 5 seconds.例程将每 5 秒执行一次,但是我的单位仍然卡住并且没有朝我希望 npc 移动的方向移动,例如向右移动 5 秒并向左移动 5 秒。

moverightleft script左右移动脚本


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

public class MoveRightLeft : MonoBehaviour
{
    public bool isOn;

    public bool _isFacingRight;


    // Start is called before the first frame update
    void Start()
    {

        _isFacingRight = transform.localScale.x > 0;



        InvokeRepeating("MoveRightMoveLeft", 0, 5);

    }

    // Update is called once per frame
    void Update()
    {

    }


    void MoveRightMoveLeft()
    {
        if (isOn)
        {


            Debug.Log("ison");


            GameObject.Find("SceneHandler").GetComponent<Routines>().startwalkCoroutine(this.gameObject, "right");


            if (!_isFacingRight)
                Flip();

            isOn = false;
        } else
        {


            Debug.Log("isoff");


            GameObject.Find("SceneHandler").GetComponent<Routines>().startwalkCoroutine(this.gameObject, "left");


            if (_isFacingRight)
                Flip();

            isOn = true; 
        }

    }


    private void Flip()
    {

        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);

        _isFacingRight = transform.localScale.x > 0;


    }

}

Routines script例程脚本


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


//burrow needed functions from this class

public class Routines : MonoBehaviour
{


    public void runCoroutine(IEnumerator coroutine)
    {


        StartCoroutine(coroutine);

    }


    
    public void startwalkCoroutine(GameObject animobj, string direction)
    {

        StartCoroutine(walkCoroutine(animobj, direction));

    }


    public void stopwalkCoroutine(GameObject animobj, string direction)
    {

        StopCoroutine(walkCoroutine(animobj, direction));

    }


    public IEnumerator walkCoroutine(GameObject animobj, string direction)
    {

        //while (target > 0)
        //{

        while (true) { 

            if (direction == "right")
            {

                float origposx = animobj.transform.position.x;


                float posmovedbyx = origposx + 3;



                //target = target + 20; 


                animobj.transform.position = new Vector3(posmovedbyx, animobj.transform.position.y, animobj.transform.position.z);


            //}


         } else if (direction == "left")
        {

            float origposx = animobj.transform.position.x;


            float posmovedbyx = origposx - 3;



            //target = target + 20;


            animobj.transform.position = new Vector3(posmovedbyx, animobj.transform.position.y, animobj.transform.position.z);


            //}


        }





        yield return new WaitForSeconds(1);


        }

    }




    

    }


    #endregion

You can replace all of that with this:你可以用这个替换所有这些:

public class Mover : MonoBehaviour
{
    public Vector3 directionToWalkTowards = Vector3.forward;
    private bool isWalkingTheOtherWay;

    public void Update()
    {
        // This is the remainder operator. It works like this: 7 % 3 = 1, 13 % 10 = 3, 5 % 3 = 2
        // It is saying if the remainder of the time since the start divided by ten is greater than 5 walk in the one direction, otherwise walk into the other, 
        // resulting in her walking 5 seconds in the one, then 5 secs in the other direction
        if (Time.time % 10 > 5) 
        {
            if (isWalkingTheOtherWay)
            {
                transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);
                isWalkingTheOtherWay = false;
            }
            transform.position += directionToWalkTowards * Time.deltaTime;
        }
        else
        {
            if (!isWalkingTheOtherWay)
            {
                transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);
                isWalkingTheOtherWay = true;
            }
            transform.position -= directionToWalkTowards * Time.deltaTime;
        }
    }
}

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

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