Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SirWaffleGamin · Nov 10, 2015 at 05:31 PM · c#physicsscript.gravity

How to make a player instantly reach to a change in gravity?

I am making a game where the player can change the position of gravity. Objects instantly react to the change, but the player doesn't. The player only reacts to gravity going up after the player has jumped, and only reacts to gravity going down after an uncertain amount of time. I want to make it so that as soon as the gravity changes, Here is the code on gravity switching:

     using UnityEngine;
     using System.Collections;
 
     public class ChangeGravity : MonoBehaviour {
 
  // Use this for initialization
  void Start () {
      Physics.gravity = new Vector3(0, -1, 0);
  }
  
  // Update is called once per frame
  void Update () {
  
      if(Input.GetKeyUp(KeyCode.UpArrow)) {
          Physics.gravity = Vector3.up * 5;
      }
      else if(Input.GetKeyUp(KeyCode.DownArrow)) {
          Physics.gravity = Vector3.down * 5;
      }
   }
  }

I have a video as an example but I will add it as a YouTube link if you need it.

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 SirWaffleGamin · Nov 10, 2015 at 06:22 PM 0
Share

I accidentally didn't finish what I wanted to say. I want to make is so that as soon as the gravity changes, the player instantly reacts to the changes in gravity.

2 Replies

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

Answer by SirWaffleGamin · Nov 19, 2015 at 07:02 PM

I found out the answer. If you want to make it so that the player automatically moves when there is a change in gravity, all you have to do set the m_StickToGrondForce = 0 in the Update method.

Like this:

 private void Update()
 {
      GetCommponent<FirstPersonController> ().m_StickyToGroundForce = 0;

      // This is where you can do everything else
 }


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 jonofarc · Nov 10, 2015 at 10:02 PM

@SirWaffleGamin

The problem is that the FPSController that you are using reacts diferent than a normal rigid body so what you can do its:

first we modify the FirstPersonController script on line 18 we make m_StickToGroundForce a public variable instead of private

[SerializeField] public float m_StickToGroundForce

instead of

[SerializeField] privatefloat m_StickToGroundForce

we do the same with m_MoveDir on line 35

public Vector3 m_MoveDir = Vector3.zero;

instead of

private Vector3 m_MoveDir = Vector3.zero;

once done we can acces those variable from an outside script like yours then you just need to add some minor changes


     using UnityEngine;
     using System.Collections;
     // we use firstPerson controller from standar assets
     using UnityStandardAssets.Characters.FirstPerson;

     public class ChangeGravity : MonoBehaviour {

 //we create the variable so we can acces FirstPersonController script 
 FirstPersonController FPScontroller;
 // Use this for initialization
 void Start () {
     Physics.gravity = new Vector3(0, -1, 0);
     FPScontroller = GetComponent<FirstPersonController>();
 }
 
 // Update is called once per frame
 void Update () {
     /
     if(Input.GetKeyUp(KeyCode.UpArrow)) {
         Physics.gravity = Vector3.up * 3;
         //we stop all movement from FPSController
         FPScontroller.m_MoveDir=Vector3.zero;

         // we removeS stick to ground force wich was the force keeping us in ground untill we jumped
         FPScontroller.m_StickToGroundForce=0;
         
     }
     else if(Input.GetKeyUp(KeyCode.DownArrow)) {
         Physics.gravity = Vector3.down * 3;

         //we restore m_StickToGroundForce so we can walk on ground again
         FPScontroller.m_StickToGroundForce=10;
         //we stop all movement from FPSController
         FPScontroller.m_MoveDir=Vector3.zero;
     }
 
 }



     }


this script goes into de FPSController prefab

Comment
Add comment · Show 5 · 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 SirWaffleGamin · Nov 11, 2015 at 06:22 PM 0
Share

Thank you very much!

avatar image SirWaffleGamin · Nov 12, 2015 at 06:51 PM 0
Share

I added what you told me to my code, but it doesn't work. I'm still having the same problem. Is it because the prefab has a setting for stick to ground force and it is always changing to that value?

Also this is my code:

what.jpg (34.6 kB)
avatar image SirWaffleGamin SirWaffleGamin · Nov 12, 2015 at 06:52 PM 0
Share
         private void InputHandle()
         {
             if (Input.Get$$anonymous$$ouseButtonUp($$anonymous$$eyCode.$$anonymous$$ouse0) 
             {
                 GetComponent<FirstPersonController> ().m_$$anonymous$$oveDir = Vector3.zero;
                 GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
             }
             else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad8)) 
             {
                 Physics.gravity = Vector3.up * 10;
                 GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
 
                 /*Quaternion deltaRotation = Quaternion.Euler(AngleVelocity * Time.deltaTime);
             m_RigidBody.$$anonymous$$oveRotation(m_RigidBody.rotation * deltaRotation);*/
             }
             else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad4)) 
             {
                 Physics.gravity = Vector3.left * 10;
                 GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
             }
             else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad5)) 
             {
                 Physics.gravity = Vector3.down * 10;
                 GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
             }
             else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Escape)) 
             {
                 Application.Quit();
             }
         }
avatar image jonofarc SirWaffleGamin · Nov 12, 2015 at 10:00 PM 0
Share

This works for me I made some $$anonymous$$or changes to your code and it works for me

      using UnityEngine;
      using System.Collections;
      using UnityStandardAssets.Characters.FirstPerson;
      
      public class gravityuser : $$anonymous$$onoBehaviour {
      
          // Use this for initialization
          void Start () {
          
          }
          
          // Update is called once per frame
          void Update () {
      
              if (Input.Get$$anonymous$$ouseButtonUp(0) )
              { 
                  Physics.gravity = Vector3.up * 10;
                  GetComponent<FirstPersonController> ().m_$$anonymous$$oveDir = Vector3.zero;
                  GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
              }
               if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad8)) 
              {
                  Physics.gravity = Vector3.up * 10;
                  GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
         
                  /*Quaternion deltaRotation = Quaternion.Euler(AngleVelocity * Time.deltaTime);
          m_RigidBody.$$anonymous$$oveRotation(m_RigidBody.rotation * deltaRotation);*/
              }
               if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad4)) 
              {
                  Physics.gravity = Vector3.left * 10;
                  GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
              }
               if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad5)) 
              {
                  Physics.gravity = Vector3.down * 10;
                  GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
              }
              if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad6)) 
              {
                  Physics.gravity = Vector3.right * 10;
                  GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
              }
               if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Escape)) 
              {
                  Application.Quit();
              }

          }



      }
Show more comments

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

Adding Gravity to a game object to make a black hole sucking effect. 1 Answer

How can I bind this script into the state of my camera? 2 Answers

WheelColliders Slip without end (with planet gravity, custom terrain mesh) 1 Answer

I need to add a jump function and a gravity function to this script, but I am stuck. 0 Answers

Object refuses to collide 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