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 hanni02678 · Feb 11, 2017 at 02:52 PM · c#script.jumpcharacter control

How do i make character JUMP?

Hey guys I've got a request to ask could you help me implement jump into my script ? I tried so many variations and none of them worked. It is in C# and should be written on the line 65 // Y - Up and Down. Thanks for any advice !

using UnityEngine; using System.Collections;

public class PlayerMotor : MonoBehaviour { private CharacterController controller; private Vector3 moveVector;

 private Vector3 moveDir = Vector3.zero;
 private float speed = 5.0f;
 private float verticalVelocity = 0.0f;    
 private float gravity = 12.0f;



 private float animationDuration = 3.0f;
 private float startTime;

 private bool isDead = false;

 //Use this for initialization
 void Start()
 {
     controller = GetComponent<CharacterController>();
     startTime = Time.time;

 }

 //Update is called once per frame
 void Update()
 { 
     if (isDead)
         return;

     if(Time.time - startTime < animationDuration)
     {
         controller.Move(Vector3.forward * speed * Time.deltaTime);
         return;
     }

     moveVector = Vector3.zero;

     if (controller.isGrounded)
     {
         verticalVelocity = -0.5f;
     }
     else
     {
         verticalVelocity -= gravity * Time.deltaTime;
     }

     
     moveVector = Vector3.zero;

     if (controller.isGrounded)
     {
         verticalVelocity = -0.5f;
     }
     else
     {
         verticalVelocity -= gravity * Time.deltaTime;
     }

     // X - Left and Right
     moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
     if(Input.GetMouseButtonDown(0))
     {

         if (Input.mousePosition.x > Screen.width / 2)
             moveVector.x = speed;
         else
             moveVector.x = -speed;
     }

     // Y - Up and Down

     moveVector.y = verticalVelocity; 

     // Z - Forward and Backward
     moveVector.z = speed;

     controller.Move(moveVector * Time.deltaTime);

 }

 public void SetSpeed(float modifier)  
 {
     speed = 5.0f + modifier;
 }


 private void OnControllerColliderHit(ControllerColliderHit hit)
 {
     if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy") 
         Death();
 }

 private void Death()
 {
     isDead = true;
     GetComponent<Score>().OnDeath();
 }

}

Comment
Add comment · Show 3
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 Nomenokes · Feb 11, 2017 at 06:13 PM 0
Share

Do you maybe want to do moveVector.y += verticalVelocity?

avatar image hanni02678 Nomenokes · Feb 13, 2017 at 10:05 PM 0
Share

It hasnt changed anything, do i need to make a make an extra imput or something else because it doesnt react to any buttons. :P

avatar image Nomenokes hanni02678 · Feb 13, 2017 at 10:25 PM 0
Share

hmm... i don't think you have an input for jumping yet. you would do something like your move function, like

 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.SPACE)){
      verticalVelocity=10.0;
 }
 

You may want to switch to using a rigidbody2D ins$$anonymous$$d, they handle stuff like this well.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Pengocat · Feb 14, 2017 at 05:46 AM

