Answer the question
In order to leave comments, you need to log in
Character movement along the camera's gaze vector?
Hello, I'm making a 3D game where the character is a Ball (sphere). Which I move with the help of Rigidbody.addForce
The control is made through the UI joystick, in the code it turns out:
rb.addForce(joystick.Horizontal * speed, 0f,joystick.Vertical * speed)
Immediately I say, by making an empty object in front of the player and fixing it so that he was always parallel to the camera, not an option because I did it, but anyway: I pull the joystick forward and the sphere moves along the common Z axis (forward). How to make the character move along the camera's view vector?
Answer the question
In order to leave comments, you need to log in
Hey! If I understand everything correctly, then:
public GameObject camera;
rb.AddForce(camera.transform.forward * joystick.Vertical * speed, 0f, camera.transform.right * joystick.Horizontal * speed);
By the way, it looks like you mixed up the joystick axes in your code, I advise you to change them (you pass the "Horizontal" axis in "x", which is incorrect from the point of view of the vector), otherwise misunderstandings of the whole system may arise later.
It is better to study normally right away, this is a general principle with which you need to approach any issue. Otherwise, every day it will be "forget everything you were taught before."
You will almost never need to move the character in the direction of the camera, this is self-deception so as not to strain. Even with a balloon, this trick will not work, because the customer / designer / players / common sense will sooner or later ask you to add eyes to the balloon so that it is clear that this is a character. We take the direction vector of the camera, project it onto the plane in which the character walks, as a rule, for balls this is xz. And after that, you need to rotate the BALL in the direction of the resulting vector, and it will walk correctly rotated through, for example, rb.AddForce(rb.transform.forward*......)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question