Align to surface normal doesn't work properly

Hi, everyone.

This is my first post here in the forums for this amazing game engine that is Unity.

I downloaded Unity 5 a couple of weeks ago and I’m now trying to make a game in which the player can choose to play as some arthropod (an insect, arachnid and the like).
Since these animals have the ability to walk on surfaces and, as such, it is one of the main types of movements that is going to be used, I decided to code it first.
This is my rationale for the code: I cast a ray for surface detection, get its position and normal vector, place the player there and adjust the player’s up vector to match the normal. For the movement I simply translate the player along its forward or right axis. These are the pieces of code involved in the process:

void FixedUpdate()
{
   if(AttachToSurface())
      Walk();
}

bool AttachToSurface()
{
    RaycastHit hit;
    bool hasHit = Physics.Raycast(transform.position, -transform.up, out hit, Mathf.Infinity, ~(1 << 8));

    if(hasHit)
    {
        transform.up = hit.normal;
        transform.position = hit.point + transform.up.normalized * height / 2;
    }

    return hasHit;
}

void Walk()
{
    float vertical = Input.GetAxis("Vertical");
    transform.Translate(transform.forward * vertical * movementSpeed * Time.fixedDeltaTime);

    float horizontal = Input.GetAxis("Horizontal");
    transform.Translate(transform.right * horizontal * movementSpeed * Time.fixedDeltaTime);
}

My scene setup is in a screenshot attached to this thread.

The problem that I’m facing is that, when I go to the sides of the capsule, my player doesn’t move, only when I’m on top of it, on the rounded portion. I’ve tried moving the cube (player) live through the scene editor using the handles, and then my attachment code seems to work fine. I suspect that setting my transform.up vector to hit.normal is somehow affecting the way the player should move using Translate(), because, when I’m on the side of the capsule and I press forward, the cube tries to get into the capsule, i.e. it tries to go downwards along its local y axis, even though I’m using transform.forward for the translation.

So, guys, any ideas on how to fix this problem?

Translate is relative to local space by default. Set the second parameter to Space.World and you should be good to go.

Oh, that was my mistake! I was using transform.forward as the axis for my translation, but, by default, it is already relative to the object’s local space. I fixed it by changing

void Walk()
{
    float vertical = Input.GetAxis("Vertical");
    transform.Translate(transform.forward * vertical * movementSpeed * Time.fixedDeltaTime);

    float horizontal = Input.GetAxis("Horizontal");
    transform.Translate(transform.right * horizontal * movementSpeed * Time.fixedDeltaTime);
}

to

void Walk()
{
    float vertical = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward * vertical * movementSpeed * Time.fixedDeltaTime);

    float horizontal = Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * horizontal * movementSpeed * Time.fixedDeltaTime);
}

Thank you very much!