Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Captain_Dando · Apr 29, 2013 at 11:49 AM · movementaxis

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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        
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lelis718 · May 27, 2019 at 04:15 AM 0
Share

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.

avatar image
1

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

 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Dando · Apr 29, 2013 at 01:47 PM 0
Share

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

avatar image delVhar · Apr 29, 2013 at 11:07 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the player keep pointing in a specific direction after moving? 0 Answers

Constrain axis of isometric camera ("vertical" only) 0 Answers

Clamping movement range on X-axis 0 Answers

Rotating a player object without rotating the axis 0 Answers

Restricting axis of movement not working at all! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges