Vector3 newPosition = new Vector3(-horizontal, 0.0f, -vertical);
    transform.LookAt(newPosition + transform.position);
    transform.Translate(newPosition * MoveSpeed * Time.deltaTime, Space.World);

I'm developing 3d mobile game, you can look around with hold&sliding the right side of the screen and move with virtual joystick.


Moving character to camera's lookDirection

Issue is;

When i push joystick up, character moves to the same direction (world related)

And I'm not good at coding that's why i can't solve the problem myself, i would be glad if you help me

  • \$\begingroup\$ Can you show us what your game looks like, so we understand the typical camera perspective? \$\endgroup\$
    – DMGregory
    Apr 14 '19 at 12:12

Camera look direction is represented by its transform orientation value. The vector you want to move your character along is transform.forward of your camera.

  • \$\begingroup\$ In games where the camera hovers above the character, tilted slightly downward, using camera.transform.forward directly can end up drilling the character into the floor when moving forward, or making them hover off the ground when moving backward. So sometimes we need to first flatten that direction into the character's floor plane. \$\endgroup\$
    – DMGregory
    Apr 14 '19 at 12:14
  • \$\begingroup\$ Wow that's actually the correct situation @DMGregory, perspective is just like that. Can you please tell me how to flatten that, or simply how to write script according to that? \$\endgroup\$ Apr 14 '19 at 12:17
  • 1
    \$\begingroup\$ @AmonRa'sChannel in that case, it sounds like your question might be a duplicate of this existing Q&A about getting camera-relative movement parallel to the ground in Unity. If this doesn't solve your problem, please edit your question to clarify what you're doing now and what you need to change about its behaviour. \$\endgroup\$
    – DMGregory
    Apr 14 '19 at 12:25
  • \$\begingroup\$ @DMGregory i have red the post that you have shared. But it didn't solve my problem, i can't use Vector3 to "Space". What can i do? \$\endgroup\$ Apr 15 '19 at 16:18
  • \$\begingroup\$ @AmonRa'sChannel don't try to put a square peg in a round hole. Don't put a Vector3 in a spot that's expecting a Space. Put the Vector3 in the Vector3's spot, and put the Space in the Space's spot: transform.Translate(worldSpaceInput, Space.World); \$\endgroup\$
    – DMGregory
    Apr 22 '19 at 16:18

Here's how I calculated my movement according to my camera's direction:

Note: There's TouchField because my game is for mobile. If your game is not mobile you need to calculate somethings different than mine.

void Movement()
    Vector2 vec = new Vector2(horizontal, vertical);
    vec = Vector2.ClampMagnitude(vec, 1f);

    Vector3 camF = cam.transform.forward;
    Vector3 camR = cam.transform.right;
    Vector3 rbVelocity = rb.velocity;

    camF.y = 0;
    camR.y = 0;
    rbVelocity.y = 0;
    camF = camF.normalized;
    camR = camR.normalized;

    targetPosition = (camF * vec.y + camR * vec.x) * MoveSpeed;

    Vector2 targetVelocity = (targetPosition - transform.position)/Time.deltaTime;

    rb.AddForce(targetPosition - rbVelocity, ForceMode.VelocityChange);

    transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, Mathf.Clamp(transform.eulerAngles.z, 0, 0));

    Vector3 deltaPosition = transform.position - prevPosition;

    if (deltaPosition.sqrMagnitude > 0.1 * Time.deltaTime * Time.deltaTime)
        transform.forward = deltaPosition;

    prevPosition = transform.position;

    CameraAngle += FixedTouchField.TouchDist.x * CameraAngleSpeed;

    Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);


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.