- Home /
How do I turn 1 objects rotation into another objects movement direction?
I'm making a VR project. The VR Camera Rig contains 2 controllers, which are rotated freely by the player. I want to apply a continuous movement speed to the camera rig that can be adjusted with inputs, which is pretty easy and straight-forward. But the part I'm having trouble wrapping my head around is this: I want the Camera Rig to change movement direction -but NOT rotate- in the direction that one of the controllers is pointing. IE: The Camera Rig and I are facing forward, and if I point the controller to the bottom-left, the Camera rig will move that (or any) direction but continue to face forward. I feel like the answer is obvious, and that maybe this has revealed a gap in my understanding of simple coding principals. Either way, I would appreciate any suggestions or explanations.
Answer by CodesCove · Mar 03, 2021 at 09:39 PM
You can attach Something like this to the some component in the rig inside Update:
this.transform.Translate(controllerTransform.forward * speed * Time.deltaTime);
or
this.transform.position = Vector3.MoveTowards(this.transform.position, controllerTransform.forward + this.transform.position, speed * Time.deltaTime);
where controllerTransform is the Transform component of you controller and speed is float value representing the speed in units per second.
Worked like a charm! Thanks! I had to tweak it to work with my weird setup, but it's more than functional enough at least for testing purposes! And I was right, the best answer was the simplest, of course. Also, I didn't know about "this" (as in referring to another component of self), and I look forward to applying it in the future.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotating game object to movement direction 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers
Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer