向旋转方向移动

时间:2015-01-22 04:07:47

标签: unity3d sprite unity3d-2dtools

我想点击屏幕的某个区域,让船朝着那个方向旋转,同时朝着船的方向加速...我让旋转工作但我似乎无法弄清楚如何申请向船舶方向施力:

我的代码将船发送到最终希望它去的地方,而不是它目前面临的地方:

Vector3 currentPosition = transform.position;

    if (Input.GetButton("Fire1")) {
        Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        moveDirection = moveToward - currentPosition;
        moveDirection.z = 0; 
        moveDirection.Normalize();
    }      

    float targetAngle = Mathf.moveDirection(vector.y, moveDirection.x);

    transform.rotation = Quaternion.Slerp(transform.rotation, 
                         Quaternion.Euler(0, 0, targetAngle), 
                                          rotateSpeed * Time.deltaTime);

    Vector2 direction;
    direction.x = Mathf.Cos((targetAngle * Mathf.Deg2Rad));
    direction.y = Mathf.Sin((targetAngle * Mathf.Deg2Rad));
    direction.Normalize();

    velocity.x = direction.x * moveSpeed;
    velocity.y = direction.y * moveSpeed;         
    rigidbody2D.velocity = velocity;         

1 个答案:

答案 0 :(得分:1)

这通常适用于您能够进行轮换的情况:

Vector2 direction = transform.forward;
rigidbody2D.velocity = direction * moveSpeed;

在不知道场景布局的情况下,很难确定。

如果正在以相反的方式移动船舶,请使用- transform.forward,或者如果船舶侧向移动,请使用transform.right