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
0
Question by pablo · Nov 29, 2009 at 11:56 PM ·

moving player relative to camera.

I have a character controller and i am moving it with the fpswalker script:

moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

I have made my game to have a camera looking from above, and i want to have the player move the correct way whaever the camera rotation is.

I mean if i press the up key, i want him moving up from the camera's point of view.

Could you help me understand how to change the code above to have it move the way i want?

Thanks!

Comment
Add comment · Show 1
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 BabyFace · Mar 08, 2015 at 06:07 AM 0
Share

Thanks runevision, that helped me a lot!, Great work.

1 Reply

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

Answer by Jaap Kreijkamp · Nov 30, 2009 at 12:29 AM

EDIT bugfixed the normalized.

A (simplified) excerpt from code how I do it (C#):

// we need some axis derived from camera but aligned with floor plane Vector3 forward = cameraTransform.TransformDirection(Vector3.forward); forward.y = 0f; forward = forward.normalized; Vector3 right = new Vector3(forward.z, 0.0f, -forward.x);

float h = joystick.xaxis; float v = joystick.yaxis;

Vector3 walkDirection = (h right + v forward);

The original code did a .normalized on the walkDirection. The reason you possibly want to normalize is to prevent walking faster when going forward + sideways instead of only forwards (given h == v == 1, length of Vector3(h, 0, v) ~= 1.414, not 1). To prevent this you could add the following:

if (walkDirection.sqrMagnitude > 1f) {
    walkDirection = walkDirection.normalized;
}

If you want constant walking speed (so no slow walking when joystick isn't pressed much) replace the 1f with 0f.

Comment
Add comment · Show 3 · 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 pablo · Nov 30, 2009 at 04:11 AM 0
Share

Thanks! That is working! if (grounded) {

     forward = cameram.transform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
     right  = new Vector3(forward.z, 0, -forward.x);
     h = Input.GetAxis("Horizontal");
     v=Input.GetAxis("Vertical");

     moveDirection  = (h * right  + v * forward).normalized ;
     moveDirection *= speed ;
     
     if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
     }
 }

I am using that code in javascript. The only thing is that when i stop pressing a key the player keeps moving for while and then stops. any idea why?

avatar image pablo · Nov 30, 2009 at 04:22 AM 0
Share

Forget it it was the "moveDirection = (h right + v forward).normalized" line, i should not normalize that. Thanks so much!

avatar image runevision ♦♦ · Nov 30, 2009 at 02:13 PM 0
Share

pabloruiz55, if this answer worked for you, please click the check-mark below the answer to mark it as the accepted answer.

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers


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