简体   繁体   English

如何在 c# 中进行基于网格的运动以实现统一

[英]How do I make a grid based movement in c# for unity

I watched and tried to recreate 5 different types of grid movement for unity in c# and yet there was not a single video that worked or had it explained in an understandable way.我观看并尝试在 c# 中重新创建 5 种不同类型的网格运动以实现统一,但没有一个视频可以正常工作或以可理解的方式对其进行解释。 My last try was with this code and I just don't find any useful information for a beginner to work with... Are there good sites to learn how to recreate a Pokémon game in unity that actually works, and how do I make a grid-based movement with collision detection?我的最后一次尝试是使用这段代码,但我没有找到任何有用的信息供初学者使用......带有碰撞检测的基于网格的运动?

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

public class NewBehaviourScript : MonoBehaviour
{  
    private bool isMoving;
    private Vector3 origPos, targetPos;
    private float timeToMove = 0.2f;



    void Update()
    {
       if(Input.GetKey(KeyCode.W) && !isMoving)
       StartCoroutine/(MovePlayer(Vector3.up));

       if(Input.GetKey(KeyCode.A) && !isMoving)
       StartCoroutine/(MovePlayer(Vector3.up));


       if(Input.GetKey(KeyCode.S) && !isMoving)
       StartCoroutine/(MovePlayer(Vector3.up));

       if(Input.GetKey(KeyCode.D) && !isMoving)
       StartCoroutine/(MovePlayer(Vector3.up));
 
    }

    private IEnumerator MovePlayer(Vector3 direction)
    {
        isMoving = true;

        float elapsedTime = 0;

        origPos = transform.position;
        targetPos = origPos + direction;

        while(elapsedTime < timeToMove)
        {
            transform.position = Vector3.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
            elapsedTime += Time.deltaTime;
            yield return null;
        }

        transform.position = targetPos;

        isMoving = false;
    }
}

In the coroutine you can move the object by whatever axis you want (according to the key press) by using transform.position = new Vector3();在协程中,您可以使用 transform.position = new Vector3(); 将 object 移动到您想要的任何轴(根据按键); after that you can set a delay of 0.25 seconds by using yield return new WaitForSeconds(0.25f);之后,您可以使用 yield return new WaitForSeconds(0.25f) 设置 0.25 秒的延迟;

I hope I understood your question, Thanks!我希望我理解你的问题,谢谢!

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

相关问题 如何根据运动方向旋转带有 RigidBody 的游戏对象? 团结 | 3D | C# - How do I rotate a GameObject with a RigidBody based on it's movement direction? Unity | 3D | C# 使用 Unity 和 c# 时,如何使对象运动不那么断断续续且更加流畅? - When using Unity and c#, how do I make an objects movement less choppy and more fluid? 我如何制作一个基于六边形网格的移动系统,您可以在其中像 Unity 中的辐射 1 一样单击移动 - how would I make a hexagonal grid based movement system where you click to move like fallout 1 in Unity 如何使运动独立于 object 的旋转? C# 统一 - How to make movement independent of rotation of the object? C# Unity Unity C# 运动基于旋转? - Unity C# Movement based on rotation? 我如何将其制作成光滑的相机? (统一C#编程) - How do i make it into a smooth camera? (unity C# programming) 我如何在 C# 中为 Unity 制作骰子的分数计数器? - How do i make a score counter for a dice in C# for Unity? 如何使用 GetComponent 播放音频? (统一,C#) - How do I make audio play with GetComponent? (Unity, C#) 我是 c# 和 unity 的新手,我无法获得基于物理旋转的运动(c# unity) - I'm new at c# and unity and I am having trouble with getting a movement based on rotation with physics(c# unity) Unity C#中基于网格的A*寻路和环绕 - Grid based A* pathfinding and surrounding in Unity C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM