平滑转向C#Unity
我设法与我的卡丁车团结一致地向前前进。但是,当涉及到转弯时,这成为一个真正的挑战,我无法通过A&amp平稳地转动我的车辆; D键。
请帮忙!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KartController : MonoBehaviour
{
private Rigidbody rb;
// adding a speed variable
public float speed = 3.0f;
// adding a brake variable
public float BrakeSpeed = 1.0f;
// adding a rotation position variable
// Start is called before
void Start()
{
// refering to the game object component/class
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// adding movement for forward
if (Input.GetKey(KeyCode.W))
{
rb.velocity = new Vector3(speed * -2, 0, 0);
}
// adding movement for backward
if (Input.GetKeyDown(KeyCode.S))
{
rb.velocity = new Vector3(speed * 2, 0, 0);
}
// adding movement for rotation
float rotation_speed = 10f;
if (Input.GetButtonDown("Horizontal"))
{
transform.Rotate(0.0f, -Input.GetAxis("Horizontal") * rotation_speed, 0.0f);
}
Debug.Log(rotation_speed);
}
}
I have managed to go backwards and forwards with my kart in Unity. But when it comes to turning, it becomes a real challenge and I am unable to turn my vehicle smoothly with my a & d keys.
Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KartController : MonoBehaviour
{
private Rigidbody rb;
// adding a speed variable
public float speed = 3.0f;
// adding a brake variable
public float BrakeSpeed = 1.0f;
// adding a rotation position variable
// Start is called before
void Start()
{
// refering to the game object component/class
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// adding movement for forward
if (Input.GetKey(KeyCode.W))
{
rb.velocity = new Vector3(speed * -2, 0, 0);
}
// adding movement for backward
if (Input.GetKeyDown(KeyCode.S))
{
rb.velocity = new Vector3(speed * 2, 0, 0);
}
// adding movement for rotation
float rotation_speed = 10f;
if (Input.GetButtonDown("Horizontal"))
{
transform.Rotate(0.0f, -Input.GetAxis("Horizontal") * rotation_speed, 0.0f);
}
Debug.Log(rotation_speed);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有几种不同的选择,并且正在混合一些东西。
您的卡丁车是具有物理模拟的僵化体。要向前或向后移动它,您将设置卡丁车速度。如果对象在物理模拟中具有速度,则将尝试移动,因为在模拟中求解了标准物理公式。 IE距离移动=速度 *
旋转时间适用相同的概念。刚性体的角速度。如果对象具有角速度,则尝试相应地旋转。
使用变换旋转,您将直接设置对象的旋转。您基本上是在跳过物理模拟。如果您想在比赛开始时重置卡丁车的方向和位置,但不适用于在模拟中控制卡丁车,这是很好的。
您可以直接设置rb。离速度类似于设置卡丁车方向速度的方式。
根据游戏类型和现实主义的类型,您可能需要考虑控制加速度而不是速度。甚至是轮廓派...( https://docs.unity3d.com/manual/manual/wheelcollcolliderterorial.htmllorial.htmllorial..htmllliorial.htmllliorial.htmll )
You've got a few different options and you are mixing a few things up.
Your Kart is a Rigidbody with a physics simulation. To move it forward or backwards you are setting the Karts velocity. If an object has a velocity in the physics simulation it will try to move because in the simulation the standard physics formula are solved. I.e. distance moved = velocity * time
For rotation the same concepts apply. There is the angular velocity of a rigidbody. If an object has angular velocity it tries to rotate accordingly.
With transform Rotate you are setting the rotation of the object directly. You are basically skipping the physics simulation. This is good if you want to for example reset your kart orientation and position at the start of the race but is less suited to control your Kart in the simulation.
You could directly set rb.angularVelocity similar how you set the Karts directional velocity.
Depending on the type of game and the realism you might want to look into controlling the acceleration instead of the velocity. Or even wheelcollider... (https://docs.unity3d.com/Manual/WheelColliderTutorial.html)
您可以尝试以下代码选车,希望对您有帮助,谢谢
You can try the following code to select the car, I hope it can help you, thank you