Align CharacterTo Wall Problem

Hey, I am trying to align my characters Y position to a wall.
So in this example, the lower capsule part should stick to the red area:

8478845--1127102--GIF 30-09-2022 08-57-38.gif

here is my code:

using UnityEngine;

public class MoveTest : MonoBehaviour
{
    [SerializeField] Vector3 _direction;
    public Transform wall;

    void Update()
    {
        _direction.x = Input.GetAxis("Horizontal");
        _direction.z = Input.GetAxis("Vertical");

        UpdatePosition();
        UpdateRotation();
    }
    void UpdatePosition()
    {
 
        transform.position += wall.right * _direction.x * 0.1f;
        transform.position += wall.up * _direction.z * 0.1f;
    }
    void UpdateRotation()
    {
        var _targetRotation = Quaternion.FromToRotation(transform.up, -wall.forward) * transform.rotation;
        transform.rotation = _targetRotation;
    }
}

what do i have to do, to make get the Y of the character stay at the red wall, while still being able to move left and right/up down?

Also note, that the red and white boxes are just there for showing the problem.
Only the blue box will be there in the end - without a collider.
So raycasting down to the red cube wont be possible

so i figured out if i use a raycast down from the character (-transform.up) it does exactly what i want.

but the red and white box wont be there in the game, there is nothing to raycast against. all i know is the position of the blue cube.
i just wonder how i can do it all without the raycast?

   void UpdatePosition()
    {

        transform.position += wall.right * _direction.x * 0.1f;
        transform.position += wall.up * _direction.z * 0.1f;


        float offset = 1;
        RaycastHit hit;
        Physics.Raycast(transform.position, -transform.up, out hit, Mathf.Infinity);

        Vector3 newObjectLocation = (-transform.up * (hit.distance - offset)) + transform.position;
        transform.position = newObjectLocation;
 

    }

8479196--1127159--GIF 30-09-2022 11-20-42.gif

edit, i am curretnlty trying to get the flat distance from the purple capsule to the blue wall. like this but without any luck

        flatWall = new Vector3(wall.position.x, transform.position.y, wall.position.z);
        var distance = Vector3.Distance(transform.position, flatWall);

the distance based on the raycast - and so the value i need is 2.2, but with the solution above i get… 0.05. so something is super wrong

Here you are good sir

the plane cast is my favourite.

learn more here

thanks for your answer :slight_smile:
def learned something new :slight_smile:

but in the end, there wont be a collider on the cubes at all - i think its better to think of the blue cube as an empty gameobject - so sadly, a raycst wont work

So if you are dealing with a single point in space.
Then your objects position is clamped to that point, +- Bounds.x and Bounds.y. If object can’t exceed the point in space +- bounds x and bounds y, then it will remain in the parameters of those bounds but will be moveable within.

the bounds x and bounds y may technically be point parent or empty object +/- transform right * boundsX and +- transform up * boundsY.
Or If there is no transform then you will need to get the facing of point in space where the mathematical plane will be oriented. Which will need to be obtained by referencing a global vector direction.