The following script is based on your script and the official documentation for CharacterController.Move It handles jumping. Perhaps it can help you.

 using UnityEngine;
 
 [RequireComponent(typeof(Score))]
 public class PlayerMotor : MonoBehaviour
 {
     public float speed = 5.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 12.0f;
 
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
 
     #region Unity Magic Methods
     // Awake is called when the script instance is being loaded
     void Awake()
     {
         controller = GetComponent<CharacterController>();
     }
 
     // Update is called every frame, if the MonoBehaviour is enabled
     void Update()
     {
         UpdateControllerMovement();
     }
 
     // OnControllerColliderHit is called when the controller hits a collider while performing a Move
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         bool isImpactPointMoreThanThis = hit.point.z > transform.position.z + 0.1f;
         if (isImpactPointMoreThanThis && hit.gameObject.CompareTag("Enemy"))
         {
             Destroy(this);
         }
     }
 
     // This function is called when the MonoBehaviour will be destroyed
     void OnDestroy()
     {
         GetComponent<Score>().OnDeath();
     } 
     #endregion
 
     /// <summary>
     /// Move the CharacterController according to player Input and gravity.
     /// Note that Input is only evaluated when the controller is grounded.
     /// </summary>
     public void UpdateControllerMovement()
     {
         EvaluateMoveDirection();
         controller.Move(moveDirection * Time.deltaTime);
     }
 
     private void EvaluateMoveDirection()
     {
         if (controller.isGrounded)
         {
             SetMoveDirectionByInput();
         }
         SetMoveDirectionByGravity();
     }
 
     private void SetMoveDirectionByInput()
     {
         moveDirection = new Vector3(
             Input.GetAxis("Horizontal"), 
             0, 
             Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
 
     private void SetMoveDirectionByGravity()
     {
         moveDirection.y -= gravity * Time.deltaTime;
     }
 }
Comment
Add comment · Show 8 · 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 hanni02678 · Feb 14, 2017 at 03:33 PM 0
Share

<3 Thank you so much it was driving me mad :D, it jumps the only bummer is that it slows down but thank you !

avatar image Pengocat hanni02678 · Feb 14, 2017 at 10:44 PM 0
Share

If you by slowing down are referring to on the way down then you can increase the gravity to make it drop faster.

avatar image hanni02678 · Feb 14, 2017 at 03:34 PM 0
Share

Once again thank you and I am sorry that I am bothering you but am I supposed to run both scripts at once ? If so could you help me figure out how to make it so that player cant jump at the begining, as it is in the original script. Line 25 and 30.

avatar image Nomenokes hanni02678 · Feb 14, 2017 at 07:43 PM 0
Share

Pengocat's script would replace the existing one.

 void Update(){
      tickCount++;
      if(tickCount>60)
           UpdateController$$anonymous$$ovement();
 }
avatar image hanni02678 Nomenokes · Feb 15, 2017 at 03:12 PM 0
Share

Thank you also! Your script was reporting that tickCount was not existing in current context so I used part from pengocat's script and than I spotted That I needed to make some private / public void for tickCount to make it work, so I sticked to his version as it was working . So thank you once again for you help.

avatar image Pengocat hanni02678 · Feb 14, 2017 at 11:34 PM 0
Share

You could just wrap the piece of code into an if with the startdelay counting down each frame.

 using UnityEngine;
 
 [RequireComponent(typeof(Score))]
 public class Player$$anonymous$$otor : $$anonymous$$onoBehaviour
 {
     public float speed = 5.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 12.0f;
     public float startDelay = 3f;
 
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
 
     #region Unity $$anonymous$$agic $$anonymous$$ethods
     // Awake is called when the script instance is being loaded
     void Awake()
     {
         controller = GetComponent<CharacterController>();
     }
 
     // Update is called every frame, if the $$anonymous$$onoBehaviour is enabled
     void Update()
     {
         if (startDelay > 0f)
         {
             $$anonymous$$oveForward();
             startDelay -= Time.deltaTime;
         }
         else
         {
             UpdateController$$anonymous$$ovement();
         }
     }
 
     // OnControllerColliderHit is called when the controller hits a collider while perfor$$anonymous$$g a $$anonymous$$ove
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         bool isImpactPoint$$anonymous$$oreThanThis = hit.point.z > transform.position.z + 0.1f;
         if (isImpactPoint$$anonymous$$oreThanThis && hit.gameObject.CompareTag("Enemy"))
         {
             Destroy(this);
         }
     }
 
     // This function is called when the $$anonymous$$onoBehaviour will be destroyed
     void OnDestroy()
     {
         GetComponent<Score>().OnDeath();
     }
     #endregion
 
     /// <summary>
     /// $$anonymous$$ove the CharacterController according to player Input and gravity.
     /// Note that Input is only evaluated when the controller is grounded.
     /// </summary>
     public void UpdateController$$anonymous$$ovement()
     {
         Evaluate$$anonymous$$oveDirection();
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }
 
     private void Evaluate$$anonymous$$oveDirection()
     {
         if (controller.isGrounded)
         {
             Set$$anonymous$$oveDirectionByInput();
         }
         Set$$anonymous$$oveDirectionByGravity();
     }
 
     private void $$anonymous$$oveForward()
     {
         controller.$$anonymous$$ove(Vector3.forward * speed * Time.deltaTime);
     }
 
     private void Set$$anonymous$$oveDirectionByInput()
     {
         moveDirection = new Vector3(
             Input.GetAxis("Horizontal"),
             0,
             Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
 
     private void Set$$anonymous$$oveDirectionByGravity()
     {
         moveDirection.y -= gravity * Time.deltaTime;
     }
 }
avatar image hanni02678 Pengocat · Feb 15, 2017 at 02:59 PM 0
Share

Thanks ! It works now, i am using my main script and the second one with added delay from the third one, the problem i had with the slowing down while running was caused by 2 speeds on player one from my main script and one from the second script so i have put it to 0 and it works fine now. Once again thanks for help !

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

95 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

Related Questions

i need help jump script c# 1 Answer

Unity GetComponent Script? 1 Answer

Saving Data (please help) 0 Answers

Audio on keydown Won't stop 1 Answer

I create material with script but it does not render right 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