A minigame of spacecrafts and learning

angeljrp

angeljrp

Posted on May 29, 2024

A minigame of spacecrafts and learning

Hey everyone!

I'm super excited to share my first experience with you all as an internship. Today, I delved into the world of game development, tasked with creating a mini-game for our upcoming collection of games. And let me tell you, it's been quite the journey already!

Today, I rolled up my sleeves and delved into the world of game development, all in service of creating an engaging experience for our audience. With our game, not only do we aim to entertain, but we also want to sneak in some learning too. Picture this: a space-themed game for two players, reminiscent of the classic Space Invaders. But here's the twist - instead of shooting down aliens, players have to navigate their little spacecraft through space to collect floating numbers. Why? Well, to solve equations, of course! It's an exciting way to engage kids with math concepts in a fun and interactive way.

However, like any project, I hit a roadblock. You see, I've been grappling with adapting the game mechanic for mobile devices. Specifically, I'm struggling to figure out how to make the player's spacecraft draggable on a phone screen. I've tinkered with some code, but alas, it's not quite behaving as expected. It moves, but not in the way I want it to - it hovers on top of the screen and doesn't quite reach where it's supposed to go.

This is what i used for this problem:

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

public class TouchControl : MonoBehaviour
{
    //Public Variables
    public GameObject player1;
    public GameObject player2;
    //A modifier which affects the rackets speed
    public float speed;
    //Fraction defined by user that will limit the touch area
    public int frac;

    //Private Variables
    private float fracScreenWidth;
    private float widthMinusFrac;
    private Vector2 touchCache;
    private Vector3 player1Pos;
    private Vector3 player2Pos;
    private bool touched = false;
    private int screenHeight;
    private int screenWidth;
    // Use this for initialization
    void Start()
    {
        //Cache called function variables
        screenHeight = Screen.height;
        screenWidth = Screen.width;
        fracScreenWidth = screenWidth / frac;
        widthMinusFrac = screenWidth - fracScreenWidth;
        player1Pos = player1.transform.position;
        player2Pos = player2.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        //If running game in editor
#if UNITY_EDITOR
        //If mouse button 0 is down
        if (Input.GetMouseButton(0))
        {
            //Cache mouse position
            Vector2 mouseCache = Input.mousePosition;
            //If mouse x position is less than or equal to a fraction of the screen width
            if (mouseCache.x <= fracScreenWidth)
            {
                player1Pos = new Vector3(-3.70f, Mathf.Clamp(mouseCache.y / screenHeight * speed, -3, 3.5f), 0.5f);
            }
            //If mouse x position is greater than or equal to a fraction of the screen width
            if (mouseCache.x >= widthMinusFrac)
            {
                player2Pos = new Vector3(15.75f, Mathf.Clamp(mouseCache.y / screenHeight * speed, -3, 3.5f), 0.5f);
            }
            //Set touched to true to allow transformation
            touched = true;
        }
#endif
        //If a touch is detected
        if (Input.touchCount >= 1)
        {
            //For each touch
            foreach (Touch touch in Input.touches)
            {
                //Cache touch position
                touchCache = touch.position;
                //If touch x position is less than or equal to a fraction of the screen width
                if (touchCache.x <= fracScreenWidth)
                {
                    player1Pos = new Vector3(-3.70f, Mathf.Clamp(touchCache.y / screenHeight * speed, -3, 3.5f), 0.5f);
                }
                //If mouse x position is greater than or equal to a fraction of the screen width
                if (touchCache.x >= widthMinusFrac)
                {
                    player2Pos = new Vector3(15.75f, Mathf.Clamp(touchCache.y / screenHeight * speed, -3, 3.5f), 0.5f);
                }
            }
            touched = true;
        }
    }

    //FixedUpdate is called once per fixed time step
    void FixedUpdate()
    {
        if (touched)
        {
            //Transform rackets
            player1.transform.position = player1Pos;
            player2.transform.position = player2Pos;
            touched = false;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

But fear not! This is where you all come in. I'm reaching out to our amazing community for any suggestions or ideas on how to tackle this challenge. Whether you're a seasoned developer or just have a knack for problem-solving, I'm all ears!

So, if you've got any thoughts or insights to share, please don't hesitate to drop them in the comments below. Together, I'm confident we can crack this code and create something truly special.

Thanks for joining me on this adventure, and stay tuned for more updates on my internship journey!

πŸ’– πŸ’ͺ πŸ™… 🚩
angeljrp
angeljrp

Posted on May 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related