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 Monkeydl_1929 · Mar 28, 2017 at 10:57 AM · physicscontrollerjumpjumping

Not able to jump when running!Where did I go wrong?

I wrote a character controller script on my own but I can't seem to jump forward when I am running I can jump Front when I walk but I can't do it if I run.. : ( can someone help me, please?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     public Transform RaycastPt;
 
     [Range(0,40)]
     public float speedFb;
     [Range(0, 20)]
     public float speedLr;
 
     private bool isGrounded;
     private bool isStill;
 
     private Vector3 moveAmount;
     private Vector3 jumpMovement;
 
     private float oldSpeedFb;
     private float oldSpeedLr;
     private Vector3 oldMoveAmount;
 
     public float jumpSpeed = 100f;
     private float velocityY = 0;
     private float oldJumpSpeed;
 
     private float dirFb;
     private float dirLr;
     float jumpMx;
     float jumpMy;
 
     public Rigidbody rb; 
 
     // Use this for initialization
     void Start () {
         Rigidbody rb = GetComponent<Rigidbody>();
         oldSpeedFb = speedFb;
         oldJumpSpeed = jumpSpeed;
     }
     
     // Update is called once per frame
     void Update () {
 
 
     }
 
     void FixedUpdate()
     {
         CheckIsGrounded();
 
         DirBefJump();
 
         Movement();
 
         Jump();
        
     }
 
 
     void CheckIsGrounded()
     {
         RaycastHit hitInfo;
         float maxDistance = 1f;
 
         if (Physics.Raycast(RaycastPt.position, Vector3.down, out hitInfo, maxDistance))
         {
             //print("Found an object - distance: " + hitInfo.distance);
             isGrounded = true;
             //print("true");
         }
         else
         {
             isGrounded = false;
             //print("false");
         }
         
         
     }
 
     void Movement()
     {
         if (isGrounded)
         {
             if (Input.GetKey(KeyCode.LeftShift))
             {
                 speedFb = 3f * oldSpeedFb;
                 jumpSpeed = 1.5f * oldJumpSpeed;
             }
             else
             {
                 speedFb = oldSpeedFb;
                 jumpSpeed = oldJumpSpeed;
             }
             
 
             Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
 
             if (input == Vector3.zero)
             {
                 isStill = true;
             }
             else
             {
                 isStill = false;
             }
 
             Vector3 direction = input.normalized;
 
             Vector3 moveAmount = new Vector3(direction.x * speedLr * Time.deltaTime, 0, direction.z * speedFb * Time.deltaTime);
 
             transform.Translate(moveAmount);
 
         }
 
     }
 
     void Jump()
     {
         if(isStill == true)
         {
             jumpMovement = Vector3.up;
         }
         if (Input.GetKeyDown(KeyCode.Space))
         {
             print("SpacePressed");
             if (isGrounded)
             {
                 velocityY = 1;
                 rb.AddForce(jumpMovement * jumpSpeed, ForceMode.VelocityChange);
                 print("forceAdded: " + new Vector3(jumpMovement.x, velocityY * jumpSpeed, jumpMovement.y));
                 
                 //transform.Translate(oldMoveAmount); 
             }
         }
         
     }
 
     void DirBefJump()
     {
         if (Input.GetKey(KeyCode.W))
         {
             dirFb = 1; 
         }
         else if (Input.GetKey(KeyCode.S))
         {
             dirFb = -1;
         }
         else
         {
             dirFb = 0f;
         }
 
         if (Input.GetKey(KeyCode.D))
         {
             dirLr = 1;
         }
         else if (Input.GetKey(KeyCode.A))
         {
             dirLr = -1;
         }
         else
         {
             dirLr = 0f;
         }
 
         float dirMag = Mathf.Sqrt(Mathf.Pow(dirLr + dirFb, 2) - 2 * dirLr * dirFb);
 
         float jumpMx = dirLr / dirMag;
         float jumpMy = dirFb / dirMag;
 
         jumpMovement = new Vector3(jumpMx, velocityY, jumpMy);
     }
 
 }
 
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 F-N · Mar 28, 2017 at 01:33 PM 0
Share

Try narrowing your problem down before posting it plz. Seeing an unknown script of 175 lines is quite demotivating. Finding in what piece of your script the problem may be, will help you in solving the problem as well!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dvandamme · Mar 29, 2017 at 04:02 AM

swap all your jump code for whats below, and remove the ground checking function and DirBefJump for use these 2 functions. use a rigidbody and a capsule collider (or other appropriate shape collider)

you must have a good reason for not using the standard first person controller or rigid body controller, so these might help. if you dont have a good reason, use them instead of making your own

i think you're trying to make the jump continue in its initial trajectory and not allow player input when in the air, in which case you check isGrounded when doing the keypress's

public float jumpHeight = 5;

 void Jump()
 {
       if (Input.GetKeyDown(KeyCode.Space))
       {
             if (isGrounded)
             {
                 isGrounded = false;
                 jump = jumpHeight;
                 GetComponent<Rigidbody>().AddForce(0, jump, 0, ForceMode.VelocityChange);
             }
       }
 }


and

  void OnCollisionEnter(Collision other)
     {
         isGrounded = true;
     }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with jumping/ landing 0 Answers

How do i achieve a rounded jump arc? 1 Answer

jumping and landing not very responsive 1 Answer

how can I make the character jump more by holding the jump button in this script? 1 Answer

Unity physics going against a wall and trying to jump fails while still running at it 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