Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 RajTheWolf · Nov 21, 2020 at 07:40 PM · velocityplayer movementtranslate

My player velocity changing by itself?

So i am trying to make this geometry type game and just finished making a start menu not done though. The scene loads fine and everything works but when you start the game the player don't jump when you click but when you open the player's inspector everything starts working again. I have the script jump whenever you click and it used to work fine but now I keep have pause the game go to the inspector and then its normal. Can someone help also I can describe if someone is willing to help. my scene manager script is fine but I think there is something wrong with the player script: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour { public Rigidbody rb; public GameObject Player;

 private float time = 0.0f;
 private bool isMoving = false;
 private bool isJumpPressed = false;
 public float Boost;
 public float L;
 public float F;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 void Update()
 {
     
   

     isJumpPressed = Input.GetMouseButtonDown(0);
 }

 void FixedUpdate()
 {
     if (isJumpPressed)
     {
         
        
         // the cube is going to move upwards in 10 units per second
         rb.velocity = new Vector3(0, Boost, 0);
         isMoving = true;

         Debug.Log("jump");
     }

     if (isMoving)
     {
         // when the cube has moved for 10 seconds, report its position
         time = time + Time.fixedDeltaTime;
         if (time > 10.0f)
         {
             Debug.Log(gameObject.transform.position.y + " : " + time);
             time = 0.0f;
         }
     }
 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Obstacle")
     {
         Destroy(Player);
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Nov 21, 2020 at 08:32 PM

Well, one thing that stands out is that FixedUpdate is called by default 50 times per second unless you change the fixed time step in the time settings. So if you are getting 100 FPS for example, there will be 2 Update calls per 1 FixedUpdate call. So it is possible for you to press the button and have multiple frames pass before FixedUpdate is called and thus ‘isJumpPressed‘ will equate to false because that state resets on every update.


I would suggest that you make sure that the jump is going to happen by perhaps having another Boolean or an enum:


 private enum JumpState
 {
     Jumping,
     WaitingToJump,
     Grounded
 }
 private JumpState state;
 
 private void Start()
 {
     state = JumpState.Grounded;
 }
 
 private void Update()
 {
     if(Input.GetMouseButtonDown(0) && state == JumpState.Grounded)
         state = JumpState.WaitingToJump;
 }
 private void FixedUpdate()
 {
     if(state == JumpState.WaitingToJump)
     {
         rb.velocity = new Vector3(0, Boost, 0);
         state = JumpState.Jumping;
     }
     //when it becomes grounded again, state should become JumpState.Grounded again
 }

I’ve left the grounding part up to you (maybe you want to check for a collision with the ground), but this should work.

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

144 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

Related Questions

Checking when the player is stopped, without velocity? 2 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How to push object along Z axis with Ray on touch? 1 Answer

Rigidboy.velocity with integer? 1 Answer

RigidBody clips into corners 3 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