- Home /
Rigidbody2d.velocity making character stop in between
I'm making a 2D infinite runner game. I want to make a player move to the right all the time. I added this to my Update method, with moveSpeed
being a field variable that I had created.
rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
The problem I am having is the all of a sudden the player just stops moving unless I manually change the x position. Then I tried changing the transform position like so:
transform.position = new Vector3(transform.position.x + moveSpeed/10, transform.position.y, transform.position.z);
This works but I am not sure which way is good practice and if using velocity is the way I should be doing it what I am a doing wrong. If you can help that would be great!
Update 1:
Here is the full code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public bool grounded;
public bool doubleJump;
public bool sliding;
public LayerMask whatIsGround;
private new Rigidbody2D rigidbody2D;
private new Collider2D collider2D;
private Animator animator;
// Start is called before the first frame update
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
collider2D = GetComponent<Collider2D>();
animator = GetComponent<Animator>();
doubleJump = false;
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.IsTouchingLayers(collider2D, whatIsGround);
sliding = false;
//rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
transform.position = new Vector3(transform.position.x + moveSpeed/10, transform.position.y, transform.position.z);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.UpArrow))
{
if (grounded)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
doubleJump = true;
}
else if (doubleJump)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
doubleJump = false;
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
sliding = true;
}
animator.SetBool("grounded", grounded);
animator.SetBool("sliding", sliding);
}
}
you need rigidbody velocity to collider/trigger detection, if rigidbody velocity is zero, and you just change transform.position
, even if it where colliding cant trigger OnCollider...
events, so, if you use collider detection, you will need to use rigidbody.velocity
if you use raycast, overlap sphere, u other way to detect proximity with obstacles, you can any, the costs of erformance depends of the tasks, and the device (platform) that you are using
Answer by Dartictheunic · Aug 02, 2019 at 08:12 AM
It will depend on how you want to manage your game. Using velocity is a good way to ensure collisions will be perfect but can lack precision when your project grows bigger.
If you're new to Unity and scripting, using Rigidbody movement is a good idea in my opinion. You could try using rigidbody2D.MovePosition since using rigidbody2D.Velocity is physic-dependant and lacks control (if you collide with something you can quickly have values you don't want interfering with your code).
Here is an exemple of how you could achieve this:
public class SimpleMovement : MonoBehaviour
{
public float playerSpeed;
Vector2 playerMovement;
Rigidbody2D playerBody;
// Start is called before the first frame update
void Start()
{
playerBody = GetComponent<Rigidbody2D>();
playerMovement = new Vector2(playerSpeed, 0f);
}
// We are using FixedUpdate to avoid unreliable speed and collision problems
void FixedUpdate()
{
//Move the player TO a new position, so adding the speed we want to its position
playerBody.MovePosition((Vector2)transform.position + playerMovement); //This allows us to leave this here and change playerMovement according to our needs
}
}
Since I've not seen your entire code I'm not sure if everything is clear, don't hesitate to ask if you don't understand something !
Well you can use my code and replace
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
with
rigidbody2D.AddForce(0, jumpForce)
*jumpForce might need to be Higher then it is actually when using this method
Your answer
Follow this Question
Related Questions
Raycast from Character to Mouse Position? 2 Answers
"wheel" character 2 Answers
Pipe in a Snowboarding Infinite Runner not staying connected 0 Answers
2D Animation does not start 1 Answer