x


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.

more ▼

asked Apr 29, 2013 at 11:49 AM

Captain_Dando gravatar image

Captain_Dando
229 71 64 75

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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        
}
more ▼

answered Apr 29, 2013 at 03:02 PM

Captain_Dando gravatar image

Captain_Dando
229 71 64 75

(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Apr 29, 2013 at 01:26 PM

delVhar gravatar image

delVhar
31 1 2 3

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

Apr 29, 2013 at 01:47 PM Captain_Dando

Oh whoops, thats right. Instead 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.SimpleMove(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.

Apr 29, 2013 at 11:07 PM delVhar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2525
x543

asked: Apr 29, 2013 at 11:49 AM

Seen: 825 times

Last Updated: Apr 29, 2013 at 11:07 PM