【问题标题】:How do I make a object rotate around the center of the screen towards the mouse in unity?如何使对象围绕屏幕中心向鼠标统一旋转?
【发布时间】:2023-04-01 20:51:01
【问题描述】:

所以我知道这听起来令人困惑,但它有点简单。 这是问题: 我想使用transform.RotateAround() 并让围绕屏幕中心旋转的播放器朝向鼠标。 请注意,这是在 Unity C# 2D 中

 __
/  \
|  |
\__/

这是玩家旋转的圆圈 o = 圆圈

 __
/  \
|   o   (if the mouse is right here then the player should go where the o is)
\__/

抱歉解释太复杂了

【问题讨论】:

    标签: c# unity3d geometry rotation


    【解决方案1】:
    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; 
        }
    } 
    

    我为你写的 :) 我希望你能做到,如果你遇到困难,给我发短信或联系。

    【讨论】:

    • 这不是我的问题,但是使用这个动作你帮助我完全改进了我的游戏并使其独一无二。
    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多