unity3d 无法在Unity游戏项目中实现命令模式

qmb5sa22  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(141)

我一直在尝试在我的网球游戏统一项目中实现一个命令模式,我已经尝试了几个星期,但无济于事。首先,我显然有我的原始游戏脚本连接到我的球员gameobject(Character.cs),然后我首先尝试实现am接口(Command.cs),然后我创建了按键命令(RightCommand.cs,LeftCommand.cs)扩展到所述接口,然后我创建了一个调用程序(invoker.cs),它存储了keycommand并实现了它们,然后我尝试在Character.cs类中使用ivoker和keycommand,但当我运行它时,向左移动的时候什么也没发生。这是我的代码来得到一个更好的图片。
Command.cs

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

public interface Command 
{
    void Execute();
}

RightCommand.cs

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

public class RightCommand : Command
{
    Vector3 position;
    float speed = 3f;

    public RightCommand(Vector3 vector)
    {
        position = vector;
    }

    public void Execute()
    {
        position.x -= speed * Time.deltaTime;
    }
}

LeftCommand.cs

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

public class LeftCommand : Command
{
    Vector3 position;
    float speed = 3f;

    public LeftCommand(Vector3 vector)
    {
        position = vector;
    }

    public void Execute()
    {
        position.x += speed * Time.deltaTime;
    }
}

调用者

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

public class Invoker 
{
    Stack<Command> mCommands;

    public Invoker()
    {
        mCommands = new Stack<Command>();

    }
    public void Execute(Command command)
    {
        if (command != null)
        {
            mCommands.Push(command);
            mCommands.Peek().Execute();
        }
    }
}

cs-在这里,我尝试实现LeftCommand和调用程序,看看它是否能与左箭头键一起工作,但由于某种原因,它最终无法工作。

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

public class Character : MonoBehaviour
{
    public Leftmove left;
    float speed = 3f;
    float force = 10;
    bool hit;
    public Transform target;
    public Transform Tennisball;
    Animator animator;
    private Invoker minvoker;
    // Start is called before the first frame update

  

    void Start()
    {
        minvoker = new Invoker();
        animator = GetComponent<Animator>();
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 position = transform.position;
        Vector3 positiont = target.position;

        if (Input.GetKeyDown(KeyCode.F))
        {
            hit = true;
        }
        else if (Input.GetKeyUp(KeyCode.F))
        {
            hit = false;
        }

        if (Input.GetKey(KeyCode.LeftArrow) && hit == false)
        {
            Command command = new LeftCommand(position);
            minvoker.Execute(command);
            
        }

        if (Input.GetKey(KeyCode.RightArrow) && hit == false)
        {
            position.x -= speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.UpArrow) && hit == false)
        {
            position.z -= speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.DownArrow) && hit == false)
        {
            position.z += speed * Time.deltaTime;
        }

        if ((hit == true) && (Input.GetKey(KeyCode.LeftArrow)))
        {

            positiont.x += speed * Time.deltaTime;

        }

        if ((hit == true) && (Input.GetKey(KeyCode.RightArrow)))
        {

            positiont.x -= speed * Time.deltaTime;

        }

        if ((hit == true) && (Input.GetKey(KeyCode.UpArrow)))
        {
            positiont.z -= speed * Time.deltaTime;

        }

        if ((hit == true) && (Input.GetKey(KeyCode.DownArrow)))
        {
            positiont.z += speed * Time.deltaTime;

        }

        transform.position = position;
        target.position = positiont;

       
      
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Tennis ball"))
        {
            Vector3 dir = target.position - transform.position;
            //Vector3 horizo = transform.position;
            //horizo.y = speed * Time.deltaTime;
            other.GetComponent<Rigidbody>().velocity = dir.normalized * force + new Vector3(0, 10, 0);
            Vector3 side = Tennisball.position - transform.position;
            if (side.x >= 0)
            {
                animator.Play("backhand play");
            }

            else
            {
                animator.Play("forehand play");
            }
            //animator.Play("forehand play");

        }

    }

}
plupiseo

plupiseo1#

这里的问题是,您只更改了Vector3结构体,而没有更改实际的Trasnform,因此您可以执行类似的操作

Transform targetTransform;

public RightCommand(Transform transform)
{
    targetTransform = transform;
}

public void Execute()
{
    var targetPosition = transform.position;
    targetPosition.x -= speed * Time.deltaTime;
    targetTransform.position = targetPosition;
}

Character.cs中,将调用更改为

if (Input.GetKey(KeyCode.LeftArrow) && hit == false)
    {
        Command command = new LeftCommand(transform);
        minvoker.Execute(command);       
    }

相关问题