1
\$\begingroup\$

So i am developing this game (as part of a larger game), where the player has to collect all the coins before moving on to the next level. Right now, I am using the space key on the keyboard to move the player but what I eventually want to do is move the player when it receives a button click from an arduino. The issue here is that I only want the player to move from one platform to the next; no where else. I have got some help with this but right now the player only moves from the first to the second platform and doesn't move anywhere else after that/ There are various platforms in each level and there are four levels Attached is the player controller script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//using System.IO.Ports;

public class PlayerControllerb : MonoBehaviour
{
    [Header("Debug")]
    [SerializeField] private PlatformProvider platformProvider;
    //SerialPort sp = new SerialPort("\\\\.\\COM4", 9600);
    //player = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
    private void Awake()
    {
        OnSceneLoaded();
        SceneManager.sceneLoaded -= OnSceneLoaded;
        SceneManager.sceneLoaded += OnSceneLoaded;
    }
    public void OnSceneLoaded()
    {

    }
    public void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        platformProvider = FindObjectOfType<PlatformProvider>();
    }
    public float Speed = 1;
    private int currentPlatformIndex;
    public Vector2 height;
    public float xMin, xMax, yMin, yMax;



    /*void Start()
    {
        if (!sp.IsOpen)
        { // If the erial port is not open 
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 250; // Timeout for reading
    }*/

    void Update()
    {

        // if (sp.IsOpen)
        { // Check to see if the serial port is open 
          // try
            {

                //string value = sp.ReadTo("EOL"); //Read the information
                //float amount = float.Parse(value);


                //if (/*amount > 25f)*/Input.GetKeyDown(KeyCode.Space))  //jump
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    //GoToNextPlatform();
                     GetComponent<Rigidbody2D>().AddForce(2* height, ForceMode2D.Impulse);
                    GoToNextPlatform();
                }
            }
            /*catch (System.Exception)
            {


            }*/
        }
    }
    [ContextMenu(nameof(GoToNextPlatform))]
    public void GoToNextPlatform()
    {
        currentPlatformIndex++;
        currentPlatformIndex = Mathf.Min(currentPlatformIndex, platformProvider.platforms.Length - 1);
        GetComponent<Rigidbody2D>().AddForce(2 * height, ForceMode2D.Impulse);

        GoToPlatform(currentPlatformIndex);
    }
    [ContextMenu(nameof(GoToPreviousPlatform))]
    public void GoToPreviousPlatform()
    {
        currentPlatformIndex--;
        currentPlatformIndex = Mathf.Max(currentPlatformIndex, 0);

        GoToPlatform(currentPlatformIndex);
    }
    private void GoToPlatform(int index)
    {
        StopAllCoroutines();
        StartCoroutine(MoveRoutine(platformProvider.platforms[index].transform.position));

    }
     IEnumerator MoveRoutine (Vector3 target)
    {
        while (!Mathf.Approximately(0, Vector3.Distance(transform.position, target)))
        {
            yield return new WaitForFixedUpdate();
            GetComponent<Rigidbody2D>().MovePosition(Vector3.MoveTowards(GetComponent<Rigidbody2D>().position, target, Time.deltaTime * Speed));
        }
    }

    private void FixedUpdate()
    {
        GetComponent<Rigidbody2D>().position = new Vector3

                   (
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                    );
    }
        /*void ApplicationQuit()
        {
            if (sp != null)
            {

                {
                    sp.Close();
                }
            }
        }*/

    }

There is a PlatformProvider:

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

public class PlatformProvider : MonoBehaviour
{
    public Platform[] platforms;
}

and this is attached to all the platforms:

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

public class Platform : MonoBehaviour
{

}

game

\$\endgroup\$
  • \$\begingroup\$ So the character can't fall? When the player hits the button the character teleports to the next coin? \$\endgroup\$ – Vaillancourt Dec 6 '19 at 1:40
  • \$\begingroup\$ @Vaillancourt right now, the player can fall. I have actually got some help with this but it still isn;t working...i should update the question \$\endgroup\$ – user19964 Dec 6 '19 at 2:47
  • \$\begingroup\$ Yes, updating the question with the current state is the way to go. \$\endgroup\$ – Vaillancourt Dec 6 '19 at 3:20
  • \$\begingroup\$ @Vaillancourt I have updated it now ...hopefully someone will be able to spot the problem \$\endgroup\$ – user19964 Dec 6 '19 at 4:09
  • \$\begingroup\$ Good luck :) (I'm not that familiar with Unity, so I can't offer much help on that.) \$\endgroup\$ – Vaillancourt Dec 6 '19 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.