Unity3D & C# - Can't Jump Properly in my Game - c#

I dont know what to do, when I click play, I click Space for Jump and sometime it jump but sometime it not. What do I do wrong?
i got it from https://www.youtube.com/watch?v=I_mjYhwSsS8
this is my PlayerController Script.
// Start is called before the first frame update
void Start()
{
this.rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
private void FixedUpdate()
{
Grounded();
Jump();
Move();
}
private void Jump()
{
if(Input.GetKeyDown(KeyCode.Space) && this.grounded)
{
this.rb.AddForce(Vector3.up * 4, ForceMode.Impulse);
}
}
private void Grounded()
{
if(Physics.CheckSphere(this.transform.position + Vector3.down, 0.2f, layerMask))
{
this.grounded = true;
}
else
{
this.grounded = false;
}
this.anim.SetBool("jump", !this.grounded);
}
private void Move()
{
float verticalAxis = Input.GetAxis("Vertical");
float horizontalAxis = Input.GetAxis("Horizontal");
Vector3 movement = this.transform.forward * verticalAxis + this.transform.right * horizontalAxis;
movement.Normalize();
this.transform.position += movement * 0.04f;
this.anim.SetFloat("vertical", verticalAxis);
this.anim.SetFloat("horizontal", horizontalAxis);
}

In general you want to get single time event user input within Update!
FixedUpdate is called in fixed time intervals, not every frame which might cause that if you press a button down exactly in a frame where FixedUpdate is not called you miss that event.
Secondly as soon as there is a rigidbody involved you should not use Transform.position to move it but rather go through the Rigidbody component either setting velocity or using MovePosition
private bool isJumpPressed;
private void Start()
{
if(!rb) rb = GetComponent<Rigidbody>();
}
private void Udpate()
{
GetJumpInput();
}
private void GetJumpInput()
{
// it is slightly more efficient to first check the simple bool flag
if(grounded && (Input.GetKeyDown(KeyCode.Space))
{
// set the jump flag that is later handled in FixedUpdate
isJumpPressed = true;
}
}
private void FixedUpdate()
{
Grounded();
HandleJump();
Move();
}
private void HandleJump()
{
// since the grounded might have changed in the meantime check it again
// then check and handle the jump flag
if(grounded && isJumpPressed)
{
rb.AddForce(Vector3.up * 4, ForceMode.Impulse);
}
// after having handled the input always reset it
isJumpPressed = false;
}
private void Grounded()
{
grounded = Physics.CheckSphere(this.transform.position + Vector3.down, 0.2f, layerMask);
anim.SetBool("jump", !this.grounded);
}
private void Move()
{
// you might want to consider to only allow a change of this while on the ground
// so midair you can't change the direction
//if(!grounded) return;
var verticalAxis = Input.GetAxis("Vertical");
var horizontalAxis = Input.GetAxis("Horizontal");
// store the current velocity
var velocity = rb.velocity;
// most importantly store the Y velocity in order to maintain that one
var y = velocity.y;
// instead of normalize you rather want to Clamp the magnitude
// otherwise you also get magnitude 1 even though the input is actually smaller
velocity = Vector3.ClampMagnitude(rb.rotation * Vector3.forward * verticalAxis + rb.rotation * Vector3.right * horizontalAxis, 1) * 0.04f;
// finally maintain the Y velocity so you can't fly ;)
velocity.y = y;
// and assign the velocity back to the rigidbody
rb.velocity = velocity;
this.anim.SetFloat("vertical", verticalAxis);
this.anim.SetFloat("horizontal", horizontalAxis);
}

Related

How to prevent speeding of rigidbodies used to control characters

I have a player in unity with the movement controlled by a rigidbody. The movement on the Z axis is kept contstant by the game to keep the player moving forward. However, this means that the rigidbody keeps speeding up so its speed at the start of the game is much slower than the speed at the end of the game. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour
{
[Tooltip("Rigidbody component attached to the player")]
public Rigidbody rb;
public float forwardMax;
public float slowBy;
private float movementX;
private float movementY;
private float gravity = -9.81f;
private bool isJumping = false;
private bool isSlowing = false;
private bool isSpeeding = false;
private float speedX = 100;
private float speedY = 150000;
private float speedZ = 60;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// if(!controller.isGrounded)
// {
// Vector3 gravityVector = new Vector3(0.0f, gravity, 0.0f);
// rb.AddForce(gravityVector * Time.deltaTime);
// }
}
void OnCollisionEnter(Collision collision)
{
// SceneManager.LoadScene(1);
}
// Update is called once per frame
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void OnJump()
{
isJumping = true;
}
void CalculateMovement()
{
if(rb.velocity.z > 20)
{
rb.drag = 20;
}
else
{
rb.drag = 0;
}
Vector3 movement = new Vector3(movementX * speedX, 0.0f, speedZ);
if(isJumping)
{
movement.y += Mathf.Sqrt(speedY * -3.0f * gravity);
isJumping = false;
}
rb.AddForce(movement);
Debug.Log("Speed is " + rb.velocity.z);
}
void FixedUpdate()
{
CalculateMovement();
}
}
Is there a way to keep the forward velocity constant? The problem is worse when the player jumps.
First I tried clamping the forward (z-axis) vector but that had no effect. Then, I tried adding a backward vector onto the total when the forward velocity was above a certain number but this led to it speeding up and slowing down all the time. Then I tried the same thing with the drag on the rigidbody but that had the same effect.
You can use rigidbody.velocity and set it to constant value or whatever you want instead of adding force. By adding force, your character's speed increases.
Also you can use AddForce but you have to tune the force value dynamically according to the current velocity.
rigidbody velocity
How about directly setting the z value you want at the end of FixedUpdate() ?
......
void FixedUpdate()
{
CalculateMovementWithoutZMovement();
rb.velocity = new Vector3 (rb.velocity.x, rb.velocity.y, ConstantZValue);
}

Unity: raycast groundcheck 2d doesn't work

So I want to check if there is ground under my character
here is the code
public class move2d : MonoBehaviour
{
public float moveSpeed = 7f;
public float distanceGround;
public bool isGrounded = false;
// Start is called before the first frame update
void Start()
{
distanceGround = GetComponent<Collider2D>().bounds.extents.y;
}
// Update is called once per frame
void Update()
{
if (Physics2D.Raycast(transform.position, -Vector2.up, distanceGround + 0.1f))
{
}
else
{
isGrounded = true;
Jump();
}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump") )
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 8f), ForceMode2D.Impulse);
}
}
}
but it doesn't work and I don't understand why it never enter else statement even there my character is on ground.
When dealing with rigidbody you shouldn't use transform.position not for getting and not for setting values. Rather use Rigidbody2D.position for getting and Rigidbody2D.MovePosition in FixedUpdate for setting.
Also shouldn't it be the other way round? You most probably are grounded if the raycast hits something .. not if it doesn't?
// Already reference these via the Inspector
[SerializeField] private RigidBody2D rigidBody2D;
[SerializeField] private Collider2D _collider2D;
public float moveSpeed = 7f;
public float distanceGround;
private Vector2 movement;
private bool jumped;
private void Awake ()
{
// Alternately get it once on runtime
if(! rigidBody2D) rigidBody2D = GetComponent<RigidBody2D>();
if(!_collider2D) _collider2D = GetComponent<Collider2D>();
}
private void Start()
{
// Micro performance improvement by calculating this only once ;)
distanceGround = _collider2D.bounds.extents.y + 0.1f;
}
// Get user Input every frame
private void Update()
{
// Directly check for the button
if (Input.GetButtonDown("Jump"))
{
// if button presed set the jump flag
jumped = true;
}
// store user input
movement = Vector3.right * Input.GetAxis("Horizontal");
}
// Apply physics in the physics update
private void FixedUpdate()
{
// If jump flag is set
if(jumped)
{
// You are grounded when you hit something, not the other way round
if (Physics2D.Raycast(rigidBody2D.position, Vector2.down, distanceGround))
{
rigidbody2D.AddForce(Vector2.up * 8f, ForceMode2D.Impulse);
}
// reset the flag
jumped = false;
}
// apply the normal movement without breaking the physics
rigidBody2D.MovePosition(rigidBody2D.position + movement * Time.deltaTime * moveSpeed);
}

