[英]How do I make a object rotate around the center of the screen towards the mouse in unity?
所以我知道这听起来令人困惑,但它很简单。 这是问题所在:我想使用transform.RotateAround()
并让播放器围绕屏幕中心 go 朝向鼠标旋转。 请注意,这是在 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;
}
}
我为你写的:)我希望你能做到,如果你遇到困难,给我发短信或联系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.