-1
\$\begingroup\$

I want to make a biped character stand on any surface I click on.

Surfaces have up vectors of any of positive or negative X,Y,Z. So imagine a cube with each face being a gameobject whose up vector pointing directly away from the cube.

If my character is facing "forward" and I click on a surface which is to the left or right of me ( left or right walls), I want my character to now be standing on that surface but still be facing in the direction he initially was. If I click on a wall which is in the forward path of my character i want him to now be standing on that surface and his forward to now be what was once "up" relative to my character. Here is the code I am working with now.

void Update()
{
    if (Input.GetMouseButtonUp (0)) {
        RaycastHit hit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit)) {
            Vector3 upVectBefore = transform.up;
            Vector3 forwardVectBefore = transform.forward;
            Quaternion rotationVectBefore = transform.rotation;

            Vector3 hitPosition = hit.transform.position;
            transform.position = hitPosition;
            float lookDifference = Vector3.Distance(hit.transform.up, forwardVectBefore);

            if(Vector3.Distance(hit.transform.up, upVectBefore) < .23) //Same normal
            {
                transform.rotation = rotationVectBefore;
            }
            else if(lookDifference > 1.412 && lookDifference <= 1.70607) //side wall
            {
                transform.up = hit.transform.up;
                transform.forward = forwardVectBefore;
            }
            else //head on wall
            {
                transform.up = hit.transform.up;
                transform.forward = upVectBefore;
            }
        }
    }
}

The first case "Same normal" works fine, however the other two do not work as I would like them to. Sometimes my character is laying down on the surface or on the wrong side of the surface. Does anyone know nice way of solving this problem?

\$\endgroup\$
1
\$\begingroup\$

Without being too assertive here, it seems you are using a lot of "Magic Numbers"

I think a nice solution here could be to use the feature :

RaycastHit.normal

Which will give you the exact direction for the upvector of your object. And use the nice built-in :

Transform.LookAt(target, RaycastHit.normal);

it could give something like that :

void Update()
{
if (Input.GetMouseButtonUp (0)) {
    RaycastHit hit;
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit)) {

        transform.position = hit.point;
        transform.LookAt(target, hit.normal);

    }
}
}

Knowing that target is the point your transform you want to spawn is looking at.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.