Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm looking to move the player in the opposite direction they are facing with a specified force. Rather than try to explain that better I created an image showing what I mean,

http://i.imgur.com/FxuCuK6.png

I've tried reversing the cameras values but I couldn't get that to work, any ideas? Or was revering the values the correct way to go about it? If so how would I properly do that in Unity using C# (Assuming the Main camera is my player)?

EDIT: For replacing the camera values I can't find the old code, but I remember multiplying the camera rotation values by -1 and using that with a speed vector.

share|improve this question
    
When you say you tried reversing the camera values, what exactly did you do? Can you post and describe your code? –  Seth Battin Aug 22 '13 at 19:36
    
Is it a first person game? –  Joseph Mansfield Aug 22 '13 at 19:38
    
@Seth I no loner have it but will do my best to replicate it in a second –  thedeadlybutter Aug 22 '13 at 19:40
1  
@thedeadlybutter If you have a camera direction vector (x,y,z), the opposite direction is (-x, -y, -z). –  Joseph Mansfield Aug 22 '13 at 19:50
1  
simply use "camera.transform.position = camera.transform.forward * -movespeed"; –  Benjamin Danger Johnson Aug 22 '13 at 21:20

2 Answers 2

up vote 0 down vote accepted

You are doing some odd things.

A quaternion's role is not to specify a direction directly, but to encapsulate a rotation transformation. The math you've shown is tantamount to "apply the same rotations about the opposite axis". Though correct, it causes extra complexity and obfuscation. Vectors are simple and easy to intuit; quaternions are an application of 4-dimensional math in order to simplify rotation transformations, but they're indecipherable as values.

You multiply that quaternion by a speed variable, which means it must be type Vector3 (using this overloaded operator). In physics, speed is generally a directionless quantity rather than a vector. Velocity is the property that combines speed and direction as a vector.

If you stored directions rather than quaternions, you could inspect the values of your directions anywhere and immediately recognize the Euler components. You could add multiple directions together, as a whole or as individual axis components. You could (sometimes) rotate any of them by simple axis-angle transformations. You would only need to pull out the quaternions to accomplish a 3-axis rotation. And if your speed was directionless, you could apply it in any direction simply by multiplying it by a unit vector, like @sftrabbit described.

In case you decide to try to implement any of these suggestions, this is the unit direction opposite to the camera's direction (forward property):

Vector3 backward = Camera.main.transform.forward * -1;
share|improve this answer
    
First of all thank you for explaining this. I really don't know much about physics and everything I do is mostly from trial and error. –  thedeadlybutter Aug 23 '13 at 0:00
    
Darn, hitting enter makes it post. So continuing from there, the reason I used a quaternion was because the way I originally coded it I multiplied the current camera rotation by a Vector3 called speed (as you pointed out). So to get the opposite of that I just made a new quaternion, but I like your solution a lot more. The only issue is this can only work backwards, how would I make it based off the opposite angle I'm pointing? –  thedeadlybutter Aug 23 '13 at 0:03
    
Oh I re-read your comment, I think I understand how to accomplish this now. –  thedeadlybutter Aug 23 '13 at 18:17

Okay fixed the problem - I was incorrectly getting the reverse before. This is the new code using the advice from @sftrabbit.

    Quaternion test = new Quaternion(-transform.rotation.x, -transform.rotation.y, -transform.rotation.z, transform.rotation.w);
    speed = test * speed;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.