Can't Jump while moving left and right in 2D - c#

A little bit of context, this is a 2D game I got it so I can move left right and jump, I can't jump while moving left and right and if I move while in midair then the player's decent dramatically slows down
Here's the code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float horSpeed;
[SerializeField] private float vertSpeed;
private Rigidbody2D rb;
private bool isJumping;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKey(KeyCode.Space) && !isJumping)
rb.velocity = new Vector2(0, horSpeed);
isJumping = true;
if (Input.GetKey(KeyCode.A))
rb.velocity = new Vector2(-vertSpeed, 0);
if(Input.GetKey(KeyCode.D))
rb.velocity = new Vector2(vertSpeed, 0);
if (rb.velocity.y==0f)
{
isJumping = false;
}
}
}
I don't see what I've done wrong here, it might be that I'm registering the inputs on different lines of code but I'm not sure how to fix it if that is the problem

The code presented is dealing with the velocity of an object. As such, you want to assign the calculated velocity as one value once all factors have been taken into account. I’ve corrected a few points and come up with this:
private void Update()
{
// we store the initial velocity, which is a struct.
var v = rb.velocity;
if (v.y==0f)
isJumping = false;
if (Input.GetKey(KeyCode.Space) && !isJumping) {
v.y = vertSpeed;
isJumping = true;
}
if (Input.GetKey(KeyCode.A))
v.x = -horSpeed;
if(Input.GetKey(KeyCode.D))
v.x = horSpeed;
rb.velocity = v;
}
The reason the code stores the velocity first, is because any changes you make to the temporary velocity struct needs to be applied back as a single value assignment to the rigid body’s velocity field (this is due to the difference between value and reference type objects). Here’s the Microsoft discussion about the difference.

The reason that you can't jump while moving left or right is because you overwrite your "horizontal" speed when you move. Look at your code for moving left or right. Notice how you set your y velocity to 0 when a or d is pressed.
Try this:
private void Update() {
if (Input.GetKey(KeyCode.Space) && !isJumping) {
rb.velocity.y = horSpeed;
isJumping = true;
}
if (Input.GetKey(KeyCode.A))
rb.velocity.x = -vertSpeed;
if(Input.GetKey(KeyCode.D))
rb.velocity.x = vertSpeed;
if (rb.velocity.y==0f)
{
isJumping = false;
}
}
I think this should work but I don't have access to Unity right now so I'll have to wait til I get home to check it. Also, I didnt change it in this code but it seems like you have horSpeed and vertSpeed flipped. Up and down movement (a.k.a. jumping) should be vertSpeed.

Related

Unity3D: How to move an object in relation to another object?

I have a physics enabled sphere in my scene and a character that i can move around using WASD. now what i want is that as soon as player hits the ball it should not be physics enabled anymore and move along with player as like the player is holding it in the hands.
What i have tried so far:
i was able to do it but not at all perfect how i want it to be. i tried OnCollisionEnter to detect the collision, i made the sphere a child of the player, i used isKinematic = true as soon as collision is detected and i used Vector3.MoveTowards for the object to follow the position as the player.
I have attached two reference videos below
Reference Clip(The style i am aiming for):https://www.youtube.com/watch?v=fi-GB0RwLr0
MyVersion(This is what i am able to do):https://www.youtube.com/watch?v=JtV11KHY4pU
overall, if you watch the clips you can see how my version is very rigid, stucky, buggy, not so light weight feeling at all. Cut me a slack as well if i did anything way wrong, i started unity like just in the previous month.
This is my Player script through which i am controlling it all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float MoveSpeed;
[SerializeField]private Rigidbody playerRig;
[SerializeField] private Rigidbody ballRig;
[SerializeField] private GameObject Ball;
[SerializeField] private GameObject heldObj;
private bool isTouching = false;
private Vector3 offset = new Vector3(0, 1, 3);
private Vector3 force;
public float jump;
private bool isGrounded = true;
private void Start()
{
Ball = GameObject.FindGameObjectWithTag("Ball");
ballRig.GetComponent<Rigidbody>();
playerRig.GetComponent<Rigidbody>();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
playerRig.velocity = new Vector3(x*MoveSpeed, playerRig.velocity.y,z*MoveSpeed);
Vector3 vel = playerRig.velocity;
vel.y = 0;
if (vel.x != 0 || vel.z != 0)
{
transform.forward = vel;
}
if(transform.position.y < -10)
{
GameOver();
}
ReleaseTheBall();
if (isTouching == true)
{
ballRig.transform.position = Vector3.MoveTowards(ballRig.transform.position, playerRig.transform.position, 5f);
}
}
void Jump()
{
force = new Vector3(0, jump, 0);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
isGrounded = false;
playerRig.AddForce(force, ForceMode.Impulse);
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ball" && heldObj == null)
{
ballRig.isKinematic = true;
// ballRig.constraints = RigidbodyConstraints.FreezeRotation;
ballRig.transform.parent = playerRig.transform;
isTouching = true;
Debug.Log(" BALL PICKED UP AND MOVING ");
heldObj = Ball;
}
}
public void ReleaseTheBall()
{
if (Input.GetKeyDown(KeyCode.Space) && heldObj != null)
{
isTouching = false;
heldObj = null;
ballRig.transform.parent = null;
//ballRig.constraints = RigidbodyConstraints.None;
ballRig.isKinematic = false;
//Vector3 forceDirection = new Vector3(playerRig.transform.position.x, 0, playerRig.transform.position.z);
Vector3 rotationDirection = new Vector3(playerRig.transform.rotation.x, playerRig.transform.rotation.y, playerRig.transform.rotation.z);
ballRig.AddForce(new Vector3(transform.position.x,0,transform.position.z) * 5, ForceMode.Impulse);
ballRig.AddTorque(rotationDirection * 1);
Debug.Log("BALL RELEASED");
}
}
public void GameOver()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
i know there could be some very inefficient lines of code here, feel free to call out those lines as well.
So, basically what i want is that i want my player to pick up the ball as soon as it hits it and the ball should move exactly like player as if he is holding the ball in his hands and when i hit the spacebar i want the ball to be released into the direction i am looking(this part is also very tricky for me), what i am trying to do is i want to do exactly like in the refrence clip. Thanks a lot in advance.
one more thing, when i stop pressing any key to move(WASD), my player transform keeps changing values up and down and it slightly starts moving. i mean when i dont press any key, it still kinda moves.
First of all, you have indeed quite a lot of lines that aren't useful in here and that could be optimized.
Anyway, I hope this will do what you want it to do:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Rigidbody))]
public class Player : MonoBehaviour {
[Header("Movement Setup")]
[Tooltip("Translation speed in m/s")]
[SerializeField] float _TranslationSpeed;
[Tooltip("Rotation speed in °/s")]
[SerializeField] float _RotationSpeed;
[Tooltip("Interpolation speed [0;1]")]
[SerializeField] float _InterpolationSpeed;
[Tooltip("Strength for jumps")]
[SerializeField] float _JumpingStrength;
[Tooltip("Strength for shoots")]
[SerializeField] float _ShootingStrength;
[Header("Other Settings")]
[Tooltip("Where the helded item will be placed")]
[SerializeField] Transform _ContactPoint;
[Tooltip("Which layers should be considered as ground")]
[SerializeField] LayerMask _GroundLayer;
// The player's rigid body
Rigidbody _Rigidbody;
// Is the player on the ground
bool _IsGrounded;
// Rigidbody of what's the player is helding. Null if the player isn't holding anything
Rigidbody _HeldedItemRigidbody;
// Getter to _IsGrounded, return true if the player is grounded
public bool IsGrounded { get { return _IsGrounded; } }
private void Start() {
_Rigidbody = GetComponent<Rigidbody>(); // The player's rigidbody could be serialized
}
void FixedUpdate() {
float horizontalInput, verticalInput;
// Getting axis
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
// Moving toward the x
Vector3 moveVect = transform.forward * _TranslationSpeed * Time.fixedDeltaTime * verticalInput;
_Rigidbody.MovePosition(_Rigidbody.position + moveVect);
// Rotating the player based on the horizontal input
float rotAngle = horizontalInput * _RotationSpeed * Time.fixedDeltaTime;
Quaternion qRot = Quaternion.AngleAxis(rotAngle, transform.up);
Quaternion qRotUpRight = Quaternion.FromToRotation(transform.up, Vector3.up);
Quaternion qOrientationUpRightTarget = qRotUpRight * _Rigidbody.rotation;
Quaternion qNewUpRightOrientation = Quaternion.Slerp(_Rigidbody.rotation, qOrientationUpRightTarget, Time.fixedDeltaTime * _InterpolationSpeed); // We're using a slerp for a smooth movement
_Rigidbody.MoveRotation(qRot * qNewUpRightOrientation);
// This prevents the player from falling/keep moving if there is no input
if (_IsGrounded) _Rigidbody.velocity = Vector3.zero;
_Rigidbody.angularVelocity = Vector3.zero;
if (transform.position.y < -10) GameOver();
if (Input.GetKey(KeyCode.Mouse0) && _HeldedItemRigidbody != null) ShootTheBall();
if (!_IsGrounded) return; // What's below this won't be executed while jumping
if (Input.GetKey(KeyCode.Space)) Jump();
}
void Jump() {
_IsGrounded = false;
_Rigidbody.AddForce(_JumpingStrength * transform.up, ForceMode.Impulse);
}
private void OnCollisionEnter(Collision collision) {
// Checking if the player encountered a ground object
if ((_GroundLayer & (1 << collision.gameObject.layer)) > 0) _IsGrounded = true;
if (collision.gameObject.tag == "Ball") { // this can be improved as tags are error prone
// Setting the item position to contact point
collision.gameObject.transform.position = _ContactPoint.position;
// Getting the rigidbody
_HeldedItemRigidbody = collision.gameObject.GetComponent<Rigidbody>();
// Setting the parent
_HeldedItemRigidbody.transform.parent = transform;
// Setting the body to be kinematic
_HeldedItemRigidbody.isKinematic = true;
// Stopping any movement or rotation
_HeldedItemRigidbody.velocity = Vector3.zero;
_HeldedItemRigidbody.angularVelocity = Vector3.zero;
}
}
public void ShootTheBall() {
// Reverting what's done in OnCollisionEnter
_HeldedItemRigidbody.transform.parent = null;
_HeldedItemRigidbody.isKinematic = false;
// Adding force for the shoot
_HeldedItemRigidbody.AddForce(transform.forward * _ShootingStrength, ForceMode.Impulse);
// Resetting helding item to null
_HeldedItemRigidbody = null;
}
public void GameOver() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
You have to add a layer, and set the ground to this new layer.
Set up the variable as you like, don't hesitate to mess up with it a little bit to find what suits you (and don't forget to set up the player's mass (in rigidbody) as well!!)
Exemple of setup for the player script
Last things, you have to add an empty game object to your player which will be where the ball will be placed. I know it's a little bit dirty but place it a bit far from the player (more than the ball's radius) or it will bug. We could do something programatically to fix the ball at a spot and adapt this spot to the ball's radius.
This is an example of player
As you can see, I have the empty "player" gameobject which has the rigidbody, the collider and the player script.
The GFX item which is just the capsule.
If you don't know about this: the player game object is actually on the floor (at the player's feet) so it's better for movements. If you do this, don't forget to adjust the player's capsule collider to fit the GFX.
I also have the contact point.
Forget about the nose stuff, it's just a way to see where the player is facing when you just have a capsule.
If you have any question, feel free to ask.

