Gewoo
Gewoo

Reputation: 457

Why does the z change when I move this cube? [c#][Unity]

I am really new to unity so I wanted to make a simple 2d project where you can move a cube. So I made a script to move the cube but when I play the game the Z changes along with the X so it will fall of the map.

Video: https://www.youtube.com/watch?v=M9oHSc6dN2A&feature=youtu.be

The script I'm using:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    private Vector2 input;

    public float movementSpeed = 50f;
    private float horizontal;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void FixedUpdate () {
        horizontal = Input.GetAxis ("Horizontal");

        rigidbody.AddForce ((Vector2.right * movementSpeed) * horizontal);
    }
}

I am using unity 4

Upvotes: 0

Views: 580

Answers (1)

Gunnar B.
Gunnar B.

Reputation: 2989

Your rigidbody has Use Gravity checked. Romove that and it should function the way you want. [Wrong axis]

Edit:

A Rigidbody has a constraint property. Freeze z position there.

enter image description here

Upvotes: 1

Related Questions