Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hueman · Feb 11, 2014 at 04:38 AM · charactercontrollerflyconstantforcejetpack

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DajBuzi · Feb 11, 2014 at 09:31 AM 0
Share

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.

avatar image Leosori · Feb 11, 2014 at 10:21 AM 0
Share

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.

avatar image AdamScura · Feb 11, 2014 at 10:40 AM 0
Share

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?

avatar image Hueman · Feb 11, 2014 at 04:33 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hueman · Feb 12, 2014 at 01:06 AM 0
Share

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.

avatar image Leosori · Feb 12, 2014 at 08:36 AM 0
Share

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

avatar image Hueman · Feb 12, 2014 at 09:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges