Unity3d 使汽车上坡
我正在Unity中创建一个简单的汽车游戏,到目前为止,我已经创建了汽车和敌人的车辆。我有一座山,山上有一条倾斜的路,上上下下 不幸的是,到目前为止,我的代码,我的车没有成功地爬过山,或采取了很长时间的建设 我需要一段代码,使我的车停留在地面上行驶,或倾斜正确的角度,并成功地向山上行驶 这是我目前的代码:Unity3d 使汽车上坡,unity3d,unityscript,Unity3d,Unityscript,我正在Unity中创建一个简单的汽车游戏,到目前为止,我已经创建了汽车和敌人的车辆。我有一座山,山上有一条倾斜的路,上上下下 不幸的是,到目前为止,我的代码,我的车没有成功地爬过山,或采取了很长时间的建设 我需要一段代码,使我的车停留在地面上行驶,或倾斜正确的角度,并成功地向山上行驶 这是我目前的代码: var speed : float = 10.0; var rotationSpeed : float = 100.0; function Update () { var translation
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.CompareTag ("Road")) {
speed = 20;
rotationSpeed = 200;
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Road")) {
speed = 75;
rotationspeed = 750;
}
}
不要使用
transform.Translate()
移动汽车,它将无法适应障碍物。您必须使用rigidBody.AddForce()
您应该尝试以下内容:
var isGrounded : Boolean = false;
function Update () {
if(isGrounded && !Mathf.Approximately(Input.GetAxis ("Vertical"), 0f))
rigidBody.AddForce(transform.forward
* Input.GetAxis ("Vertical")
* speed
* Time.deltaTime);
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.CompareTag ("Road"))
isGrounded = false;
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Road"))
isGrounded = true;
}
我不断收到第一行“UCE001:”;“的错误。应在末尾插入分号。”脚本无法识别“bool”。当我将其连接到汽车上时,脚本将无法工作。它根本不会移动。你不应该只是“将我的脚本连接到汽车”我是从我的头脑中编写的。您应该从中学习,并相应地更改代码。这不是脚本编写服务。请使用附加了
刚体的CharacterController
。