- Home /
Constant Force applied to player without rigidbody on button press
Okay, I want to make myself very clear. My goal is to have a player be able to press and hold jump after holding rightclick to boost in the opposite direction of gravity. Im talking about a constant force, not just a jump. If the player releases jump the force stops. If while in mid air the player holds jump down again, the boost is re-activated. After reading about jetpacks and whatnot around Unity, I found that a lot of people use rigidbodies. I gave that a go, but my player just randomly rotates (including upside down). So I made the rigid body Kinematic. That fixed the rotation problem, but then every example of code I tried for the boost said: Actor::setLinearVelocity: Actor must be (non-kinematic) dynamic! UnityEngine.Rigidbody:set_velocity(Vector3)
So here is what I am using now. The code I started out with is from here: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Here is my edited code.
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; var boost : float = 30;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
if (Input.GetButton ("Fire2")) {
//Add friction mod here
//This is the upward force
if (Input.GetButton ("Jump")) {
//I have tried many things here to no avail
}
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
I plan on making it to where I can rightclick and have zero friction, but that will come later as well as a fuel supply.
It would be better to use rigidbody ins$$anonymous$$d of characterController but you can justset new move direction with same X and Z coordinates and just pass the boost value into the Y axis.
I guess the main problem is checking the 'both buttons pressed' condition. As soon as you press Jump, you are no longer grounded and the check for the right mouse button will never execute.
What do you mean upwards boost similar to a jetpack? Do you want the player to move upwards at boost speed until they release the right button? Can the player re-activate the boost when falling?
DajBuzi, I will try both of those.
Leosori, the rightclick happens before the jump. Does that make a difference?
AdamScura, Yes I want the player to move upwards at a boost speed until they release the jump button. The player can re-activate the boost when falling.
Answer by Leosori · Feb 11, 2014 at 09:08 PM
This should work kinda the way you wanted it. I know you don't want to use a rigidbody, but if you want more realistic effects it would be a lot easier to use the Unity physics engine instead of trying to build your own little physics engine :)
As you can see below, you have to handle the situation when you are not grounded. The original example code just continues to move in the same direction during the jump - while applying gravity to fall down - until the player hits the ground again.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var boostSpeed : float = 6.0;
var gravity : float = 20.0;
private var boostAllowed : boolean = false;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
if (Input.GetMouseButton(1)) {
// use the jetpack without jumping
boostAllowed = true;
moveDirection.y = boostSpeed;
} else {
// just a normal jump
boostAllowed = false;
moveDirection.y = jumpSpeed;
}
}
} else {
// we are not grounded
// use boost if allowed
if (boostAllowed && Input.GetButton("Jump")) {
// If you want to change direction while boosting
// uncomment the following block
/*
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
*/
moveDirection.y = boostSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Yes that is pretty much exactly what I wanted. I did notice that after having used the boost, I can just jump in midair though. How do I make it to where mid air boost is only available when rightclick is held down?
I do not want to create my own physics engine, its just that I have not had any luck with rigid bodies as players. Could you point me in a direction to learn about that?
Thank you so much for your help.
If the user should press both buttons again in midair, you can simply add that condition in line 31.
$$anonymous$$aybe these tutorials from Unity can help you to better understand rigidbodies and how to apply some force to them
http://unity3d.com/learn/tutorials/modules/beginner/physics
If you are having trouble with the vector math
http://docs.unity3d.com/Documentation/$$anonymous$$anual/VectorCookbook.html
And finally, I can see that you are pretty new to coding scripts. These tutorials are not the best way to get started with coding but they are focusing on Unity. I guess you can find better ones with some googling.
http://unity3d.com/learn/tutorials/modules/beginner/scripting
Thank you Leosori. I will be switching over to rigidbodies. Yes I am new to coding XD Thanks for the links, I will read them before posting anymore questions. Cheers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133148im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jet-Pack Script 1 Answer
[Character controller] run faster on slopes and jetpack 1 Answer
Am I headed in the right direction? 0 Answers
Character Controller Fly 1 Answer
How to make a jetpack with fuel? 2 Answers