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 garanon · Mar 02, 2012 at 02:44 AM · forceconstantconstant forceglide

Adding an upward force

I'm trying to add an upward force on my character to give a gliding feel when he is in midair, but it doesn't seem to work.

Here is the related code:

 if (Input.GetButton ("Fire1")) {   
                     Debug.Log("Glide");
                     rigidbody.AddForce (0,10,0);

There is a rigidbody attached to the object. The scene is just a simple cube as character and plane as floor so nothing else should be interfering with it.

I also tried adding a constant force component which didn't work either

If anyone could help would be greatly appreciated

Comment
Add comment
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

3 Replies

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

Answer by Kleptomaniac · Mar 03, 2012 at 12:48 AM

Hey there, moving comment to answer so people can see what was wrong:

The problem was that while Fire1 was pressed, moveDirection.y is still being minuses. Therefore the glide and vertical position modifiers were cancelling each other out. Try this:

 if (Input.GetButton ("Fire1")) { 
     Debug.Log("Glide");
     rigidbody.AddForce (0,100,0);
 } else {
     moveDirection.y -= gravity * Time.deltaTime;
 }

Thanks also to @Berenger for help!

Comment
Add comment · 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
2

Answer by Berenger · Mar 02, 2012 at 05:00 AM

If pushing doesn't work, push harder. That's my philosophy.

However, you could disable it's gravity as well.

Comment
Add comment · Show 1 · 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 garanon · Mar 02, 2012 at 07:00 AM 0
Share

i tried 10, 100 and 10,000 all with no result Also I don't want to disable or turn down gravity as it will have a strange effect on an upward jump

avatar image
0

Answer by Berenger · Mar 02, 2012 at 07:37 AM

Are you manually modifying the velocity ? That would cancel the AddForce. Also, the debug line gets printed right ?

Comment
Add comment · Show 7 · 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 garanon · Mar 02, 2012 at 09:04 AM 0
Share

Yes the debug line gets printed. I am modifying the velocity im not sure how else to make a jump :P

avatar image garanon · Mar 02, 2012 at 10:10 AM 0
Share

my entire code is as follows, if you could point out my issue that would be great

var walkSpeed : float = 20.0; var gravity = 10.0; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; var jumpSpeed = 3; var player : GameObject;

function Start() { charController = GetComponent(CharacterController); animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop; }

function Update () { if(charController.isGrounded == true) {

     if(Input.GetAxis("Vertical") > .1) {
     }
     else if(Input.GetAxis("Vertical") < -.1)
     {
        animation["walk"].speed = -1;
        animation.CrossFade("walk");
        walkSpeed = 20;
     }
     
     else {
         animation.CrossFade("stop");
     }

     if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))  // Create an animation cycle for when the character is turning on the spot
     {
         animation.CrossFade("walk");
     }


     transform.eulerAngles.y += Input.GetAxis("Horizontal");

     
     moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
     moveDirection = transform.TransformDirection(moveDirection);

     if (Input.GetButton ("Jump")) {
     moveDirection.y = jumpSpeed;                        
     }

 }

 moveDirection.y -= gravity * Time.deltaTime;
 charController.$$anonymous$$ove(moveDirection * (Time.deltaTime * walkSpeed));

                     

      if (Input.GetButton ("Fire1")) {   //GLIDE FUNCTION HERE
                 Debug.Log("Glide");
                 rigidbody.AddForce (0,100,0);

 }


}

avatar image garanon · Mar 02, 2012 at 10:12 AM 0
Share

you can create a new scene with a simple cube character and comment out the animate commands to test it

avatar image Kleptomaniac · Mar 02, 2012 at 11:56 AM 1
Share

I believe your problem is that while Fire1 is pressed, moveDirection.y is still being $$anonymous$$used! Therefore your glide and vertical position modifier are cancelling each other out. Possibly try this:

 if (Input.GetButton ("Fire1")) {   //GLIDE FUNCTION HERE
                 Debug.Log("Glide");
                 rigidbody.AddForce (0,100,0);
 
 } else {
 moveDirection.y -= gravity * Time.deltaTime;
 }

I think that should work ...

avatar image Berenger · Mar 02, 2012 at 04:58 PM 0
Share

Isn't character controller supposed to apply gravity on it's own ?

Show more comments

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

7 People are following this question.

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

Related Questions

Applying an Initial force on button press, and a constant force while its held down that gets weaker over time? 0 Answers

How can I set a ConstantForce's force to move the object towards another object? 1 Answer

[Solved]Moving object at constant speed. 1 Answer

Move 3D camera on 2D plane 0 Answers

Player not able to climb an angle 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