2D unity platformer player movement question

i am trying to make a 2D unity platformer that shoots projectiles to the mouse direction.
so, i was trying to make the player move. but, there is a problem. it does not jump as i expected. when i pressed the jump key, sometimes, it jumps, but sometimes, it doesn't. and i think the jump doesn't usualy work while moving but i don't know how to fix this.
i tried putting the jump function into the if statement,changed variables, but it still doesn't work as i thought.
i used the sprite render to flip the sprite so the child objects does not flip with the player later in the game progress(because when i did it by vector and rotation at my first try, it didn't ended well)
public class playerMovement : MonoBehaviour
{
//movement variables
public float speed;
private float moveInput;
//player components
private Rigidbody2D rb;
private SpriteRenderer spriteRender;
//jump variables
private float jumpInput;
public float jumpForce;
//ground variables
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
spriteRender = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate()
{
moving();
jumping();
}
//move left & right function
void moving()
{
moveInput = Input.GetAxis("Horizontal");
if (moveInput > 0)
{
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
spriteRender.flipX = false;
}
else if (moveInput < 0)
{
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
spriteRender.flipX = true;
}
}
//jump function
void jumping()
{
//this input is what i tried for the jump function at the first
//jumpInput = Input.GetAxis("Jump");
//rb.velocity = new Vector2(rb.velocity.x, jumpInput * jumpForce);
//but it fell down slowly so i made this line of code
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
He I had the same mistake. You just have to put "jumping()" in "Update()"!
Because if it is in the Fixedupdate it will only allow you to jump every second or something like that.
https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
Alternatively, you could use rb.AddForce to make the character jump.

Player Momentarily Stuck at Edges of Screen! Unity

i am working on a Unity 2d game thats rounded about a player dodging random objects while flying...something like flappybird. the game work normal.
But the problem is when the player when touch the top screen edge and keep adding force the player get stuck a while on the edge. i think i need to disable adding force when the player touch the edge of the screen.
I hope u guys help me.
This the player code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jetpack: MonoBehaviour
{
public GameManager GameManager;
private Rigidbody2D rb;
private float jumpForce = 40f;
private bool engineIsOn;
[SerializeField] private GameObject fire;
void Start()
{
engineIsOn = false;
fire.SetActive(false);
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
engineIsOn = true;
fire.SetActive(true);
}
else
{
engineIsOn = false;
fire.SetActive(false);
}
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -4.5f, 4.5f),
Mathf.Clamp(transform.position.y, -4.5f, 4.5f), transform.position.z);
}
private void FixedUpdate()
{
switch (engineIsOn)
{
case true:
rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Force);
break;
case false:
rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
break;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
GameManager.gameover();
}
}
Whenever dealing with Rigidbody then don't set any values via the Transform component!
This might break the physics.
And then basically you already said it: Don't add more upwards force if you are on the edges.
As a simple trick you could set the Y velocity to 0 when hitting the edges like
private void FixedUpdate()
{
// Very unusual to use a switch for a single bool
if (engineIsOn)
{
rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Force);
}
// This does absolutely nothing
// Adding a force of 0,0 doesn't change the velocity at all ...
//else
//{
// rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
//}
// Check if currently hitting an edge
// and if so set the velocity on that axis to 0
// also clamp the position then
var velocity = rb.velocity;
var position = rb.position;
if(Mathf.Abs(rb.position.x) >= 4.5f)
{
velocity.x = 0;
position.x = Mathf.Clamp(position.x, -4.5f, 4.5f);
}
if(Mathf.Abs(rb.position.y) >= 4.5f)
{
velocity.y = 0;
position.y = Mathf.Clamp(position.y, -4.5f, 4.5f);
}
rb.velocity = velocity;
rb.position = position;
}

