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 devgr · Apr 24, 2014 at 01:33 AM · rotationmovementgravity

[fixed] Player movement after changing gravity direction

Context: My goal is to make a first person game where the player can change the direction of gravity in the 6 main directions. Everything with a rigidbody, including the player, is affected by the change of gravity. The player is rotated so they are still standing "up" based on the new gravity direction, and the player can move around like normal (basically, the wall becomes the new floor).

What works: The gravity direction can be changed by the player, and they rotate for the new direction, and mouse look works great.

What doesn't: Player movement when gravity is different. Normally, the direction that you move in is based on the vertical and horizontal input axis, and then adjusted based on the y axis rotation (meaning pressing 'w' makes you walk in the direction in which you are looking). This works correctly for regular gravity, but how do you calculate the right direction to move in when everything is turned sideways or upside down?

How I have it set up: The player is a sphere, and the camera is a child of the sphere. The sphere has a rigidbody, and a script called "Mover.cs" that gets input and adds forces to the sphere to move it. Mover has a reference to the transform of the camera so that moving forward means moving in the direction the camera is facing. It also has a script called "GravityController.cs" that gets input from certain keys and changes the value of physics.gravity. When changing the gravity, it makes the sphere kinematic, rotates it to the new "up", and then makes it not kinematic so physics continue to affect it like normal. The camera, which is a child of the sphere, has "MouseLook.cs" on it which gets the x and y movements of the mouse - this is just the script provided by unity.

Edit: I figured it out, all is working right now. I've changed the code below to reflect that.

 using UnityEngine;
 using System.Collections;
 
 public class Mover : MonoBehaviour {
     public Transform cam;
     public float power = 20f;
 
     void FixedUpdate () {
         Vector3 directionVector = Vector3.zero;
         Vector3 grav = Physics.gravity.normalized;
         Quaternion rotation = Quaternion.identity;
 
         float horiz = Input.GetAxis ("Horizontal");
         float vert = Input.GetAxis ("Vertical");
 
         int g = 0;
         if(grav == Vector3.down){
             directionVector = new Vector3 (horiz, 0f, vert);
             rotation.eulerAngles = new Vector3 (0f, cam.eulerAngles.y, 0f);
             g = 0;
         }
         else if(grav == Vector3.left){
             directionVector = new Vector3 (horiz, 0f, vert);
             rotation.eulerAngles = cam.eulerAngles;
             g = 1;
         }
         // ... continue for other gravity cases
 
         // Basically code from the FPSInputController
         if (directionVector != Vector3.zero) {
             float directionLength = directionVector.magnitude;
             directionVector = directionVector / directionLength;
             directionLength = Mathf.Min(1, directionLength); // 1 or less
             //directionLength = directionLength * directionLength; // square
             directionVector = directionVector * directionLength;
         }
 
         Vector3 usefulVector = rotation * directionVector;
 
         switch(g){
             case 1: usefulVector.x = 0f;
                 break;
             // ... continue for other gravity cases
         }
 
         rigidbody.AddForce (usefulVector * power);
     }
 }

The place where I've marked <<<<< in the code above is where I'm just unsure about. Let me know if there is other information I can provide. Thank you!

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

20 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

Related Questions

How to change the angle my character falls at 1 Answer

Why is Quaternion.FromToRotation() not working properly? 3 Answers

CharacterController.Move uses gravity even though it shouldn't 1 Answer

click to move workig script!! but pls help with rotation!! :( 1 Answer

Character too floaty after jumping and grappling.,Grapple too floaty 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