mercredi 28 décembre 2022

Can't implement the command pattern at all in Unity game project

I have been trying to implement a command pattern in my Tennis game unity project, I have tried this for a couple weeks to no avail. First I obviously have my original gamescript that connects to my Player gameobject (Character.cs), then I tried first by implementing am interface (Command.cs), then I created the keycommands (RightCommand.cs, LeftCommand.cs) that extend to said interface, then I created an invoker (invoker.cs) that store the keycommands and implements them, then I tried to use the ivoker and keycommands in my Character.cs class but when I ran it, nothing happened when trying to move left. Here's my code to get a better picture.

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

Invoker

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

Character.cs - here I tried to implement the LeftCommand and invoker to see if it would work with the left arrow key, but it ended up not working for some reason.

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");

        }

    }


}

Aucun commentaire:

Enregistrer un commentaire