Player Script cant move right and jump at the same time

I'm learning Unity, and I'm writing a player script. Based on the script I've written, I expect to see my player to be able to jump while standing still, while moving left, and while moving right. The player cant jump if it is moving right at the same time. I did a bunch of refactoring and reorganizing. I think it might have something to do with Input.GetButton("Jump").
Also, I changed rb2d.AddForce(new Vector2(0.0f, jumpHeight)) to rb2d.velocity = new Vector2(0.0f, jumpHeight), but the player just disappears.
Here's my script so far:
private Rigidbody2D rb2d;
[SerializeField]
private LayerMask whatIsGround;
private bool isTouchingGround;
private bool facingRight = true;
[SerializeField]
private float speed;
[SerializeField]
private float jumpHeight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
Flip(moveHorizontal);
if (Input.GetButton("Jump") && IsGrounded()) {
rb2d.AddForce(new Vector2(0.0f, jumpHeight));
}
rb2d.velocity = new Vector2(moveHorizontal * speed, rb2d.velocity.y);
}
private void Flip(float horizontal) {
if ((horizontal > 0 && !facingRight) || (horizontal < 0 && facingRight)) {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private bool IsGrounded() {
if (rb2d.velocity.y <= 0) {
foreach (Transform point in groundPoints) {
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
foreach (Collider2D collider in colliders) {
if (collider.gameObject != gameObject)
return true;
}
}
}
return false;
}
I found a solution, but I'm not sure why it fixed the problem. If you have an idea why, please comment below!
In the private bool IsGrounded() method, I removed the if statement checking if rb2d.velocity.y <= 0. I thought that would be a helpful check to see if the player is moving down or stationary. For some reason, moving the player right was making this if statement fail. This seems like a bug.

Make ball Jumping

I am trying to make a script where i can move a ball, horizontal and vertical. I managed to get that working.
But now I want to make my ball "jump". I ended with script below, but now my ball just get launched like a rocket xD
Can anyone help me out
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpSpeed;
public GUIText countText;
public GUIText winText;
private int count;
void Start()
{
count = 0;
SetCountText();
winText.text = " ";
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
Vector3 jump = new Vector3 (0, jumpSpeed, 0);
GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
if (Input.GetButtonDown ("Jump"));
GetComponent<Rigidbody>().AddForce (jump * jumpSpeed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PickUp") {
other.gameObject.SetActive(false);
count = count +1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 10)
{
winText.text = "YOU WIN!";
}
}
}
Jumping does not work with adding a continuous force on an object. You will have to apply a single impulse to the object once when the jump button is first pressed. This impulse will also not include a time factor, because it is applied only once. So you would get something like this:
bool jumping;
if (Input.GetButtonDown ("Jump") && !this.jumping);
{
GetComponent<Rigidbody>().AddForce (jumpForce * new Vector3(0,1,0));
this.jumping = true;
}
Also note that in your example you are multiplying the upward unit vector by the jumpspeed twice. Once in the jump vector initialization and then once in the AddForce method.
Of course, you will also have to make sure that gravity applies to pull the object back down (and if the object hits the ground, reset the jump bool.
In general, depending on what kind of game you are making, it is easier to just set the velocity of the object yourself and don't work with the Unity physics engine to do some simple moving around.
There is an error in your code in the function FixedUpdate:
if (Input.GetButtonDown ("Jump"));
in that way you are applying a force on your object at every frame because the semicolon is excluding the row below from the condition. By removing the semicolon you will have a correct if implementation in case of a jump, as long as on your RigidBody component UseGravity is enabled.
if (Input.GetButtonDown ("Jump"))
GetComponent<Rigidbody>().AddForce (jump * jumpSpeed * Time.deltaTime);
Hope it helps.
Thanks everyone, it was a great help. I now have a jumping character. One who can only jump when grounded.
public bool IsGrounded;
void OnCollisionStay (Collision collisionInfo)
{
IsGrounded = true;
}
void OnCollisionExit (Collision collisionInfo)
{
IsGrounded = false;
}
if (Input.GetButtonDown ("Jump") && IsGrounded)
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 10, 0);
}

Categories

Resources