- Home /
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");
}
}
}
Are you using NavmeshAgent to control your player's movement?
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");
// }
}
}
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.
Just tried it and it works thanks a bunch :), make your comment a answer so i can accept please.
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);
}
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 :)
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.
Your answer
