【问题标题】:Replace Input.GetAxis to touch替换 Input.GetAxis 来触摸
【发布时间】:2022-06-11 20:58:49
【问题描述】:

我想在左右移动时对汽车的旋转系统进行编码。我为此使用以下代码。

float steer = Input.GetAxis("Horizontal");

    float finalangel = steer * 45f;
    wheelcoll[0].steerAngle = finalangel; 
    

但我想为手机设置它。当用户触摸手机屏幕并将手放在手机屏幕上时,汽车会向左行驶并停留。当用户将手从手机上移开时,汽车会回到原来的位置。但在执行此过程时,我希望汽车转向正确的方向。

我该怎么做?

我也试过这个:

[SerializeField] Rigidbody rb;

public  Vector3 targetpostion;
public int Speed;

public bool FirstLaneBlueCar;
public bool BlueCar;

public Vector2 Xpos;

public float rotatlerptime;

bool rottrue;

void Start()
{
    rottrue = false;
    BlueCar = false;
}
 void Update()
{


    if (Input.GetMouseButtonDown(0))
    {
       
        BlueCar = true;
        rottrue = true;
        LeftButtonPressed();

    }else if (Input.GetMouseButtonUp(0))
    {
       
       
        BlueCar = true;
        rottrue = true;
       


        LeftButtonPressed();

      
    }

    if (!rottrue)
    {
        if (transform.position.x <= 4f)
        {
            Debug.Log(">-.5");
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, 0), rotatlerptime * Time.deltaTime);

        }

        if (transform.position.x >= 3f)
        {
            Debug.Log(">.5f");
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, 0), rotatlerptime * Time.deltaTime);
        }
    }
   
}

private void FixedUpdate()
{
    transform.Translate(targetpostion, Space.World);

    if (BlueCar)
    {
        if (FirstLaneBlueCar)
        {
            
            if (rottrue)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, -60f, 0), rotatlerptime * Time.deltaTime);
                Invoke("rot2", .1f);
            }

            Invoke("left", .1f);

        }
        else
        {


          
            if (rottrue)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 60f, 0), rotatlerptime * Time.deltaTime);
                Invoke("rot2", .1f);
            }

            Invoke("right", .1f);
        }

    }



   


}



public void rot2()
{
    rottrue = false;
}

void left()
{
    transform.position = Vector3.Lerp(transform.position, new Vector3(-Xpos.y, transform.position.y, transform.position.z), .08f);
}

 void right()
{
    transform.position = Vector3.Lerp(transform.position, new Vector3(-Xpos.x, transform.position.y, transform.position.z), .08f);
}


public void LeftButtonPressed()
{
    if (FirstLaneBlueCar)
    {
        FirstLaneBlueCar = false;

    }
    else
    {

        FirstLaneBlueCar = true;

    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可以跟踪第一个touch位置,并在Update方法中进行相应的计算,比较之前的触摸位置和当前的位置。

    private void Update()
    {
        if (Input.touches.Length < 1)
            return;
    
        var touch = Input.touches[0];
        var deltaPosition = touch.deltaPosition;
    
        // Move the car according to the shift in touch position
    }
    

    您还可以在触摸刚开始时保存触摸 ID,以便稍后跟踪相同的触摸(用户可以在屏幕上有多个手指)。

    为此使用Touch.fingerId

    【讨论】:

    • 抱歉没有注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多