Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by OnayoMoro · Apr 09 at 07:11 PM · movementgravityplatformer3rd person controller3rd person camera

3rd person movement with camera between platforms with gravity

This is my first question on the community forum so I hope I can properly articulate my problem.

I've been trying to make a 3rd person controller that derives the players movement direction from the camera rotation similar to the Brackeys video in this link: - https://www.youtube.com/watch?v=4HpC--2iowE&t=996s&ab_channel=Brackeys

I've tried adapting various 3d person character controllers to allow the player to walk on differently oriented platforms with their own gravity when jumping between other platforms. This has been demonstrated before on youtube by various creators such as Slug Glove. However, I don't believe any of these examples have used a free orbiting camera to direct the players movement. - https://www.youtube.com/watch?v=aZOyZJhreSU&ab_channel=SlugGlove - https://www.youtube.com/watch?v=QSDUA9YpVwQ&t=71s&ab_channel=SlugGlove

I've come close to a solution... My player has no trouble maintaining left and right movement level with the platform they are standing on as the character adapts to the platforms rotation in space. On the other hand, forward and backward movement, can only move directly in the camera's forward transform. This means the character will walk into the ground if the camera looks at the player down from above. This also work for making the player fly if the camera is looking up.

I've been struggling for a solution to keep the player level with the platform when moving forward and backwards. I'm very grateful for any advice on how to solve this problem.

Bellow is currently what my script looks like. At the moment, player forward direction is linked to "cam.transform.forward" which I've tried editing through various methods including using the platforms normal to somehow derive some kind of level forward direction.

 void Update()
 {
     //THINK ABOUT DERRIVING MOVEMENT DIRECTION FROM NORMAL

     Cursor.visible = false;
     //walk
     //grab mouse axis input
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Vertical");

     Debug.Log("Vertical: " + vertical);

     Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

     //LEFT AND RIGHT WORK
     Vector3 screenMovementRight = cam.transform.right;
     Vector3 screenMovementForward = cam.transform.forward;

     if (Physics.Raycast(transform.position, Vector3.down, out hit, raycastDistance))
     {
         dir = cam.transform.forward - hit.normal;
         angle = Vector3.Angle(hit.normal, cam.transform.forward);
         Debug.Log("Angle difference: " + angle);
     }

     Debug.DrawRay(transform.position, screenMovementForward*3);

     //Debug.Log("Camera Forward Transform" + screenMovementForward);

     Vector3 h = screenMovementRight * horizontal;
     Vector3 v = screenMovementForward * vertical;

     Vector3 moveDirectionRL = (h).normalized;
     Vector3 moveDirectionFB = (v).normalized;

 if (GetComponent<GravityTransform>().gravityHost)
     {
         //GRAB GRAVITY PLATFORM OBJECT
         gravHost = GetComponent<GravityTransform>().gravityHost.transform;

         //ROTATE PLAYER TO GRAVHOST LOCAL UP ROTATION
         RotateSelf(gravHost.transform.up);

         //LOOKS FOR GRAVEHOST OBJ WITH DOWNWARD RAYCAST TO PRINT OUT NORMAL
         if (Physics.Raycast(transform.position, Vector3.down, out hit, raycastDistance))
         {
             //Debug.Log(hit.normal);
             //targetDir = targetDir + (hit.normal - targetDir);
         }
     }

 if (direction.magnitude >= .1f)
     {
         // if (Input.GetKey(KeyCode.W))
         //     rb.AddForce(speed * targetDir);
         if (Input.GetKey(KeyCode.W)) 
             RotateMesh(moveDirectionFB);
             //lookDir = Quaternion.LookRotation(moveDirectionFB);
             transform.position += Time.deltaTime * speed * moveDirectionFB; 
             
         if (Input.GetKey(KeyCode.S))
             RotateMesh(moveDirectionFB);
             //lookDir = Quaternion.LookRotation(moveDirectionFB);
             transform.position += Time.deltaTime * speed * moveDirectionFB; 
             
         if (Input.GetKey(KeyCode.A))
             RotateMesh(moveDirectionRL);
             //lookDir = Quaternion.LookRotation(moveDirectionRL);
             transform.position += Time.deltaTime * speed * moveDirectionRL;    

         if (Input.GetKey(KeyCode.D))
             RotateMesh(moveDirectionRL);
             //lookDir = Quaternion.LookRotation(moveDirectionRL);
             transform.position += Time.deltaTime * speed * moveDirectionRL; 

         if (Input.GetKey(KeyCode.Space))
             transform.position += Time.deltaTime * speed * gravHost.transform.up;
         //controller.Move(targetDir.normalized * speed * Time.deltaTime);
     }
 }

 void RotateSelf(Vector3 Direction)
 {
     Vector3 LerpDir = Vector3.Lerp(transform.up, Direction, turnSmoothTime * 2);
     transform.rotation = Quaternion.FromToRotation(transform.up, LerpDir) * transform.rotation;
 }

 void RotateMesh(Vector3 LookRotationDir)
 {
     Quaternion SlerpRot = Quaternion.LookRotation(LookRotationDir, transform.up);
     //float targetAngle = Mathf.Atan2(LookDir.x, LookDir.z) * Mathf.Rad2Deg;
     //transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
     transform.rotation = Quaternion.Slerp(transform.rotation, SlerpRot, 2 * turnSmoothTime);
 }

 void RaycastAngle()
 {
     if (Physics.Raycast(transform.position, Vector3.down, out hit, raycastDistance))
     {
         Quaternion rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

247 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character only falls when button is pushed down 0 Answers

3rd person rotate around sphere. 0 Answers

Question about moving in isometric game 2 Answers

My character moves according to global Vectors 1 Answer

Third person platformer | Player can only jump while moving. Please help. 0 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