- Home /
How do you use Add.Force() to fly in the mouse-inputted direction?
Hello!
I have been having trouble getting my plane to fly straight with the add.force command in javascript. My plane simply flies up really fast or straight down very fast and I cannot get it to go straight(or any horizontal direction). I have tried all of the solutions similar to rigidbody.AddForce(transform.forward * speed) and now I have resorted to thinking that I am missing something small yet detrimental. Please help!
var thrust: float; var rb: Rigidbody; var rotateSpeed = 30.0;
function Start() { if(GetComponent.().IsMine)//CAMERA { rb = GetComponent.(); Camera.main.GetComponent(Smoothfollow).target = transform; } }
function Update() {
if (GetComponent.().isMine) { var transAmount = thrust Time.deltaTime; var rotateAmount = rotateSpeed Time.deltaTime; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false;
var h = -(Input.GetAxis("Mouse X"));
var v = -(Input.GetAxis("Mouse Y"));
transform.Rotate(v, 0, h);
if (Input.GetKey ("w"))
{
rb.AddForce(transform.forward * transAmount);
}
}
Please format your code more properly, that's unreadable :/
$$anonymous$$ay your transform.forward points up. Could you debug the result of transform.forward and post it here?
Your answer
![](https://koobas.hobune.stream/wayback/20220611231630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
With enough force, box collider goes through planes. 5 Answers
Nose Diving Plane 1 Answer
Continious force 1 Answer
How can I determine force and direction by holding mouse? 0 Answers
Limit speed on x axis when applying force with gravity 0 Answers