fix object on an axis

Hey guys,

I'm trying to fix an object on one axis. For example: An object is falling on the Y Axis. I can control the object on the Z Axis. Now I won't forbid the object to go out of place on the X Axis, if it hits an object.

Is it possible to freeze it on the x axis, that it cant move on it?

You could use a script like that and mark the axis you want to freeze:


using UnityEngine;
using System.Collections;

public class FreezeMovement : MonoBehaviour
    public bool FreezeX = false;
    public bool FreezeY = false;
    public bool FreezeZ = false;
    private Vector3 m_OriginPos;
    void Start ()
        m_OriginPos = transform.position;

    void Update ()
        Vector3 currentPos = transform.position;
            currentPos.x = m_OriginPos.x;
            currentPos.y = m_OriginPos.y;
            currentPos.z = m_OriginPos.z;
        transform.position = currentPos;

If you're using a rigidbody, check the box next to X in Freeze Position under Constraints.

you can use something simple like this:

function Update(){ transform.position = Vector3(0,transform.position.y,0);

//where it can only move in the y-axis. the x and z stay at 0.