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 Muska · Jun 17, 2016 at 02:52 PM · c#navmeshnavmeshagentjumpinglines

Help with good coding practices and player Nav Mesh Agent Jumps. Thanks.

Hello guys, I'm introducing myself in unity and c# and I'm facing my very first ..."complicated situation".

Let me explain what I'm trying to achieve; as a newbie the first thing I'm doing is trying to (little by little) understand one language at least so I started with C# and sticking to it for long time unless I need something very specific and impossible in C#.. and I don't even know if that could eventually happen.

So here is my first question, in the script that I'm going to paste down below, please tell me if I can avoid repeating too many lines at the end, thanks :D.

Then, the main thing that leads me to be here asking is the next situation, with a terrain as ground, a cube based player which moves along the terrain using a Nav Mesh Agent and a click to move script, I want this player to be able to perform his jump following the direction he was already moving before the jump.

Right now, my player just stop moving at all and jumps 100% vertically, just because I told him to do it as I don't know how to set x, and z) values of the Vector3 in a way that follows his previous movement direction.

Here is the script which I'm working with, any help would be really appreciated, thanks in advance and love from Spain.

using UnityEngine; using System.Collections;

public class Jump : MonoBehaviour { private bool onGround; private float jumpPressure; private float minJump; private float maxJumpPressure; private Rigidbody rb; NavMeshAgent navAgent;

 void Start ()
 {
     onGround = true;
     jumpPressure = 0f;
     minJump = 0.5f;
     maxJumpPressure = 2f;
     rb = GetComponent<Rigidbody>();
     navAgent = GetComponent<NavMeshAgent>();
 }
 
 void Update ()
 {
     if(onGround)
     {
         //holding jump button//
         if (Input.GetButton("Jump"))
         {
             if(jumpPressure < maxJumpPressure)
             {
                 jumpPressure += Time.deltaTime * 12f;
             }
             else
             {
                 jumpPressure = maxJumpPressure;
             }
         }

         //not holding jump button anymore//
         else
         {
             //jump//
             if(jumpPressure > 0f)
             {
                 onGround = false;
                 jumpPressure = jumpPressure + minJump;
                 rb.isKinematic = false;
                 rb.useGravity = true;
                 navAgent.enabled = false;
                 rb.AddForce(new Vector3(0f, 
                                         jumpPressure,
                                         0f),
                                         ForceMode.Impulse);
                 rb.AddRelativeForce(new Vector3(0f,
                                                 jumpPressure,
                                                 0f),
                                                 ForceMode.Impulse);
                 jumpPressure = 0f;
                 //rb.velocity = new Vector3(0f, jumpPressure, 0f);
                 //onGround = false;

             }
         }
     }
 }

 void OnCollisionEnter(Collision other)
 {
     if(other.gameObject.CompareTag("Ground"))
     {
         onGround = true;
         navAgent.enabled = true;
         rb.isKinematic = false;
         rb.useGravity = true;
         print ("On ground");
     }
     if (FindObjectOfType<TerrainCollider>())
     {
         onGround = true;
         navAgent.enabled = true;
         rb.isKinematic = false;
         rb.useGravity = true;
         print("On ground");
     }
 }

}

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 Muska · Jun 17, 2016 at 05:45 PM

I had a syntax error the first time I tried but now at the end both "if" statements are together with the or thing "||"

But still need help with the main thing... :(

I think that the point here is to calculate these X and Z values using the position I clicked to move and the position the player currently is, so we could determine if each one is a positive or negative movement in it's axis in order to aim that given direction, and then make the value closer or further to 2 depending of the "jumpPressure" which is set to a maximum of 2 units. I could paste the script I'm using to move my player if you guys request.

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

167 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

Related Questions

Creating prefab through Zenject Factory makes navMeshAgent act strange 0 Answers

NavMeshAgent dont find the right way on runtime build NavMesh 0 Answers

Navmesh Agent - The Y Next Position & Y Velocity does not update if there is no X velocity. 0 Answers

How to change speed for a cloned prefab instead of applying it to all instances of prefab? 1 Answer

ProBuilder-made object not generating Nav Mesh (Unity3D) 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