0

我想点击屏幕的一个区域,让一艘船向那个方向旋转,同时朝着船所面对的方向加速……我得到了旋转工作,但我似乎不知道如何在船朝向:

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

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;         
4

1 回答 1

1

这通常适用于您能够进行旋转的这种情况:

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

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

如果那是移动船相反的方式使用- transform.forward,或者如果船是移动的方式使用transform.right

于 2015-01-22T06:15:20.647 回答