0
votes

I am working on a script to move an object back anf forth based on swipe similarly to a game called Sky Rusher on the iOS App Store. The movement in the original game lets you swipe in any direction and an object moves in the same direction. However, the object also "bounces" for lack of a better term. For example, if you swipe to the left, the object will tilt to the left and then tilt back to its original position. For the best example I can give, please take a look at this video for a demonstartion of the game:

Sky Rusher Gameplay

This is the code I currently have (the object also doesn't move back and forth when swiping, not sure what==y that is but have an idea on how to fix it):

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

public class MovePlayer : MonoBehaviour
{
private Vector3 currentPos;
private Vector3 touchPos;
private float screenWidth;
private float screenHeight;
private float touchX;
private float touchY;
private float objectX;
private float objectY;

// Start is called before the first frame update
void Start()
{
    touchX = 0;
    touchY = 0;
    screenWidth = (float)Screen.width / 2.0f;
    screenHeight = (float)Screen.height / 2.0f;
    currentPos = new Vector3(0.0f, 1.0f, 0.0f);
}

// Update is called once per frame
void Update()
{
    if(Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if(touch.phase == TouchPhase.Moved)
        {

            //touchPos = new Vector3((touch.position.x - screenWidth)/screenWidth, (touch.position.y - screenHeight)/screenHeight + 1, 0.0f);
            touchPos = new Vector3(touch.position.x, touch.position.y, 0.0f);

            touchX = (touchPos.x - screenWidth)/screenHeight - 1f;
            touchY = (touchPos.y - screenHeight)/screenHeight + 1f;

            //objectX = ((currentPos.x * screenWidth) - screenWidth)/screenWidth;
            //objectY = ((currentPos.y * screenHeight) - screenHeight)/screenHeight;
            objectX = currentPos.x;
            objectY = currentPos.y;

            objectX += (touchX - objectX) * 1.5f;
            //objectY += (touchY - objectY) * 1.5f;

            if(touchX >= 0.9f)
            {
                objectX+=0.05f;
            }
            else if(touchX <= -0.9f)
            {
                objectX-=0.05f;
            }

            currentPos = new Vector3(objectX, objectY, 0.0f);
            transform.position = currentPos;
        }
    }
}

void OnGUI()
{
    /*
    // Compute a fontSize based on the size of the screen width.
    GUI.skin.label.fontSize = (int)(Screen.width / 40.0f);

    GUI.Label(new Rect(20, 20, screenWidth, screenHeight * 0.25f),
        "Pos: x = " + (objectX.ToString("f2")) +
        ", y = " + objectY.ToString("f2"));

    GUI.Label(new Rect(20, 50, screenWidth, screenHeight * 0.25f),
    "Touch: x = " + (touchX.ToString("f2")) +
    ", y = " + (touchY.ToString("f2")));
    */
}
}

I need my object to tilt when moved similarly to how it is done in sky rusher. My game is played in a landscape orientation on an iOS Device using Unity Remote 5 and Unity 2018.3.

1

1 Answers

0
votes

Quick way to achieve this effect, if I understood correctly what you mean:

1) Each time touchX >= .9f (you are moving right) apply localRotation along Z axis with some angle. 2) Each time touchX <= -.9f (you are moving left) apply localRotation along Z axis with some negative angle.

To make this look smooth and not jumpy, apply rotation along several frames, first calculating target rotation, then using RotateTowards with some given speed. Here is yourr code slightly modified:

public float tiltEffectAngle = 20;
public float tiltEffectSpeed = 90f;

void Update() {
    var targetRotation = Quaternion.identity;
    if (Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Moved) {

            //touchPos = new Vector3((touch.position.x - screenWidth)/screenWidth, (touch.position.y - screenHeight)/screenHeight + 1, 0.0f);
            touchPos = new Vector3(touch.position.x, touch.position.y, 0.0f);

            touchX = (touchPos.x - screenWidth) / screenHeight - 1f;
            touchY = (touchPos.y - screenHeight) / screenHeight + 1f;

            //objectX = ((currentPos.x * screenWidth) - screenWidth)/screenWidth;
            //objectY = ((currentPos.y * screenHeight) - screenHeight)/screenHeight;
            objectX = currentPos.x;
            objectY = currentPos.y;

            objectX += (touchX - objectX) * 1.5f;
            //objectY += (touchY - objectY) * 1.5f;



            if (touchX >= 0.9f) {
                objectX += 0.05f;
                targetRotation = Quaternion.Euler(
                    transform.localEulerAngles.x,
                    transform.localEulerAngles.y,
                    tiltEffectAngle);
            } else if (touchX <= -0.9f) {
                objectX -= 0.05f;
                targetRotation = Quaternion.Euler(
                    transform.localEulerAngles.x,
                    transform.localEulerAngles.y,
                    -tiltEffectAngle);
            }

            currentPos = new Vector3(objectX, objectY, 0.0f);
            transform.position = currentPos;
        }
    }
    transform.localRotation = Quaternion.RotateTowards(transform.localRotation, targetRotation, tiltEffectSpeed * Time.deltaTime);
}