Answer the question
In order to leave comments, you need to log in
Unity | The object moves by itself (2d ) what to do?
Good afternoon. Recently started working with Unity. Decided to make a small platformer.
The problem is this:
There is a character, an even square, through AddForce a force acts horizontally on it. The fact is that even if no force is acting (checked through the display in the movement console ), then the square itself moves to the right. The body moves on the same flat surface. Neither the character nor the floor has any angles.
Movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float speed;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
Vector2 movement = new Vector2 (moveHorizontal, 0);
rb.AddForce (movement * speed);
Debug.Log (movement);
}
}
Answer the question
In order to leave comments, you need to log in
rb.AddForce (movement * speed);
If the button is not pressed - do not apply force to the object!
So you launched the game and he immediately went? I do not believe. You probably poked the arrow on the clave and he went. Well, Duc strength has been added to the object, nothing takes it away. Naturally, it will go slower in the other direction, because you add the opposite "left" to the already existing force "to the right". I suppose that if you hold down the button on the keyboard at all, then it will go with acceleration.
In general, if you need a constant speed, then you need to stupidly use translate. Vaughn in the documentation is almost your example.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question