英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论