D
D
DraconVegasa2017-02-13 20:13:22
C++ / C#
DraconVegasa, 2017-02-13 20:13:22

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.
c7d70e3e2acd4ddaac3030d631c13b57.JPG
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);
  }
}

And the second problem is the different speed of movement to the left and right (to the left is slower than to the right).
What can you tell me to fix?
====== UPDATE =======
Through experiments, I came to the conclusion that the scripts are not to blame (completely disabled them, the problem remained). If you raise the player in Y up through the Editor, then he still continues to move to the right.
I give a screenshot of the inspector on the player6eaf7c12a1a14f448a83a9a5b85c13a6.JPG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2017-02-14
@DraconVegasa

rb.AddForce (movement * speed);
If the button is not pressed - do not apply force to the object!

G
GreatRash, 2017-02-14
@GreatRash

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 question

Ask a Question

731 491 924 answers to any question