鼠标位置在世界坐标中以及玩家方向

huangapple go评论79阅读模式
英文:

mouse position in world point and player direction

问题

if (MoveByTouch) 
{   
    float deltaTime = Time.deltaTime;    
    float speed = runSpeed * deltaTime;
    r_Zpos += speed;            
    Vector3 mousePos = Input.mousePosition;
    mousePos.z = r_Zpos;
    Vector3 pos =  _camera.ScreenToWorldPoint(mousePos);
    r_XPos = Mathf.Lerp(r_XPos, pos.x, deltaTime * swipeSpeed);
}
r_transform.position = new Vector3(r_XPos, 0f, r_Zpos);
if (r_transform.position != r_LastPosition)
{
    r_transform.forward = Vector3.Lerp(r_transform.forward, (r_transform.position - r_LastPosition).normalized,
        speed);
}

r_LastPosition = r_transform.position;
英文:
        if (MoveByTouch) 
        {   
            float deltaTime = Time.deltaTime;    
            float speed = runSpeed * deltaTime;
            r_Zpos += speed;            
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = r_Zpos;
            Vector3 pos =  _camera.ScreenToWorldPoint(mousePos);
            r_XPos = Mathf.Lerp(r_XPos, pos.x, deltaTime * swipeSpeed);
        }
        r_transform.position = new Vector3(r_XPos, 0f, r_Zpos);
        if (r_transform.position != r_LastPosition)
        {
            r_transform.forward = Vector3.Lerp(r_transform.forward, (r_transform.position - r_LastPosition).normalized,
                speed);
        }

        r_LastPosition = r_transform.position;

I can't really find the problem here, I want my player follow the mouse position but still facing to the camera. but the player keep facing the mouse direction.
does anyone have any solution? thank you for taking your time!

答案1

得分: 0

你说你不想让玩家面向鼠标位置,但你在代码中通过设置r_transform.forward=...来实现了这一点。

如果你的目标是让玩家面向摄像机,那是一个旋转的问题。不过,transform.forward也与旋转相关联。
尝试将transform.position设置为鼠标的世界位置,就像你已经尝试过的那样,并通过操作transform.rotation使玩家独立面向摄像机。

如果你必须根据鼠标位置设置transform.forward,你可以尝试在玩家中创建一个子对象用于可视化表示,并使该子对象面向摄像机,同时父对象按你喜欢的方式移动。

也许可以查看这个链接,了解如何使物体看向某物:https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

英文:

You say that you don't want the player to face the mouse position, but you are doing so by setting r_transform.forward=... in your code.

If your goal is to make the player face the camera, that is a rotational problem. transform.forward is connected to rotation as well though.
Try setting the transform.position to the world position of your mouse as you already tried and make the player face the camera independantly by manipulating transform.rotation.

If you must set the transform.forward in relation to your mouse position, you can try to make a child object in your player for visual representation and make that child face the camera while the parent moves as you like.

Maybe have a look at this to learn how to make objects look at something: https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

huangapple
  • 本文由 发表于 2023年6月29日 09:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定