- Home /
I can't write a realistic jumping script,help!
So i am trying to do a portal-like game but for which i need realistic jumping,and obviously i cant do it...I am using a rigidbody and a capsulecollider not a charactercontroller , i'd be really greatful if you'd help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator anim;
[SerializeField] float moveSpeed = 4f;
[SerializeField] float rotationSpeed = 4f;
Vector3 forward, right;
Rigidbody rb;
void Start()
{
anim = GetComponent<Animator>();
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
void Update()
{
if (Input.anyKey)
Move();
if (Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d"))
{
anim.SetBool("iswalking", true);
}
else {
anim.SetBool("iswalking", false);
}
}
public void Move()
{
Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * direction.x;
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * direction.z;
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.forward = Vector3.Lerp(transform.forward, heading, Time.deltaTime * rotationSpeed);
transform.position += rightMovement;
transform.position += upMovement;
}
}
Answer by Bogdan003 · Oct 27, 2019 at 10:47 AM
GUYS FGET BOUT IT ! IT WAS THE Y AXIS DIRECTION THAT WAS WRONG
NYWAY THANK U ALL ##
Answer by Xrsftw · Oct 26, 2019 at 08:49 PM
I think you shouldn’t use transform.position += ... And instead use rb.AddForce or rb.MovePosition to move your player. Then the physic engine of unity will have a better understanding of what are you trying to do. Also remember to transfer your code in FixedUpade instead of Update if you want a physics based movement.
Ok so i tried making the gravitational force stronger and i got some good results when falling but when i try to jump by using if (Input.Get$$anonymous$$eyDown("c")) { rb.AddForce(Vector3.up *jumpforce); }
it rotates the character in every posible direction , i tried locking the rotation but this way he doesnt even move...
Answer by TheShadyColombian · Oct 26, 2019 at 08:30 PM
Heya so this question is kinda vague but I'll still try to give some advice:
First of all, what do you mean by "realistic"? Assuming your physics are working fine, your player should already have the most realistic movement it'll ever have. You apply upwards force, the physics engine will take care of reducing that velocity, colliding on the ground, etc.
I have a feeling you mean the jumping doesn't feel good, which would make more sense, and is a whole other can of worms. If that is the case, that 's something that can be solved by improving your game feel. Camera sway, shaking, subtle little things like that can give you that extra feeling of "weight". Giving your camera some movement based on the player velocity is typically one way in which a player's jump is made to feel better.
But all of this is, in my opinion, pretty irrelevant for you right now. If you're planning on developing this game completely, these improvements to the game feel should be left until the end. It's all just polish. Your main priority should be in completing a full prototype, then you can consider moving onto a vertical slice of your game if you're happy with the way the game plays.
Lastly, and I'm sure most people will tell you this, I'm worried you might have too broad a scope for a one person project. If this is a small project for you to just mess around and gain experience, then that's good, but I'm afraid I don't see this project being something that one person can finish on their own.
Best of luck on your project though! If you need some clarification on anything I mentioned feel free to leave a comment, or send me a DM (not sure if Unity Answers has those though :\ )
Answer by Reedex · Oct 26, 2019 at 09:54 PM
This ytb link has interesting observations along with simple code. : - ) although it's presented in 2d. https://www.youtube.com/watch?v=7KiK0Aqtmzc
Your answer
Follow this Question
Related Questions
jumping from ledge problem 1 Answer
2D "Platformer" Movement Problems 1 Answer
Is this sprint/stamina script optimal or wasting performace? 0 Answers
Character not jumping when moving 0 Answers