B
B
barniok2020-05-28 13:27:18
C++ / C#
barniok, 2020-05-28 13:27:18

Why does an object move by inertia?

I have control, for some reason the object flies further by inertia, how to fix it?
In Rigidbody 2D gravity is set to 0.
Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class object : MonoBehaviour
{
    
   
    public int velocity = 1;

    private Rigidbody2D rb;

    void Awake()

    {
        
        rb = GetComponent<Rigidbody2D>();

    }

    void Update()

    {
        if (Input.GetKey("space"))


            rb.velocity = Vector3.down* velocity;

        if (Input.GetMouseButtonDown(0))

            rb.velocity = Vector3.up * velocity;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-05-28
@barniok

It's just plain physics. You set the object a speed, so it moves with it. Nothing prevents him. Either introduce friction or return speed to 0 when needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question