- Home /
Character to fly in axis X
Hello to all.
In my character I have the First Person Controller. I want the character to get up in the air going up on the Y axis Now everything works ok thanks alo script Aldonaletto below. The problem lies in the fact that the character salt, but can not get it right and to the left of it seems to be locked. I tried to change all the values but nothing. I ask for a help from someone more experienced maybe Aldonaletto by itself. How could I go up and then move to the right and left forever in flight? Thank you.
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*1.5);
}
}
// This do-nothing function is included just to avoid error messages
// because SetVelocity tries to call it
function OnExternalVelocity(){
}
Answer by NoseKills · May 04, 2014 at 10:03 AM
We can't see how SetVelocity works, but if it works as supposed you should define the velocity and modify it based on key presses and finally pass it to the SetVelocity(). Something like this:
function Start(){ // get the CharacterMotor script at Start:
cMotor = GetComponent(CharacterMotor);
}
function Update(){ // move player upwards while F is pressed
var velo:Vector3 = Vector3.zero;
//vertical velocity
if (Input.GetKey("f")){
velo = Vector3.up;
}
else if (Input.GetKey("v"))
{
velo = Vector3.down;
}
//horizontal velocity
if (Input.GetKey("z")){
velo += Vector3.left;
}
else if (Input.GetKey("x"))
{
velo += Vector3.right;
}
// normalize velo so it's length is 1. Otherwise we will move faster diagonally
cMotor.SetVelocity(velo.normalized * 1.5);
}
Really good. Thanks to work. Now I have to fix the controller because when I press w walk slow. Thank you.
Sorry again Nose$$anonymous$$ills.
When you are at the top and then release the keys, the character falls slowly, slowly. How can I disable the Down to lower the character normally? Thank you.
I guess the character has a rigidbody that makes the character fall due to gravity ? This Update function sets the velocity as Vector3.zero when no keys are pressed so there has to be something else making the character fall.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make ball flip around cube... 0 Answers
RPG scripts 2 Answers
Horizontal axis seperate inputs 0 Answers
Mouse look and character model? 1 Answer
Touch and move in any axis 0 Answers