S
S
Saveli Tomak2017-01-28 22:04:29
Unity
Saveli Tomak, 2017-01-28 22:04:29

How to organize the movement of the character based on the incoming Vector3?

All the best!
Here is a code snippet that makes an object move:

Vector3 InputVector = joystick.GetInputVector();

this.gameObject.transform.position += InputVector * 3 * Time.deltaTime;

The problem is that when I rotate the character (if I turned it 45 degrees in transform.rotation.x for example) and move the character forward with the joystick, it goes sideways, that is, in the direction in which it was originally turned, I would like to achieve moving forward (left, right, back) relative to the camera, that is, if the character is facing south, and I drag the joystick forward so that the character moves south.
What do you suggest about this?
Thank you all in advance. I apologize for this wording of the question, in this area I am still very green.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2017-01-29
@aresouji

For such purposes, Transform.TransformDirection is suitable . The joystick gives you direction in local character coordinates, and you want to change world coordinates. Take the vector from the joystick, transform it with TransformDirection, and then use the new vector as before.

P
Pparma, 2017-01-29
@Pparma

Сделайте персонажу родительский объект и применяйте повороты к нему, а перемещение задавайте в localPosition персонажа.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question