Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yih058652 · Jan 29, 2020 at 11:52 AM · physicsrigidbodyjumprpginertia

control the inertia of a character in rpg when it jumps

Hi everyone: I am a noob :-p.Here is what I wanted to do but still not find a way to realize it:

Basically I want to build a character in a 3D rpg(a capsule with it's rigidbody and collider) like what discribed below: 1.when pressing "up","down","left" or "right",it walks normally with no inertia(which means it stop immediately as soon as the up/down/left/right key is unpressed).

2.if it jumps when it is walking, there should exist inertia(for example,when it walk towards left,and I hit the jump button after I unpress the "left" key like 0.1 sec,then it will jump towards left).

But I then met a bunch of problems,the problem descirped below is a mess..so it can be skipped.my code is after that It is a normal thing in many rpg or other 3d game,so if any of you have experience please give me some advice.

I have tried it in many way but it didn't worked out: First,I have to use "rigidbody" to realize the gravity. Then,in this way,"transform.translate" is not recommended to use as said in the Unity manual.Besides ,in this way,the character jumps straight up after it walks.(Besides,I can not "get" the "rigidybody.velocity" so I can not add together the “rigidybody.velocity” and “Velocity3d.up” manually to the “rigidbody.AddForce()”.) After that,when the character is walking,there exists "inertia"(which means it does not stop immediately and slip after I unpress the walking key) To erase the inertiaa little,I try to increase the air force resistence by increasing rigidbody.drag,but then it float down slowly in the air after it jumped like in low gravity. I don't want to change the gravity value(9.81) because in the game there are many other things which should follow the physical rule in real world.Besides, I don't think this would work out.

then,I want to do it manually by getting the velocity before it jumps.I write "rigidbody.movePosition" in "FixedUpdate()",But the "rigidbody.velocity" still reads zero.

and my code is below:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class man : MonoBehaviour { public string TerrainName; public float jumpCoefficient; public float velocityCoefficient; public float rotateCoefficient; public KeyCode moveForward; public KeyCode moveBackward; public KeyCode moveLeft; public KeyCode moveRight; public KeyCode jump;

 private float rotationX;
 private float rotationY;
 private Rigidbody rigidbody_man;
 private bool isOnTheGround=false;
 // Start is called before the first frame update
 void Start()
 {

     rigidbody_man = this.GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     Debug.Log(this.GetComponent<Rigidbody>().velocity);


     if (Input.GetKey(jump))
     {
         RegularJump();
     }
     if (Input.GetKey(moveForward))
     {
         RegularMove(Vector3.forward);
     }
     else if (Input.GetKey(moveBackward))
     {
         RegularMove(Vector3.back);
     }
     else if (Input.GetKey(moveRight))
     {
         RegularMove(Vector3.right);
     }
     else if (Input.GetKey(moveLeft))
     {
         RegularMove(Vector3.left );
     }
 
        rotationX = -Input.GetAxis("Mouse X") * rotateCoefficient;
        // rotationY = Input.GetAxis("Mouse Y") * rotateCoefficient;
         transform.Rotate(rotationY,rotationX, 0f);
     
 
 }
 void RegularMove(Vector3 direction)
 {
     if (isOnTheGround)
     { 
     rigidbody_man.MovePosition(transform.position + transform.right);
     //rigidbody_man.velocity=direction*velocityCoefficient;
 //    rigidbody_man.AddForce(direction*velocityCoefficient);
 //  transform.Translate(direction * velocityCoefficient);
     Debug.Log(this.GetComponent<Rigidbody>().velocity);



     }
  
 }
 void RegularJump()
 {
     if (isOnTheGround)
     { 
     rigidbody_man.AddForce(Vector3.up * jumpCoefficient);
     isOnTheGround = false;
     }
 }
 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.name == TerrainName)
     {
         isOnTheGround = true;   
     }
 }

}

The problems are written above(which can be skipped).Thanks for your help!

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

0 Replies

· Add your reply
  • Sort: 

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

227 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 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 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 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 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 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 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 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 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 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 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

Wierd Internal Physics problem 1 Answer

How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers

Interpolate problem? Jump isn't smooth... 1 Answer

How can I create a "round arc" physics based jump? 2 Answers

A FLAWLESS way to check if character is grounded? 1 Answer


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