如何让一个物体围绕屏幕中心朝向鼠标统一旋转?
How do I make a object rotate around the center of the screen towards the mouse in unity?
所以我知道这听起来很混乱,但其实很简单。
这是问题所在:
我想使用 transform.RotateAround()
并让围绕屏幕中心旋转的播放器朝向鼠标。
请注意,这是在 Unity C# 2D
中
__
/ \
| |
\__/
那是玩家旋转的圆圈
o = 圆
__
/ \
| o (if the mouse is right here then the player should go where the o is)
\__/
对于糟糕的解释感到抱歉它非常复杂
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class hareketkodları : MonoBehaviour
{
//Classic Movement Codes //
public float YourPlayerSpeed = 8.2f;
public Rigidbody2D Rigidb;
Vector2 Movement_vc;
//Right Here, the codes you want//
public Camera PlayerMouseCam;
Vector2 PlayerMousePos;
void Update()
{
Movement_vc.x = Input.GetAxisRaw("Horizontal"); //I'm writing here, classic movement methods
Movement_vc.y = Input.GetAxisRaw("Vertical"); //I'm writing here, classic movement methods
// =========== the codes you want ============ //
PlayerMousePos = PlayerMouseCam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Rigidb.MovePosition(Rigidb.position + Movement_vc * YourPlayerSpeed * Time.fixedDeltaTime); //I'm writing here, classic movement methods
// =========== the codes you want ============ //
Vector2 lookingDR = PlayerMousePos - Rigidb.position;
float angle = Mathf.Atan2(lookingDR.y, lookingDR.x) * Mathf.Rad2Deg - 90f;
Rigidb.rotation = angle;
}
}
我为你而写 :) 我希望你能实现它,如果你遇到困难,给我发短信或联系我。
所以我知道这听起来很混乱,但其实很简单。
这是问题所在:
我想使用 transform.RotateAround()
并让围绕屏幕中心旋转的播放器朝向鼠标。
请注意,这是在 Unity C# 2D
__
/ \
| |
\__/
那是玩家旋转的圆圈 o = 圆
__
/ \
| o (if the mouse is right here then the player should go where the o is)
\__/
对于糟糕的解释感到抱歉它非常复杂
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class hareketkodları : MonoBehaviour
{
//Classic Movement Codes //
public float YourPlayerSpeed = 8.2f;
public Rigidbody2D Rigidb;
Vector2 Movement_vc;
//Right Here, the codes you want//
public Camera PlayerMouseCam;
Vector2 PlayerMousePos;
void Update()
{
Movement_vc.x = Input.GetAxisRaw("Horizontal"); //I'm writing here, classic movement methods
Movement_vc.y = Input.GetAxisRaw("Vertical"); //I'm writing here, classic movement methods
// =========== the codes you want ============ //
PlayerMousePos = PlayerMouseCam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Rigidb.MovePosition(Rigidb.position + Movement_vc * YourPlayerSpeed * Time.fixedDeltaTime); //I'm writing here, classic movement methods
// =========== the codes you want ============ //
Vector2 lookingDR = PlayerMousePos - Rigidb.position;
float angle = Mathf.Atan2(lookingDR.y, lookingDR.x) * Mathf.Rad2Deg - 90f;
Rigidb.rotation = angle;
}
}
我为你而写 :) 我希望你能实现它,如果你遇到困难,给我发短信或联系我。