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 mkobaner · Apr 11, 2016 at 03:30 PM · navmeshagentcontrol

Using navmesh as controller and making character jump ( unable to jump as of now)

Hello,

I try to make a game with pandemonium like controls, character should move around in a 3d enviroment in a path, while used controls left right and jump options. with another script i set the path with navmesh. However character just doesnt jump, jump line works since it gives jump to console however, the character doesnt jump at all.

Here is my code thanks for advice

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour {
     public bool grounded = true;
     public float jumpPower = 1;
     // Use this for initialization
 
     IEnumerator SetGroundedTrue(){
         yield return new WaitForSeconds (1);
 
         grounded=true;
 
 
     }
 
     void Start () {
 
     }
 
     // Update is called once per frame
     void FixedUpdate () {
         //if (Physics.Raycast (transform.position, Vector3.down, transform.localScale.y / 2) || Physics.Raycast (transform.position - new Vector3 (transform.localScale.x / 2, 0, 0), Vector3.down, transform.localScale.y / 2) || Physics.Raycast (transform.position + new Vector3 (transform.localScale.x / 2, 0, 0), Vector3.down, transform.localScale.y / 2)) {
         //    grounded = false;
         //}
 
         if (Input.GetButtonDown("Jump") && grounded == true) {
             transform.Translate(Vector3.up * jumpPower * Time.deltaTime);
             grounded = false;
             Debug.Log("Jump");
             StartCoroutine ("SetGroundedTrue");
         }
     }
 
 
 }
 
Comment
Add comment · Show 2
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 incorrect · Apr 11, 2016 at 04:39 PM 0
Share

Are you using NavmeshAgent to control your player's movement?

avatar image mkobaner incorrect · Apr 11, 2016 at 05:23 PM 0
Share

Yes, i do control code is like this altough, control and jump code are separate.

 using UnityEngine;
 using System.Collections;
 
 public class Control2 : $$anonymous$$onoBehaviour {
 
 
     public Transform hedef;
     public Transform baslama;
 
     Nav$$anonymous$$eshAgent agent;
     //public float x;
 
 
     // Use this for initialization
     void Start () {
         agent = GetComponent<Nav$$anonymous$$eshAgent>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         if (Input.GetAxis("Horizontal")> 0.3f)
         {
 
             //x=1.00f;
             agent.Resume();
             agent.SetDestination (hedef.position);
 
         }
 
          if(Input.GetAxis("Horizontal")< -0.3f)
         {
             //x = -1.00f;
             agent.Resume();
             agent.SetDestination(baslama.position);
         }
 
         if(Input.GetAxis("Horizontal")> -0.3f&& Input.GetAxis("Horizontal")< 0.3f)
         {
             //x = 0.00f;
             agent.Stop();
             //agent.SetDestination(baslama.position);
         }
 
 
         //if (x == 1.00f) {
 
         //    agent.Resume();
         //    agent.SetDestination (hedef.position);
         //    Debug.Log("<color=red>X=1");
         //}
 
 //        if (x == -1.00f) {
 //            agent.Resume();
 //            agent.SetDestination (baslama.position);
 //            Debug.Log("<color=red>X=-1");
 //        }
 
 //        if (x == 0.00f) {
 //            agent.Stop();
 //            Debug.Log("<color=red>X=0");
 //        }
 
     }
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by incorrect · Apr 11, 2016 at 06:27 PM

NavmeshAgent controls movement of an object in every dimension, so it just overrides your attempts to move an object in vertical direction. It moves exactly along the navmesh itself. Try to make player's model a child of a NavmeshAgent and move it up and down when jumping using transform.localPosition.

Comment
Add comment · Show 3 · 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 mkobaner · Apr 11, 2016 at 06:44 PM 0
Share

Thats a very good idea i will try it, Thanks.

avatar image mkobaner · Apr 11, 2016 at 06:48 PM 1
Share

Just tried it and it works thanks a bunch :), make your comment a answer so i can accept please.

avatar image incorrect mkobaner · Apr 11, 2016 at 07:07 PM 0
Share

Glad to help. :)

avatar image
1

Answer by b1gry4n · Apr 11, 2016 at 07:05 PM

Translate needs to be continuously called in update.

http://docs.unity3d.com/ScriptReference/Transform.Translate.html

You are only calling it for 1 frame when your GetKeyDown is true. Take the logic out of the input controls and it should work.

         if (Input.GetButtonDown("Jump") && grounded)
         {            
             grounded = false;
             Debug.Log("Jump");
             StartCoroutine("SetGroundedTrue");
         }
 
         if (!grounded)
         {
             transform.Translate(Vector3.up * jumpPower * Time.deltaTime);
         }
Comment
Add comment · Show 1 · 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 mkobaner · Apr 11, 2016 at 07:27 PM 0
Share

Thanks this is a much better way to implement force, however real issue was making the player child of agent and assigning the jump script, incorrect gave me the idea so i will make her comment answer when she reverts it to answer, thanks for the optimization tough :)

avatar image
0

Answer by Lycanite · Mar 28, 2017 at 07:09 AM

In my approach I wanted absolute control over movement as I intend to use root bone animation for movement, also there will be knockbacks, etc to handle.

First I set updatePosition and updateRotation on the agent both to false.

Next on update I use :

 Agent.nextPosition = Controller.transform.position;

This updates the simulated position of the agent so that it will update the position on the path, etc. Finally I move the character controller towards the Agent.steeringTarget which is the Vec3 of the next node of the path to move towards.

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

43 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Reversed Controls? 2 Answers

Help with iphone/android touch controls 2 Answers

Camera Control and Character Rotation 1 Answer

how to make another object do what i do but with a delay 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