admin管理员组文章数量:1417407
My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.
The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.
How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.
private void Update()
{
HandleInput();
MoveBike();
RotateObject();
}
private void HandleInput()
{
movementDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
movementDirection += Vector3.forward;
}
if (Input.GetKey(KeyCode.S))
{
movementDirection += Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
movementDirection += Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
movementDirection += Vector3.right;
}
movementDirection = movementDirection.normalized;
}
private void MoveBike()
{
transform.position += movementDirection * speed * Time.deltaTime;
}
private void RotateObject()
{
if(movementDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.
The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.
How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.
private void Update()
{
HandleInput();
MoveBike();
RotateObject();
}
private void HandleInput()
{
movementDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
movementDirection += Vector3.forward;
}
if (Input.GetKey(KeyCode.S))
{
movementDirection += Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
movementDirection += Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
movementDirection += Vector3.right;
}
movementDirection = movementDirection.normalized;
}
private void MoveBike()
{
transform.position += movementDirection * speed * Time.deltaTime;
}
private void RotateObject()
{
if(movementDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
Share
Improve this question
asked Jan 31 at 10:53
EPiedrahitaEPiedrahita
394 bronze badges
3
- 2 Is it possible that just your visuals (3D model etc) are simply rotated against this root object? – derHugo Commented Jan 31 at 13:56
- You need a starting point (A) and an end point (B); and a starting "facing" angle and a target facing angle (difference == the sweep) .... for the "turn". You can move from A to B while turning (rotating) the "sweep". The duration of the "rotation" is the same as the time to move from A to B (rotates faster when moving faster). – Gerry Schmitz Commented Jan 31 at 19:50
- Think about it. A "turn" has a starting point and an end point. If "we" can't agree on that, you won't understand the rest either. I'm talking "solutions"; not "why" his code "doesn't work"; which isn't a solution. I was expecting this moment. – Gerry Schmitz Commented Feb 1 at 18:55
1 Answer
Reset to default 0To get angle use Mathf.Atan2
function instead. Here's code to improve rotation of object
private void RotateObject()
{
if (movementDirection != Vector3.zero)
{
//That's for 3D. Use x and y for 2D
float angle = Mathf.Atan2(movementDirection.z, movementDirection.x) * Mathf.Rad2Deg;
//Get quaternion by angle. Rotation by z axis. For other change Vector.forward to another
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//Rotate object
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}
}
That may not work properly to you because it might rotate by unwanted axis or wrong angle. Edit code values to fit your requirements
本文标签: cGame Object facing the wrong direction when movingStack Overflow
版权声明:本文标题:c# - Game Object facing the wrong direction when moving - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745266502a2650634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论