top of page

Looking Hyper

  • thetperson314
  • Aug 20, 2021
  • 1 min read

ree

This script will help you to create movement using the mouse or touch (in android).

This type of movement is one of the most common in hyper casual games.



The code consists of 3 main parts:


  1. Getting the mouse position (if in editor)

  2. Getting the finger position (if in android)

  3. moving the player according to the position



using UnityEngine;

public class PlayerMovementScript : MonoBehaviour
{
    public float speed = 10f;
    Vector3 mouseLastPos = Vector3.zero;
    public float barrierXmin = -4f;
    public float barrierXMax = 4f;
    Rigidbody rigidbody;
    
   void Start()
   {
       rigidbody = GetComponent<Rigidbody();
   }
   
   void Update()
   {
#if UNITY_ANDROID && !UNITY_EDITOR
       if (Input.touchCount > 0)
       {
            Vector3 fingerPos = Input.touches[0].position;
            fingerPos.z = 10;
            fingerPos = Camera.main.ScreenToWorldPoint(fingerPos);
            if (mouseLastPos == Vector3.zero)
            {
                mouseLastPos = fingerPos;
            }

            fingerPos = new Vector3(fingerPos.x,       this.transform.position.y, this.transform.position.z);

            MovePlayer(fingerPos, barrierXmin, barrierXMax);
        }
        else
        {
            mouseLastPos = Vector3.zero;
        }
#endif
#if UNITY_EDITOR

        if (Input.GetMouseButton(0))
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 10;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            if (mouseLastPos == Vector3.zero)
            {
                mouseLastPos = mousePos;
            }
            mousePos = new Vector3(mousePos.x, this.transform.position.y, this.transform.position.z);
            MovePlayer(mousePos, barrierXmin, barrierXMax);
        }
        else
        {
            mouseLastPos = Vector3.zero;
        }
#endif
    }
    /// <summary>
    /// Moving the player in between the min and max X positions
    /// </summary>
    /// <param name="mousePos">In World Point</param>
    /// <param name="barrierMin">The x position</param>
    /// <param name="barrierMax">The x position</param>
    public void MovePlayer(Vector3 mousePos, float barrierMin, float barrierMax)
    {
        int dir = 0;
        dir = (mouseLastPos.x < mousePos.x) ? 1 : -1;
        float xPos = this.transform.position.x + Mathf.Abs(mouseLastPos.x - mousePos.x) * dir;
        xPos = Mathf.Clamp(xPos, barrierMin, barrierMax);
        this.transform.position = new Vector3(xPos, this.transform.position.y, this.transform.position.z);
        mouseLastPos = mousePos;
    }
}

 
 

Recent Posts

See All

FAVORITES

Try playing Core Keeper and learn from it's sound design!

From the sound effect to the music - this is a masterpiece!

WHAT'S NEW?

New game on Steam and a new mobile video asset are soon to be published!!

I AM THINKING ABOUT

AI is on the rise. Being able to use it in the right cases and explaining the task correctly is no easy task but once you finish a full task without writing a line of code - your world will change!

CONTACT ME

Use any social media to contact me. For any question about programing and project I prefer using my Discord server, See you there!

© 2021 by Amit Klein. Nerd Head.

bottom of page