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 pennzo · Mar 01, 2021 at 12:22 PM · rigidbodyvelocity

I made a rocket! Help!

Hey! Im pretty new to scriping. This is what my Movement script looks like. I dont have rotation on the character yet, But i managed to fix the isGrounded problem i had. The problem i now i have is that when i jump and press left shit i accelerate on the y axis.

Im guessing its because iv added rb.velocity.y in moveDirection and the velocity get multiplied in moveDir. But i have no clue how to fix it

Any help would be appreciated :)

public class PlayerMovement : MonoBehaviour {

 //VARIABLES
 
   //MOVEMENT//SPEED
 [SerializeField]  private float moveSpeed;
 [SerializeField]  private float walkSpeed;
 [SerializeField]  public float runSpeed;
 [SerializeField]  public float rot;
 [SerializeField]  public float rotSpeed;
 
   //GRAVITY
 [SerializeField] private float jumpHeight;
 [SerializeField] private float gravity;
 public bool isGrounded = true;
 //    [SerializeField] private float groundCheckDistance; 
 //[SerializeField] private LayerMask groundMask; 

// Variable for Layer for groundcheck

 private Vector3 moveDirection;
 private Vector3 velocity;
 private Animator anim;
 Rigidbody rb;
 
 
 
 private CharacterController controller; 
 
 private void Start()
 {
     //Debug.Log("Start");
     controller = GetComponent<CharacterController>();
     anim = GetComponent<Animator>();
     rb = GetComponent<Rigidbody>();
         }
         
 private void Update()
 {        
                      
     //Debug.Log("test");
     Move();

 }
     
 //MOVEMENT
 private    void Move()
 {       
 
                 
         float moveZ = Input.GetAxisRaw("Vertical");
         float ad = Input.GetAxis("Horizontal");
                
             moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
         
         
         if(isGrounded)  
         {
                                                             
              
             if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) 
             
             {
                 
                 //Walk
                 Debug.Log("Walk");
                 walk();
             }    
             else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
             {
                 
                 anim.SetFloat("JumpAnim", 0); //Animation stop
                 //Run
                 run();
                 Debug.Log("run");
             }
             else if(moveDirection == Vector3.zero)
             {
                 //Ìdle
                 anim.SetFloat("JumpAnim", 0);
                 idle();
                 
             }    

             if (Input.GetButtonDown("Jump") && isGrounded)
              {
                  //Animation stop
                  //Jump
                  jump();
                  
              }

     
          
         }
             
         rb.velocity = moveDirection * moveSpeed;
     
 }
 
 private    void idle()
 {
     Debug.Log("Idle");
     float MMY = Input.GetAxis("Vertical");
     anim.SetFloat("InputY",MMY );  //
 }    
 private    void walk() /////WALK 
     {
         moveSpeed = walkSpeed;
         anim.SetFloat("run", 0);
         Debug.Log("walk");
     float MMX = Input.GetAxis("Horizontal");
     float MMY = Input.GetAxis("Vertical");
     
     
     anim.SetFloat("InputY",MMY);
     anim.SetFloat("InputX",MMX);
 }

 private    void run() ///// RUN
 {
     
         moveSpeed = runSpeed;
         anim.SetFloat("run", 1);
         float MMX = Input.GetAxis("Horizontal");
         float MMY = Input.GetAxis("Vertical");
         anim.SetFloat("InputY",MMY);
         anim.SetFloat("InputX",MMX);

 }
 private void jump()
 {
     anim.SetFloat("JumpAnim", 1);
     Debug.Log("jump");
      //rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);

      rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
     // rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
     //velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
     

 }
  
  void OnCollisionEnter(Collision theCollision)
  {
      if (theCollision.gameObject.tag == "Plane")
      {
          isGrounded = true;
          print("grounded");
                  }
  }
 
 
  void OnCollisionExit(Collision theCollision)
  {
      if (theCollision.gameObject.tag == "Plane");
      {
          isGrounded = false;
           print("not grounded");
      }
  }

 

 
 

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pennzo · Mar 01, 2021 at 03:12 PM

