Screenpoint forward movement aligned with 3d perspective view

Hello,

I'm working on a 3D scroll shooter.

My main 3D World character is following a 2D screen point. I use that screen point to keep my character always within the screen-bounds. Now I need a calculation so that my keyboard forward direction is aligned with the 3d perspective view of the camera. Any ideas how to calculate this movement from my keyboard input axis and apply it to the 2d screen position?

EDIT:

alt text

The camera is moving 10 m/s in the z direction. Perspective cam, rotated 45 degree with a fov of 45. the 2d screen point is translated with the keyboard input axis. The character is following in 3D space the 2d screenpoint with the ScreenPointToRay Camera function. I want to translate my 2D point to match the camera perspective view.

Hope that this quick sketch do a better explanation.

EDIT:

I found some part of the solution.

private Vector3 movement = Vector3.zero;

private Vector3 position2D = Vector3.zero;

void Start ()
{
    position2D = mainCamera.WorldToScreenPoint (thisTransform.position);
}

void Update ()
{

    //Get keyboard movement axis
    movement = new Vector3 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"), 0);

    if (!isMoving) {

        Vector3 move = mainCamera.ScreenToWorldPoint (position2D);

        move += new Vector3 (movement.x, 0, movement.y);

        Vector3 direction = mainCamera.WorldToScreenPoint (move);

        direction = direction - position2D;
        direction.z = 0;
        movement = direction.normalized * movement.magnitude;

        StartCoroutine (...further calculation of position2D and movement...);
    }
}

Peter.

I might have misunderstood your question. This answer applies motion to an object relative camera direction. Movement is done in X and Z, so that pressing Up will cause the object to move up along the screen (but not following screen coordinates), toward the same direction the camera is facing. Same with left, right, down of course. Perspective is still respected, so if an object is further away to the left and only walking up, it would look like it's moving up toward the center of the screen as it goes off into the distance. It's coordinate axis is uniform for all places given the camera doesn't move.

  • Take the input movement as a Vector3.
  • Get the direction of the input relative to the camera.
    • Ignore the y component.
    • Normalize the result.
  • Scale the direction with the magnitude of the input and respect Time.deltaTime.

See this example.

using UnityEngine;

public class CamMove : MonoBehaviour
{
    void Update()
    {
        Vector3 input = GetInput();
        Vector3 direction = GetInputDirection2D(input);
        transform.position += direction * input.magnitude * Time.deltaTime;
    }

    Vector3 GetInputDirection2D(Vector3 input)
    {
        Vector3 direction = Camera.main.transform.TransformDirection(input);
        direction.y = 0;
        return direction.normalized;
    }

    Vector3 GetInput()
    {        
        return new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    }
}