0
\$\begingroup\$

Hello I am trying to move the character in joystick direction

enter image description here

Green arrow is what I am trying to get.

private void OnMovement(InputAction.CallbackContext obj)
    {
        if(obj.performed){
            _MovementPoint = new Vector3(-obj.ReadValue<Vector2>().y, 0,obj.ReadValue<Vector2>().x ).normalized;  
            Debug.Log(this.transform.position);
        }
    }

And I am calling this on Update

 private void Update()
    {
        this.transform.position = this.transform.position + _MovementPoint*Time.deltaTime*6;
    }

Is there a way?

\$\endgroup\$
4
  • \$\begingroup\$ It's problem is it should rotation 45 angle ? \$\endgroup\$
    – TimChang
    Jul 5 at 9:38
  • 1
    \$\begingroup\$ because your camera is 45 angle \$\endgroup\$
    – TimChang
    Jul 5 at 9:39
  • \$\begingroup\$ @TimChang Yes that's what I am trying to do \$\endgroup\$
    – Alt C
    Jul 5 at 10:36
  • \$\begingroup\$ @TimChang that worked thanks : newMovementDirection = Quaternion.AngleAxis(45, Vector3.up) * _MovementPoint; \$\endgroup\$
    – Alt C
    Jul 5 at 10:56

1 Answer 1

2
\$\begingroup\$

The screen coordinate system and the world coordinate system are different. You can convert the coordinates by Camera.ScreenToWorldPoint. Find the transformed vector by transforming two points:

void Update()
{
    var p1 = Camera.main.ScreenToWorldPoint(Vector3.zero);
    var p2 = Camera.main.ScreenToWorldPoint(_MovementPoint);
    var moveDir = p2 - p1;
    this.transform.position += moveDir * Time.deltaTime * 6;
}
\$\endgroup\$
5
  • \$\begingroup\$ Thanks, I understand but this giving me a strange movement. \$\endgroup\$
    – Alt C
    Jul 5 at 10:35
  • \$\begingroup\$ @AltC Hello, I found that _MovementPoint is on the x-z screen. Shouldn't it be on the x-y screen? \$\endgroup\$
    – Mangata
    Jul 5 at 10:39
  • \$\begingroup\$ Hi, y is upward axis? so that's okay right \$\endgroup\$
    – Alt C
    Jul 5 at 10:45
  • \$\begingroup\$ This worked for me : newMovementDirection = Quaternion.AngleAxis(45, Vector3.up) * _MovementPoint; \$\endgroup\$
    – Alt C
    Jul 5 at 10:56
  • \$\begingroup\$ @AltC If you found a solution to your problem that differs from those give, I encourage you to write it up & post it as an answer. \$\endgroup\$
    – Pikalek
    Jul 5 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .