Jump to content
Haydenmango

Moving player Forward based on Camera Angle

Recommended Posts

Hi everyone.

So this may be basic for some people but I have been having some trouble making my character move forward depending on where the camera is facing. I can make my character rotate and even move, but not consistently forward.

I am not using Navmesh (the player is floating underwater) so I don't think SetInput() would work. I have tried messing with other scripts and functions but I am not the best at math yet so any help with this would be appreciated.

Share this post


Link to post

SetInput() has nothing to do with the Navmesh. SetInput() is how you move the character controller on the ground based on your inputs (generally for the player character you use SetInput() and for AI that use the NavMesh you use GoToPoint() or Follow()), however it won't work right for under water situations. That's a very key piece of information that you might want to put in your title because it changes how you have to do things.

Share this post


Link to post

Thanks for the clarification Rick. I wasn't sure if SetInput() was connected to the Navmesh or not; all I could tell was that it wasn't working in the air/water. I'm so tired I can't figure out how to change the title if I even can, hopefully someone will understand it if not I may change it later on today.

@DudeAwesome I know there is a 3rd person camera script and a 1st person character script but neither of them contain the answer. They only utilize SetInput().

Share this post


Link to post

@DudeAwesome :

This is not real TPS camera but just X-Z camera mainly, not a camera like in mmo games or real TPS games.

Correct me if i'm wrong.

 

Would be better for LE3 in the future to have to have for new comers and non coders, essential cameras ready to use :

- minimal camera FPS (not the FPS demo that is full of other code)

- Real TPS camera

Just a suggestion.

Share this post


Link to post

yeah a simple Entity:Forward(entity,amount) would be nice. tongue.png

 

Just rotate the entity to your needs and call Entity::Move(x,y,z).

 

Take a look at:

  1. http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/entity/
  2. http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/entity/entitymove-r165

 

For underwater simulations you probably want to turn off the gravity for the object and play with physics parameters such as friction.

Share this post


Link to post

The problem is after I rotate my entity I need to change my movement values somehow to accommodate for the change in rotation. Example-

if window:KeyHit(key.w) then self.move.z = 1 end

self.entity:Move(self.move)

now when I rotate I am still moving the same direction, no longer Forward. This is my issue.

I can move fine in the water without rotation.

Share this post


Link to post

wow.... so I set Move(self.move,false) and it works! I wasn't moving in local space.

Sorry about that and thanks for the responses everyone!

Share this post


Link to post

Note that Move() will kill any physics if you want that or not.

Share this post


Link to post

I calculate movement a different way than using SetInput(...). Each object in my game has an UP vector and a Right vector. The right vector is calculate every frame. the Up vector is assigned when the object is created. When I want to move my object forward or backward, depending on the rotation of my camera, I calculate the objects forward vector and move the object in that direction by a given speed.

 

In the folowing code snippet, the view vector is a Vec3 indicating where the object is facing. The allowY variable limit the ability to move the object in the Y plane, based on the objects rotation.

 

// ------------------------------------------------------------------
// Moves the object forward or backward depending on the given speed.
// ------------------------------------------------------------------
void esMovableObject::Move(float speed, bool allowY)
{
// Get the current view vector.
Vec3 l_vVector = m_vView.Subtract(m_vPosition).Normalize();

// Add the current view vector to the position.
m_vPosition.x += l_vVector.x * speed;
m_vPosition.z += l_vVector.z * speed;
// Add the current view vector to our view.
m_vView.x += l_vVector.x * speed;
m_vView.z += l_vVector.z * speed;
if(allowY)
{
m_vPosition.y += l_vVector.y * speed;
m_vView.y += l_vVector.y * speed;
}
} // end void esMovableObject::Move(float speed, bool allowY)
// -----------------------------------------------------------------------------------------

 

Here is an example of how I accomplish strafing with my objects. The m_vRightVector is updated every frame in the base object's update method. This is accomplished from the following code.

 

// ----------------------------
// Updates the esMovableObject.
// ----------------------------
void esMovableObject::Update(float deltaTime)
{
// Update the Right Vector of the esMovableObject
m_vRightVector = -m_vView.Subtract(m_vPosition).Cross(m_vUpVector).Normalize();
} // end void esMovableObject::Update(float deltaTime)
// -----------------------------------------------------------------------------------------

 

// ------------------------------------------------------------
// Moves the object left or right depending on the given speed.
// ------------------------------------------------------------
void esMovableObject::Strafe(float speed)
{
  // Add the strafe vector to our position.
 m_vPosition.x += (m_vRightVector.x * speed);
 m_vPosition.z += (m_vRightVector.z * speed);

 // Add the right vector to our view.
 m_vView.x += (m_vRightVector.x * speed);
 m_vView.z += (m_vRightVector.z * speed);
} // end void esMovableObject::Strafe(float speed)
// -----------------------------------------------------------------------------------------

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...