- Home /
Problem with jumping,Problem with code(Jumping by checking y position)
I am trying to make code to can restrict only ony jumping by checking y position of my object. But i can jump always no matter of y position.Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEditor.UIElements; using UnityEngine;
public class kretanje : MonoBehaviour { private Transform playertransform; private Rigidbody rdb; private GameObject neprijatelj; private bool grounded; public GameObject player; public float brzina; private bool smrt; void Start() { playertransform = GetComponent(); rdb = GetComponent(); grounded = true;
}
void FixedUpdate()
{
if (player.transform.position.y <= 17.85)
{
grounded = true;
}
if (player.transform.position.y >= 17.85)
{
grounded = false;
}
Vector3 xyz = new Vector3(horizontalnokretanje(), vertikalnokretanje(), 0);
xyz = playertransform.TransformDirection(xyz);
xyz = xyz * brzina;
if (grounded==true)
{
rdb.AddForce(xyz);
}
float horizontalnokretanje()
{
return Input.GetAxis("Horizontal");
}
float vertikalnokretanje()
{
return Input.GetAxis("Vertical");
}
}
},I have tried to make code to check my y position and if it is bigger than 17.85 it should jump, but i can jump always.Looking for help there is a code:
using System.Collections; using System.Collections.Generic; using UnityEditor.UIElements; using UnityEngine;
public class kretanje : MonoBehaviour { private Transform playertransform; private Rigidbody rdb; private GameObject neprijatelj; private bool grounded; public GameObject player; public float brzina; private bool smrt; void Start() { playertransform = GetComponent(); rdb = GetComponent(); grounded = true;
}
void FixedUpdate()
{
if (player.transform.position.y <= 17.85)
{
grounded = true;
}
if (player.transform.position.y >= 17.85)
{
grounded = false;
}
Vector3 xyz = new Vector3(horizontalnokretanje(), vertikalnokretanje(), 0);
xyz = playertransform.TransformDirection(xyz);
xyz = xyz * brzina;
if (grounded==true)
{
rdb.AddForce(xyz);
}
float horizontalnokretanje()
{
return Input.GetAxis("Horizontal");
}
float vertikalnokretanje()
{
return Input.GetAxis("Vertical");
}
}
} and i have added Player to the gameobject in unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613001759im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# store compare start position and actual position 1 Answer
How can I limit jump height in unity? 3 Answers
Camera rotation around player while following. 6 Answers
How to get an object's relative position to another. 2 Answers
how to stop the camera to follow the player on his jump movement ? 2 Answers