Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ulydev · Jul 31, 2015 at 02:56 PM · unity 5movementcharactercontroller

Character movement relative to both camera and transform

Hello everyone, I'd need some help concerning character movement.

Actually, I have a transform (Super Character Controller) that aligns to the normal of the ground it's standing on. This works pretty well, except I don't know how to implement movement that takes orientation as a factor.

Basically, I would like the player to move relatively to the camera and its own orientation. Let me explain you with these pictures :

alt text

This is the main view. As you can see, the left and right vectors are based on the camera's orientation. It should NOT be the transform's left and right vectors.

Now, the same scene, with a slightly different angle (keep in mind the camera is "supposedly" at the same place) :

alt text

It now makes sense the the forward vector is based on both the player and the camera's orientations. The forward vector should be based on the camera's forward and be perpendicular to the ground the player is standing on (= simply its orientation).

A last side view to understand it better :

alt text

The forward vector can NOT be the player's, as it has to move forward relatively to the camera even if he's not oriented properly (e.g. looking on the left).

I'd also like to handle the case in which the player is walking on a ceiling. In such a case, the movement forward vector should be the backwards vector and vice-versa, as he would be walking towards the camera.

I definitely need some advice concerning this, I've been scratching my head for days yet I still can't manage to find a proper solution.

Thank you in advance for your help.

Comment
Add comment · Show 2
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 tanoshimi · Jul 31, 2015 at 03:02 PM 0
Share

I appreciate your effort with the screenshots, but I'm still not quite understanding your problem. In all the pictures you've shown, it appears to me that the line you've marked as "forward" is indeed the player's forward - i.e. it's the direction they're looking in. How would you like it to change relative to the camera?

avatar image Ulydev · Jul 31, 2015 at 03:32 PM 0
Share

The player doesn't always look in the same direction as the camera : he can move freely, and that's the problem. The forward vector should NOT be the player's forward, but rather the perpendicular of the normal of the ground he's standing on. It hope it makes it clearer, thank you for your help !

In fact, the player's Y rotation doesn't matter at all, think Sonic Adventure for example.

2 Replies

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

Answer by Ulydev · Aug 16, 2015 at 10:36 PM

What's good with Unity Answers, at least, is that it makes you look for an answer yourself.

So I finally found an answer :

float btwAngle = Vector3.Angle (Vector3.up, groundNormal); Quaternion tempAngle = Quaternion.AngleAxis (-btwAngle, cameraRight); move = tempAngle * move;

Comment
Add comment · 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
0

Answer by jabez · Aug 01, 2015 at 10:47 AM

I can think of two things that might work. In your movement script use Camera.main.transform Example :

 void Update() {
         if (Input.GetKey("w"))
           rigidbody.addforce(Camera.main.transform.forward * 20);
         
     }
  if (Input.GetKey("s"))
           rigidbody.addforce(-Camera.main.transform.forward * 20);
         
     }
 
 }

Or get the direction from the player by using

     Vector3 Direction = player.transform.position - Camera.main.transform.position;
  if (Input.GetKey("w")){
               rigidbody.addforce(Direction * 20);
             
         }






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 Ulydev · Aug 01, 2015 at 02:02 PM 0
Share

Thank you for your interest in this problem, unfortunately that won't work. The vector is not perpendicular to the ground's normal, and is based on the camera position relative to the player... If I place the camera above the player and look at it, the character will just move towards the ground.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Character controller movement 1 Answer

Xbox Controller & input manager? 1 Answer

Unity 2017.0.3 Mouse Look Script With Clamp!! 0 Answers

Help with my Character Controller 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