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 /
avatar image
0
Question by csgeorge · Feb 03, 2017 at 12:35 AM · c#movementrigidbodyinputscene

Movement Script Only Works in One Scene

This is the movement script I have attached to my player character prefab. In my test scene it works perfectly fine, but if I create another instance of the prefab in any other scene, the camera rotation still works, but I cannot move the character or jump at all. How can that be?

 public class Move : MonoBehaviour 
 {
     public float speed;
     public float rotationSpeed;
     public float jumpHght;
     public float grav;
     bool jumping;
     bool moving;
     public AudioClip jumpSound;
     public AudioClip landSound;
     private Rigidbody rb;
 
     //Sets variables.
     void Start()
     {
         Physics.gravity = new Vector3(0, grav, 0);
         rb = GetComponent<Rigidbody>();
     }
 
     void Update() 
     {
         //Jumps, if MagBot is not already in air.
         if(Input.GetButtonDown("Jump") && jumping == false)
         {
             rb.AddForce(new Vector3(0, jumpHght, 0), ForceMode.Impulse);
             GetComponent<AudioSource>().PlayOneShot(jumpSound);
         }
     }
 
     //Determines when MagBot is not in air.
     void OnCollisionStay(Collision collision)
     {
         jumping = false;
     }
 
     //Determines when MagBot is in air.
     void OnCollisionExit(Collision collision)
     {
         if(!jumping)
         {
             jumping = true;
         }
     }
 
     //Plays collision sound effects after landing from jump.
     void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "Envir") 
         {
             GetComponent<AudioSource>().PlayOneShot(landSound);
         }
     }
 
     void FixedUpdate() 
     {
         //Movement variables
         float v = Input.GetAxisRaw("Vertical") * speed;
         float h = Input.GetAxisRaw("Horizontal") * speed;
         float r = Input.GetAxisRaw("Mouse X") * rotationSpeed;
         v *= Time.deltaTime;
         h *= Time.deltaTime;
         r *= Time.deltaTime;
 
         //Rotates MagBot/camera based on mouse movement.
         transform.Rotate(0, r, 0);
 
         //Moves MagBot based on WASD/Left analog stick.
         rb.AddRelativeForce(0,0,-v, ForceMode.Impulse);
         rb.AddRelativeForce(h,0,0, ForceMode.Impulse);
     }
 }
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 rh_galaxy · Apr 24, 2020 at 01:55 PM 0
Share

This doesn't answer your question, but I think you should multiply by Time.fixedDeltaTime in FixedUpdate() to get frame rate independent physics.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by csgeorge · Feb 03, 2017 at 09:49 AM

Update: I've determined that this is somehow connected to the character's animator component: when it is on, the AddRelativeForce does not work. When the animator is unchecked, it works properly. This is however, still different from my test scene, in which the AddRelativeForce and animator both work together.

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 moqam · Apr 24, 2020 at 06:00 AM 0
Share

how did you uncheck the animator? my animations won't load and joystick won't work :(

avatar image
0

Answer by FlyingHighUp · Feb 03, 2017 at 04:59 AM

It looks like things are okay, your issue is likely caused by something else holding your player still.

Unless... Does your rigidbody have constraints enabled on it? E.g. freezing position/rotation movements. Is the Time.deltaTime = 0? (e.g. if you set the timescale to 0 in your new scene) Are your variables set to 0? e.g. jumpHght, speed etc. Perhaps you didn't apply changes to the prefab?

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 csgeorge · Feb 03, 2017 at 08:51 AM 0
Share

The rigidbody has constraints freezing rotation, but that is as it should be, and is the same in the scene where it does work. Time is as it should be- everything except the AddRelativeForce is working correctly. All the variables are set to the proper numbers.

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

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

Making a bubble level (not a game but work tool) 1 Answer

Smooth dashing issues 0 Answers

Making RB movement smoother/curvy. C#. 1 Answer

rigidbody.Addforce the force doesnt apply? 1 Answer

2D Top-Down Movement: Transform.up and right don't change 2 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