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 /
avatar image
0
Question by mrblurrrypics · Mar 22, 2021 at 11:25 AM · rotationquaterniongravitylocalrotation

How to control player camera on angled surface/independent of xyz axis?

alt text

I managed to have my player move around the inside surface of a cylinder using gravity but an issue comes up when trying to rotate the camera to look around.


First, there's a script that simulates gravity by pushing the player against the inside walls of the cylinder. This script also keeps the player upright by changing the "up" direction of the player to always be facing the center of the cylinder. I know this keeps my player from looking up so for now I'm just working on getting them to look left and right.

Second, when the player is on the bottom of the cylinder and parallel with the Y-axis I can look left and right without issue because the camera rotates using the x-axis (see circle on the left). However when the player moves around the side of the cylinder the camera is still trying to rotate based on the X-axis even though they are not aligned resulting in the camera not accurately rotating (see the circle on the right), and by the time the player is 90deg around the cylinder cannot rotate at all (thanks to the gravity keeping the player perpendicular to the sides of the cylinder), and at 180deg around the rotation is inverted.


I assume there are two possible solutions that I have not been able to successfully implement: 1. ignore the world xyz axis' and rotate relative to the player's xyz. 2. do some math to figure out the proper angles when you take into account the player's rotation around the cylinder and the angle of the current "left" direction.

The problem with the first solution is I have not been able to successfully rotate the player independent of the world xyz. I tried adding the Space.Self to the Rotate() method but no success. The problem with the second is math scares me and I've managed to avoid Quaternions & Euler angles so far so I'm not even sure how to begin figuring that out.

If anyone has had a similar issue I would greatly appreciate any insight or suggestions on how to figure it out.


Here's my code for the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerController : MonoBehaviour
 {
     InputManager inputActions;
     InputManager.PlayerMovementActions playerMovement;
 
     public GravityAttractor GravityAttractor;
 
     public float moveSpeed = 15;
     public float jumpHeight = 10f;
 
     public float sensitivityX = 1f;
     public float sensitivityY = 1f;
     float mouseX, mouseY;
     Vector2 mouseInput;
 
     private bool isJumping = false;
     private Vector3 moveDir;
     private Vector2 moveInput;
     private Rigidbody rbody;
 
 
     private void Awake()
     {
         inputActions = new InputManager();
         playerMovement = inputActions.PlayerMovement;
         playerMovement.Movement.performed += context => moveInput = context.ReadValue<Vector2>();
         playerMovement.Jump.performed += ctx => Jump();
 
         //playerMovement.MouseX.performed += context => mouseInput = context.ReadValue<Vector2>();
         playerMovement.MouseX.performed += context => mouseInput.x = context.ReadValue<float>();
         playerMovement.MouseY.performed += context => mouseInput.y = context.ReadValue<float>();
 
         rbody = GetComponent<Rigidbody>();
         isJumping = false;
     }
     private void Update()
     {
         moveDir = new Vector3(moveInput.x, 0, moveInput.y).normalized;
         if (rbody.velocity.y == 0)
             isJumping = false;
     }
     private void FixedUpdate()
     {
         rbody.MovePosition(rbody.position + transform.TransformDirection(moveDir) * moveSpeed * Time.deltaTime);
         MouseLook(mouseInput);
     }
     void Jump()
     {
         //use brackeys method of checking for contact with ground
         if(isJumping == false && rbody.velocity.y == 0)
         {
             isJumping = true;
             rbody.velocity = transform.up * jumpHeight;
             Debug.Log("player jumped");
         }
         
     }
     void MouseLook(Vector2 mouseInput)
     {
         mouseX = mouseInput.x * sensitivityX;
         mouseY = mouseInput.y * sensitivityY;
 
         var upTransform = GravityAttractor.transform.position - transform.position;
         Vector3 relativeLook = upTransform - transform.forward;
         Vector3 qLook = transform.forward - transform.position;
 
         transform.Rotate(transform.up * mouseX * Time.deltaTime, Space.Self);
         //transform.Rotate(relativeLook.normalized);
         //transform.rotation = Quaternion.LookRotation(qLook);
         //transform.Rotate(relativeLook.normalized, mouseX);
     }
     private void OnEnable()
     {
         inputActions.Enable();
     }
     private void OnDisable()
     {
         inputActions.Enable();
     }
 }

And my code for the gravity:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GravityAttractor : MonoBehaviour
 {
     public float gravityMultiplier = -10f;
     private float radius;
     public float gravity;
     public float distance;
     private void Awake()
     {
         radius = transform.localScale.x/2;
     }
     public void Attract(Transform body)
     {
         Vector3 centerOfGravity = new Vector3(transform.position.x, transform.position.y, body.position.z);
         distance = Vector3.Distance(centerOfGravity, body.position);
 
         //gravity will match multiplier no matter the radius of cylinder
         gravity = gravityMultiplier * (distance/radius);
 
         Vector3 gravityUp = (centerOfGravity - body.position).normalized;
         Vector3 bodyUp = body.up;
 
         body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
 
         Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityUp) * body.rotation;
         body.rotation = Quaternion.Slerp(body.rotation, targetRotation, 50 * Time.deltaTime);
     }
 }

untitled-artwork.png (126.1 kB)
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 unity_ek98vnTRplGj8Q · Mar 22, 2021 at 05:00 PM 1
Share

Is your camera a child of your player? And is this a third person camera or a first person camera?

avatar image logicandchaos · Mar 23, 2021 at 02:34 PM 0
Share

I think it has to do with how your camera is setup, can you show inspector?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ahsen35813 · Mar 23, 2021 at 04:22 PM

First of all, I'd like to say that this is a really cool idea!

Did you try using local rotation instead of global rotations ( https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html )? If the player orientation is already correct, local rotations might work as long as the camera is parented to the player.

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

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

162 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

Related Questions

Code to rotate around a local axis until local Y is 0? 0 Answers

Why is my object's local x rotation not going past 90 degrees? 1 Answer

Clamped Turret Doesn't Want to Lerp the Other Way 2 Answers

How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer

Complicated Rotation Issue 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