- Home /
How can I move a controller in relation to the camera's axis?
I have a simple script to move my character, as seen below;
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector3 inputVector = new Vector3(x ,0 ,y);
inputVector *= moveSpeed;
controller.SimpleMove(inputVector);
It works just fine for moving the character specifically along the x and z axis. my problem is that I would like to have the character move according to the rotation of the camera, rather than the orientation of the world. for example, at the moment, using simplemove with the "a" key held down would give me a vector of (1,0,0) to move on, however, that moves me straight toward the x axis, where I need it to move me toward the camera's x axis.
Answer by Captain_Dando · Apr 29, 2013 at 03:02 PM
I have found a semi functional solution to this problem. using the method TransformDirection I was able to translate the orientation of the camera to world space. Unfortunately, the character orientates himself strangely while moving. anyways, here it is
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector3 inputVector = mainCamera.transform.TransformDirection(x, 0, z);
inputVector *= moveSpeed;
controller.SimpleMove(inputVector);
This part will help you orient your character, though you'll get the same problem as me
if (inputVector != Vector3.zero){
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputVector), Time.deltaTime * 20); // What this means is that it is rotating FROM the first parameter to the second at the pace of the third parameter
}
Thanks!!
Your code is good but let me fix a few items:
First: you set Vertical axis on 'y' variable and transformed a unknow 'z' variable so, you must change the 'y' variable to 'z';
Second: after the 'transformDirection' you must set the y of the inputVector to Zero, otherwise it will consider the axis for your transform.rotation.
Answer by delVhar · Apr 29, 2013 at 01:26 PM
You could try multiplying x by camera.transform.right and y by camera.transform.forward, which should give you the relative axis of the camera. Depending on whether you want the z or y axis for the y movement, you may want to use transform.forward instead.
ie:
float x = Input.GetAxis("Horizontal");
x *= camera.main.transform.right;
float y = Input.GetAxis("Vertical");
y *= camera.main.transform.up;
Vector3 inputVector = new Vector3(x ,0 ,y);
inputVector *= moveSpeed;
controller.SimpleMove(inputVector);
This looks promising, but I've recieved an error after trying it out.
Cannot implicitly convert type `UnityEngine.Vector3' to `float'
my code reads
x *= mainCamera.transform.right
at the error, "mainCamera" being the name of my cached camera I've tried putting (float) before the maincamera part but it doesn't help
Oh whoops, thats right. Ins$$anonymous$$d you would need to get the vector, multiply by the directions and add. so:
float x = Input.GetAxis("Horizontal");
Vector3 newX = Camera.main.transform.right * x;
float y = Input.GetAxis("Vertical");
Vector3 newY = Camera.main.transform.up * y;
Vector3 inputVector = newX + newY;
inputVector *= moveSpeed;
controller.Simple$$anonymous$$ove(inputVector);
Camera.main is a reference to the main Camera (uppercase C, which I missed in my answer) I think this is effectively the same as what you came up with, and might not do anything about the orientation issue.