Swerve Control Unity C#

Viewed 55

Hi I am creating a hyper casual game with unity, but I have encountered a problem with the swerve control (I have also seen many git hubs but even these do not work perfectly) I've put this in my code:

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

public class PlayerMovement : MonoBehaviour
{
    private float lastframeposx;
    private float movefactorx;
    public float MoveFactorX => movefactorx;

    public Camera m_MainCam;

    private float speed = 2.0f;
    [SerializeField]
    GameObject character;
    [SerializeField] private float swerveSpeed = 0.5f;
    [SerializeField] private float maxSwerveAmount = 1f;



    // Start is called before the first frame update

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.position += Vector3.forward * speed * Time.deltaTime;  
        Cammina();
        /*Vector3 destra = Camera.main.ScreenToWorldPointt(Input.touches[i].position);
        transform.position += Vector3.zero  destra;*/
    }



    void Cammina()
    {
        if(Input.GetMouseButtonDown(0))
        {
            lastframeposx = Input.mousePosition.x;
            float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
            swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
            transform.Translate(swerveAmount, 0, 0);
        }
        else if (Input.GetMouseButton(0))
        {
            movefactorx = Input.mousePosition.x - lastframeposx;
            lastframeposx = Input.mousePosition.x;
            float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
            swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
            transform.Translate(swerveAmount, 0, 0);
        }
        else if(Input.GetMouseButtonUp(0))
        {
            movefactorx = 0f;
            float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
            swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
            transform.Translate(swerveAmount, 0, 0);
        }
    }

    /*void vaidoveschiacciato()
    {
        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            RaycastHit hit;

            if(Physics.Raycast(ray, out hit))
            {
                if(hit.collider != null)
                {
                    transform.position += hit.collider.GetComponent<Transform.position> * speed * Time.deltaTime;
                }
            }
        }
    }*/



}

1 Problem: he don't go when the finger is 2 Problem: How do I eliminate the movement from right to left (Without making it go out of the path)

(Langauge: C#)

1 Answers

The problem: When you swerve, it swerves just in the direction, there is no limits on how far it goes.

How I would fix this: I would put the movement to change it through a function. This could clamp it, so the higher the distance to the center of the track, the less it swerves. Or, you can altogether check if the distance is a maximum and then stop swerving. Note: you can use other functions to do this (they just have to flatten out the larger the input).

Smooth, good looking bell curve way

For example you could use a bell curve. Look one up if you've never seen one before. It is at it's highest possible output of one, at a zero input. When it gets hiher or lower, it outputs lower to zero.

the simplest equation is y = i-(x2). The lower i is (above 1), the wider the curve, or the larger the output is for a larger input. I made a graph here.

x can be the distance to the center of the track, so you should adjust i, so the maximum distance from the track is flat.

This output is what you should change swerveAmount to. The flatter parts of the graoh is how much you will swerve when you are that distance from the center (x axis)

Alternatively

You could just check the distance, and if it passes a certain distance don't translate it.

Let me know in the omments if there are any problems! :)

Related