Thank you, So much! This is exactly what i wanted to do! It does make me feel a little stupid tho. I was trying to do what you did there, but my knowledge in c# is very limit! Thank you!

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 pauldarius98 · Mar 01, 2021 at 12:52 PM

The problem lies at line 77 because you multiply the direction with the movement speed (which is runSpeed if you press shift). You can't press shift if you are not grounded but if you run and then jump, the movement speed will remain the one from running and thus you will jump higher. A simple solution would be to modify only the x and z axis of the velocity and let the AddForce and gravity to take care of the vertical movement. Try the following code:

  private Vector3 moveDirection;
  private Vector3 velocity;
  private Animator anim;
  Rigidbody rb;
  
  
  
  private CharacterController controller; 
  
  private void Start()
  {
      //Debug.Log("Start");
      controller = GetComponent<CharacterController>();
      anim = GetComponent<Animator>();
      rb = GetComponent<Rigidbody>();
          }
          
  private void Update()
  {        
                       
      //Debug.Log("test");
      Move();
  }
      
  //MOVEMENT
  private    void Move()
  {       
  
                  
          float moveZ = Input.GetAxisRaw("Vertical");
          float ad = Input.GetAxis("Horizontal");
                 
              moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
          
          
          if(isGrounded)  
          {
                                                              
               
              if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) 
              
              {
                  
                  //Walk
                  Debug.Log("Walk");
                  walk();
              }    
              else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
              {
                  
                  anim.SetFloat("JumpAnim", 0); //Animation stop
                  //Run
                  run();
                  Debug.Log("run");
              }
              else if(moveDirection == Vector3.zero && rb.velocity.y == 0)
              {
                  //Ìdle
                  anim.SetFloat("JumpAnim", 0);
                  idle();
                  
              }    
              if (Input.GetButtonDown("Jump") && isGrounded)
               {
                   //Animation stop
                   //Jump
                   jump();
                   
               }
      
           
          }
              
          rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);
      
  }
  
  private    void idle()
  {
      Debug.Log("Idle");
      float MMY = Input.GetAxis("Vertical");
      anim.SetFloat("InputY",MMY );  //
  }    
  private    void walk() /////WALK 
      {
          moveSpeed = walkSpeed;
          anim.SetFloat("run", 0);
          Debug.Log("walk");
      float MMX = Input.GetAxis("Horizontal");
      float MMY = Input.GetAxis("Vertical");
      
      
      anim.SetFloat("InputY",MMY);
      anim.SetFloat("InputX",MMX);
  }
  private    void run() ///// RUN
  {
      
          moveSpeed = runSpeed;
          anim.SetFloat("run", 1);
          float MMX = Input.GetAxis("Horizontal");
          float MMY = Input.GetAxis("Vertical");
          anim.SetFloat("InputY",MMY);
          anim.SetFloat("InputX",MMX);
  }
  private void jump()
  {
      anim.SetFloat("JumpAnim", 1);
      Debug.Log("jump");
       //rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
       rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
      // rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
      //velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
      
  }
   
   void OnCollisionEnter(Collision theCollision)
   {
       if (theCollision.gameObject.tag == "Plane")
       {
           isGrounded = true;
           print("grounded");
                   }
   }
  
  
   void OnCollisionExit(Collision theCollision)
   {
       if (theCollision.gameObject.tag == "Plane");
       {
           isGrounded = false;
            print("not grounded");
       }
   }
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

160 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

Related Questions

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

instantiated objects have different speed 1 Answer

Get velocity of point offset from center of rigidbody 1 Answer

Limiting the top speed of a rigidbody in 3d space? 1 Answer

Multiplying something.forward using a variable doesn't actually multiply, but multiplying by a number does. 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