在团结3d的飞行字符

时间:2014-02-11 09:18:29

标签: unity3d game-physics unityscript

我目前使用以下脚本让我的角色在空中飞舞

#pragma strict

var cMotor: CharacterMotor; // reference to the CharacterMotor script

function Start(){ // get the CharacterMotor script at Start:
cMotor = GetComponent(CharacterMotor);
}

function Update(){ // move player upwards while F is pressed
if (Input.GetKey("f")){
cMotor.SetVelocity(Vector3.up*10.5);}
else if (Input.GetKey("up")){
cMotor.SetVelocity(Vector3.forward*10.5);
}
else if (Input.GetKey("left")){
cMotor.SetVelocity(Vector3.left*10.5);
}
else if (Input.GetKey("down")){
cMotor.SetVelocity(Vector3.back*10.5);
}
else if (Input.GetKey("right")){
cMotor.SetVelocity(Vector3.right*10.5);
}
else if (Input.GetKey("g")){
cMotor.SetVelocity(Vector3.down*10.5);
}
}

// This do-nothing function is included just to avoid error messages
// because SetVelocity tries to call it

function OnExternalVelocity(){
}

然而我希望能够在空中冻结角色,这样它们就不会掉落但是如果我将maxFallSpeed设置为0,那么我就无法让角色再次降落。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

试试这个,使用默认的“w,s,a,d”移动,使用“f,g”进行上下移动。

#pragma strict

var cMotor: CharacterMotor; // reference to the CharacterMotor script

function Start(){ // get the CharacterMotor script at Start:
   cMotor = GetComponent(CharacterMotor);
}

function Update(){ // move player upwards while F is pressed

if (Input.GetKey("f")){
    cMotor.SetVelocity(Vector3.up*6);
}else if(Input.GetKeyUp("f")){
    cMotor.SetVelocity(Vector3.up*0);
}


 if(Input.GetKey("g")){
     cMotor.SetVelocity(Vector3.down*6);
}else if(Input.GetKeyUp("g")) {
    cMotor.SetVelocity(Vector3.down*0);
}
}

// This do-nothing function is included just to avoid error messages
// because SetVelocity tries to call it

function OnExternalVelocity(){
}

答案 1 :(得分:0)

我不熟悉CharacterMotor,因为它不是默认的unity3d脚本,而是你自己编写或从教程中学到的东西。 但考虑到这一点,我可以建议您在没有按下按键时禁用刚体。

function Update() {
    if(!Input.anyKey) {
       Rigidbody.mass = 0.0f;
       Rigidbody.drag = 50;
    } else {
       Rigidbody.mass = 1.0f; // or the stored variabled of the initial mass
       Rigidbody.drag = 0.7f; 
    }
}

它是一种粗糙的方法,但它会起作用。问题是你的角色电机是否正在使用任何charactercontroller.move或simplemove,它也会拖动角色。

如果是这样,在你的角色电机上添加一个布尔值,并且在进行重力检查时,只需用Input.anyKey禁用bool,如果按任何键,则返回true;如果没有按键,则返回false。

编辑:代码更改,如下面的评论中所定义。

相关问题