Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rodude123 · Aug 02, 2019 at 12:20 AM · 2dcharactertransform.positionrigidbody.velocityinfinite runner

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);
     }
 }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DCordoba · Aug 02, 2019 at 04:28 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 !

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rodude123 · Aug 02, 2019 at 10:11 AM 0
Share

I haved updated the question with full code

avatar image Dartictheunic rodude123 · Aug 02, 2019 at 11:48 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

244 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges