How to convert travel speed and position to object speed

I am developing a game like "kick the fieldgoal" with the goal of teaching myself Unity 3d. I have an interface and functionality, I want the game to look like this:

enter image description here

I am using some code in OnUpdate to detect when the mouse button is pressed (or the touch phase starts) and when it is released (or touches the phase of the phase) and calculate the direction, distance and duration of the movement. I want to translate this information into a starting velocity to get the football going, but the math seems to be a bit superior to me.

I started with something like this:

rigidbody.velocity = new Vector3(
    Mathf.Min(15, swipeDelta.x * .13f),
    Mathf.Min(20, swipeDelta.y * yRatio),
    5 / swipeDuration);

      

This worked well for my original resolution and aspect ratio, of course, but is hard to reset once I change one. I would like the code to be more permissive and proportional, or at least relative. The numbers used in the above code are completely arbitrary and have been discovered to produce the desired results at my original resolution based on iterative testing.

Any suggestions?

+3


source to share


1 answer


The idea is to take the distance and duration between the start and end points and use that for speed.

I am using ScreenToWorldPoint to get the position of the mouse from screen coordinates to world coordinates so that its world distance counts, not pixel density or screen resolution.

The expected min / max values ​​are the power values ​​that you get before any adjustments are made. To get good values, you do slow / short scrolling per minute and fast maximum for max.



The required min / max values ​​are your arbitrary values ​​that give the desired results.

You may need to rotate the speed depending on the orientation of your game. Right now, swipes give positive y values, and swipes on the right give positive x values.

Vector3 startPos;
float startTime;

void Update () 
{
    if (Input.GetMouseButtonDown(0))
    {
        //Store initial values
        startPos = Input.mousePosition;
        startTime = Time.time;
    }

    if (Input.GetMouseButtonUp(0))
    {
        //Get end values
        Vector3 endPos = Input.mousePosition;
        float endTime = Time.time;

        //Mouse positions distance from camera. Might be a better idea to use the cameras near plane
        startPos.z = 0.1f
        endPos.z = 0.1f

        //Makes the input pixel density independent
        startPos = Camera.main.ScreenToWorldPoint(startPos);
        endPos = Camera.main.ScreenToWorldPoint(endPos);

        //The duration of the swipe
        float duration = endTime - startTime;

        //The direction of the swipe
        Vector3 dir = endPos - startPos;

        //The distance of the swipe
        float distance = dir.magnitude;

        //Faster or longer swipes give higher power
        float power = distance / duration;

        //expected values are what power you get when you try 
        //desired values are what you want
        //you might want these as public values so they can be set from the inspector
        const float expectedMin = 50;
        const float expectedMax = 60;
        const float desiredMin = 15;
        const float desiredMax = 20;

        //Measure expected power here
        Debug.Log(power);

        //change power from the range 50...60 to 0...1
        power -= expectedMin;
        power /= expectedMax - expectedMin;

        //clamp value to between 0 and 1
        power = Mathf.Clamp01(power);

        //change power to the range 15...20
        power *= desiredMax - desiredMin;
        power += desiredMin;

        //take the direction from the swipe. length of the vector is the power
        Vector3 velocity = (transform.rotation * dir).normalized * power
    }

      

+2


source







All Articles