transform.position teleporting. Not smoothly moving

Simple question but hard time finding an answer.
I have a hook mechanic where on the press of key.B, a hook will fire for 5 seconds then should come back.
This code is working fine, just that when the code allocated to recall the object, it does not comeback smoothly, it teleports.
Here is the code, the problem-specific line in BOLD
public class Hook : MonoBehaviour
{ //Remember Couroutine is pretty much update()
public Transform Target;
private float Thrust; // Int for motion
public Rigidbody rb;
public float HookTravelTime; //Define float for seconds
bool isHookActive = false; //Are we currently moving?
public float timeHookTraveling = 0f;
// Use this for initialization
void Start()
{
Thrust = 75f;
rb = GetComponent<Rigidbody>();
float walkspeed = Thrust * Time.deltaTime;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "mob")
{
Destroy(col.gameObject);
print("Other code negated");
rb.velocity = Vector3.zero;
transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
isHookActive = false;
timeHookTraveling = 0f;
}
}
void ThrowHook()
{
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
{
print("hehemeth");
rb.velocity = Vector3.zero; //negate addforce from before
**HERE** transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
timeHookTraveling = 0f;//reset the travel time for your next hook activation
}
else//if you havent hit 5 keep increasing
{
timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
}
}
}
// Update is called once per frame
void Update()
{
ThrowHook();
}
}
What am I doing wrong? It should work as intended, right?
Vector3.MoveTowards is needed to be run everyframe, that's why I mentioned * Time.deltaTime in comment.
At 5s, rb's velocity becomes zero and isHookActive becomes false, and thus Vector3.MoveTowards is not called everyframe.
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
{
print("hehemeth");
rb.velocity = Vector3.zero; //negate addforce from before
isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
timeHookTraveling = 0f;//reset the travel time for your next hook activation
}
else//if you havent hit 5 keep increasing
{
timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
}
}
else if (!isHookActive && transform.position != Target.position)
{
transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust * Time.deltaTime);
}
And a even better way is putting
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
into FixedUpdate() but not Update() while
if (isHookActive )
{... }
remains in Update().

