0
\$\begingroup\$

I am trying to create a simple game using Amazon Lumberyard and my goal is to have levels/stages where the player will be able to walk around a mini planet.

I have seen a lot of tutorials about this for Unity, but I find it hard to try to translate this into Lua.

My problem is trying to make the "Feet" of the character point towards the planet. Any help would be appreciated.

the script I got for unity is below,

Planet

public class PlanetScript : MonoBehaviour {

public float gravity = -12;

public void Attract(Transform playerTransform)
{
    Vector3 gravityUp = (playerTransform.position - transform.position).normalized;
    Vector3 localUp = playerTransform.up;

    playerTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);

    Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * playerTransform.rotation;
    playerTransform.rotation = Quaternion.Slerp(playerTransform.rotation, targetRotation, 50f * Time.deltaTime);
    }
}

and the player has

void FixedUpdate()
{
    if (attractorPlanet)
    {
        attractorPlanet.Attract(playerTransform);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you give an example of a method for pointing the feet you found in Unity, and highlight the parts you need help translating into Lumberyard's Lua API? \$\endgroup\$ – DMGregory Aug 30 '18 at 10:46
  • \$\begingroup\$ Actually if you know how to do the "Quaternion.FromToRotation" using Lumberyard's api, that would be great. \$\endgroup\$ – Nerdicon Sep 4 '18 at 1:23
0
\$\begingroup\$

My Lua is pretty rusty but functionally there are several ways to do this. The easiest may be to set your characters rotation (Quaternion or Vector). If you create a new vector which is the result of playerPosition - planetPosition, you want to make this your players new Up Vector (I would normalize this vector).

You can either a) get the rotation of this vector and then set your players rotation vector. Or b) convert this vector to a quaternion and then setting your players rotation quaternion.

Quaternions are the best!

\$\endgroup\$
  • \$\begingroup\$ Quick question, what is the difference between using quaternion and vector3? I know that vector 3 represents the xyz axis, but I am not familiar with quaternions. I am interested in this because it seems I would be using a lot of it? although I am not sure. \$\endgroup\$ – Nerdicon Aug 31 '18 at 3:13
  • \$\begingroup\$ @Nerdicon euler angles (the vec3 approach) have lock up problem at some angles. Quaternions don't. Quaternions are almost always better, the only exception I can think of right now is the camera \$\endgroup\$ – Bálint Aug 31 '18 at 9:19
  • \$\begingroup\$ @ShotgunEmmet thanks for the explanation. Unfortunately I have not yet solved the problem, I have already done PlayerP - PlanetP = V3 but I have no idea how to make this the "up" of the character. What I did was to make a new vector with the original x and y of the players then use the z value of the V3. I hope it made sense. \$\endgroup\$ – Nerdicon Aug 31 '18 at 9:39

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.