如何将滑动速度和位置转换为物体速度 [英] How do I translate swipe speed and position to object velocity

查看:219
本文介绍了如何将滑动速度和位置转换为物体速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款踢球场式"游戏,目的是自学Unity 3d.我具有所需的界面和功能,因此游戏看起来像这样:

I am developing a "kick the fieldgoal" type game for the purpose of teaching myself Unity 3d. I have the interface and functionality I want, such that the game looks like this:

我在OnUpdate中使用一些代码来检测何时首次按下鼠标按钮(或触摸阶段开始)以及何时释放鼠标按钮(或触摸阶段结束),并计算滑动方向,距离和持续时间.我想将这些信息转换为初始速度以启动"足球,但看来数学似乎超出了我.

I'm using some code in OnUpdate to detect when the mouse button is first pressed (or touch phase begins) and when it is released (or touch phase ends) and calculate the swipe direction, distance and duration. I want to translate this information into an initial velocity to "launch" the football, but the math is slightly beyond me, it seems.

我从这样的事情开始:

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

在我的初始分辨率和宽高比下,这当然效果很好,但是一旦我改变其中任何一个,它都会惨遭失败.我希望代码与分辨率和长宽比无关,或者至少是相对的.上面代码中使用的数字是完全任意的,并且基于迭代测试发现在我的初始分辨率下可以产生理想的结果.

This of course worked well at my initial resolution and aspect ratio, but fails miserably as soon as I change either one. I'd like the code to be more resolution and aspect ratio agnostic, or at least, relative. The numbers used in the code above are completely arbitrary and were discovered to produce desirable results at my initial resolution based on iterative testing.

有什么建议吗?

推荐答案

想法是获取起点和终点之间的距离和持续时间,并将其用作速度.

The idea is to take the distance and duration between the start and end point and use them for the velocity.

我使用ScreenToWorldPoint来将鼠标位置从屏幕坐标转换为世界坐标,这样它在世界中的距离才是最重要的,而不是屏幕的像素密度或分辨率.

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

预期的最小/最大值是在进行任何调整之前获得的功率值.要获得良好的价值,请先进行慢速/短时滑动(最小),快速进行长时(最大)

The expected min/max values are the values of power you get before any adjustments are made. To get good values you do a slow/short swipe for min and a fast long for max.

所需的最小值/最大值是可以产生理想结果的任意值.

The desired min/max values are your arbitrary values that makes desirable results.

您可能需要根据游戏的方向旋转速度.现在向上滑动会显示正y值,向右滑动会显示正x值.

You might need to rotate the velocity depending on how your game is orientated. Right now a swipe up gives positive y values and a swipe right gives 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
    }

这篇关于如何将滑动速度和位置转换为物体速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