我想点击屏幕的一个区域,让一艘船向那个方向旋转,同时朝着船所面对的方向加速……我得到了旋转工作,但我似乎不知道如何在船朝向:
我的代码将船发送到我最终希望它去的地方,而不是它当前面对的地方:
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;