【问题标题】:Moving a character smoothly平滑移动角色
【发布时间】:2021-10-17 22:33:03
【问题描述】:

我想要一个协程来在屏幕上移动一个不受控制的单元。我内置了一些协程,等待 5 秒,然后翻转 npc,使 npc 面向另一个方向。

例程将每 5 秒执行一次,但是我的单位仍然卡住并且没有朝我希望 npc 移动的方向移动,例如向右移动 5 秒,向左移动 5 秒。

向左移动脚本


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;


    }

}

例程脚本


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

【问题讨论】:

    标签: c# unity3d routines


    【解决方案1】:

    你可以用这个替换所有这些:

    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;
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 2023-03-26
      • 1970-01-01
      • 2017-11-07
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多