How to addforce in a random direction to a rigidbody2d game object

How can I add a force to a rigidbody2D game object and keep it moving at a fixed velocity? The game object also has a bounce material attached.
private Rigidbody2D rb2D;
private float thrust = 10.0f;
void Start() {
}
void FixedUpdate() {
rb2D.AddForce(new Vector2(0, 1) * thrust);
}
This is what i got off of the Unity documentations website but this doesn't seem to do anything.
Here is the code I ended up going with and it appears to be functioning properly. The Vector2 direction and speeds can be adjusted depending on mass/ gravity.
float topSpeed = 15;
private Rigidbody2D rb2D;
private float thrust = 0.1f;
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
rb2D.AddForce(new Vector2(0, 1) * thrust);
}
void Update()
{
if (rb2D.velocity.magnitude > topSpeed || rb2D.velocity.magnitude < topSpeed)
rb2D.velocity = rb2D.velocity.normalized * topSpeed;
}
Your code as written, once it works will accelerate the rigidbody infinitely. You'll want to cap the velocity at its maximum speed: http://answers.unity.com/answers/330805/view.html
rigidbody.AddForce(new Vector2(0, 1) * thrust * Time.deltaTime);
if (rigidbody.velocity.magnitude > topSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * topSpeed;
If you want it to immediately set the velocity to the fixed value, then you can just set the velocity on every frame:
https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
void FixedUpdate()
{
if (Input.GetButtonDown("Jump"))
{
// the cube is going to move upwards in 10 units per second
rb2D.velocity = new Vector3(0, 10, 0);
moving = true;
Debug.Log("jump");
}
if (moving)
{
// when the cube has moved over 1 second report it's position
t = t + Time.deltaTime;
if (t > 1.0f)
{
Debug.Log(gameObject.transform.position.y + " : " + t);
t = 0.0f;
}
}
}
Your code doesn't show it so in case you aren't doing it yet, you'll want to make sure that rb2D is actually set to the Rigidbody2d on the object you want to manipulate. E.g. by doing in the start method:
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
}

Unity jumping fails while going against a wall

Okay im trying to make my object (player) to jump everything is okay until i go against a wall and keep going against (still W is down) i cant jump wen im hitting a wall if i stop walking he will be enable to jump i tried making the walls on touch to make the player to have velocity = zero but it does not work,
i tried to add rigid body to the walls and freezing them in place, trying to make them kinematic does not work too .
I wish wen i go against walls and keep walking against them to be enable to jump.
If you know how i can do that please share thanks .
Here is the move script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour {
private float speed;
private float jumpHight;
private float straffeSpeed;
private float fallMultiplier;
private Rigidbody rig;
private Collider coll;
// Use this for initialization
private void Awake()
{
rig = GetComponent<Rigidbody>();
coll = GetComponent<Collider>();
straffeSpeed = 1.5f;
fallMultiplier = 2.5f;
speed = 10f;
jumpHight = 4f;
}
void Start () {
GroundCheck();
}
// Update is called once per frame
void Update () {
Move();
GroundCheck();
BetterFall();
}
private void Move()
{
float hAxis = Input.GetAxis("Horizontal") * straffeSpeed;
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
rig.MovePosition(transform.position + movement);
if (Input.GetKey(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
}
private bool GroundCheck()
{
return Physics.Raycast(transform.position, -Vector3.up, coll.bounds.extents.y + 0.2f);
}
private void BetterFall()
{
if(rig.velocity.y < 0)
{
rig.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
}
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
I don't think you are doing this quite right. Try this:
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
}
:-)

Categories