- Home /
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!