Search Unity

I made movement physics and when i play the object goes to 1 direction WITHOUT ME PRESSING ANYTHING!

Discussion in 'Physics' started by Ghostly_LP, Apr 13, 2021.

  1. Ghostly_LP

    Ghostly_LP

    Joined:
    Apr 13, 2021
    Posts:
    1
    HELP i made myself my first movement script and when i press play my cube just goes 1 direction without me pressing anything please help ASAP
     

    Attached Files:

  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    If you post your script in code tags, people are far more likely to look at it (99% of the time I do not bother looking at problems with images of scripts because I am too lazy to type it out to test it), here it is for you:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveCube : MonoBehaviour
    6. {
    7.     public float speed = 0.1f;
    8.  
    9.     void Update()
    10.     {
    11.         float xDirection = Input.GetAxis("Horizontal");
    12.         float yDirection = Input.GetAxis("Vertical");
    13.         Vector3 moveDirection = new Vector3(xDirection, 0, yDirection);
    14.         transform.position += moveDirection * speed;
    15.     }
    16. }
    Whilst we are on that subject, it's better not to use caps lock.

    The code seems to work OK for me. Here are some potential problems you may be having:

    1. You don't say which direction... do you have a rigidbody on the cube that is under gravity?
    2. Do you have a controller connected to your PC that is producing constant output?
    3. Try adding Debug.Log("moveDirection: " + moveDirection); to your Update() method and check your console log to see what the inputs are.
    4. Is the cube starting off in collision with another object? I.e. is it next to another cube with a rigidbody on it?