problem with jump script
In this Script it seems i can't use the OnCollisionStay the way i did. How do i check the tag of the Object my player collides with every frame then? i'm trying to use that to set the "grounded" boolean to true everytime he touches something with the tag "Ground" How do i fix this script?
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour {
public Rigidbody rb;
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
public bool grounded = true;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if (Input.GetKeyDown (KeyCode.Space)) {
if (grounded == true) {
rb.AddForce (0, 400, 0);
grounded = false;
}
}
void OnCollisionStay (Collision col) //in this part i can't use OncollisionStay it seems.
{
if (col.gameObject.tag == "Ground") {
grounded = true;
}
}
Debug.Log (grounded);
}
}
Answer by JaredHD · Dec 14, 2016 at 08:34 PM
I would make the grounds collider slightly higher than the ground and make sure it is a trigger collider then add the following code to the ground (Not the player)
Also note you would need to make the grounded variable a static variable or access grounded by using the .GetComponent() in the ground script
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
Movement.grounded = true;
}
}
More simply (Just thought of this) you can determine your current height
void Update()
{
if(transform.position.y > 0.5f)
{
grounded = false;
}
else
{
grounded = true;
}
}
Answer by Guigondi · Dec 15, 2016 at 12:47 AM
@JaredHD I'd rather do the first method you suggested since using the Y position like you did would limit the height variety of the platforms i could use to jump.
I tried followingyour first suggestion and i'm pretty sure i've made a few obvious mistakes (i'm still learning code).I'm not sure how to properly use GetComponent.
here is the script i did for the ground based on what you showed.
using UnityEngine; using System.Collections;
public class colisao_com_chao : MonoBehaviour {
GameObject Mov;
// Use this for initialization
void Start () {
Mov = Player.GetComponent<Movement> ();
}
void OnTriggerEnter (Collider colisao)
{
if (colisao.gameobject.tag == "Player")
{
Movement.grounded = true;
}
}
// Update is called once per frame
void Update () {
}
}
The .GetComponent(); is used to retrieve a script (On a Gameobject) that you wrote to access it's public variables. ins$$anonymous$$d of .GetComponent(); you could make all the variables static to access them but that's not always best.
GameObject $$anonymous$$ov;
void Start()
{
//$$anonymous$$ake sure the player is named "Player" no quotes or add a player Tag and use this commented code ins$$anonymous$$d
// $$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent<$$anonymous$$ovement>();
$$anonymous$$ov = GameObject.Find("Player");
}
void OnTriggerEnter(Collider colisao)
{
if (colisao.tag == "Player")
{
//$$anonymous$$ov edits the grounded variable, $$anonymous$$ake sure grounded is a public bool
$$anonymous$$ov.GetComponent<$$anonymous$$ovement>().grounded = true;
}
}
Please tell me if there are any errors
it shows an error on line 12 the line with
"$$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent ();"
it says it's not possible to convert implicitly the "$$anonymous$$ovement" type in UnityEngine.GameObject. I don't know what that means.
By the way i also tried with "$$anonymous$$ov = GameObject.Find ("Player");" and while it didn't gave any errors the grounded variable still won't become true when i touch the ground after i jump.
here is the current code:
using UnityEngine; using System.Collections;
public class colisao_com_chao : $$anonymous$$onoBehaviour {
GameObject $$anonymous$$ov;
// Use this for initialization
void Start () {
$$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent<$$anonymous$$ovement> ();
}
void OnTriggerEnter (Collider colisao)
{
if (colisao.tag == "Player")
{
$$anonymous$$ov.GetComponent<$$anonymous$$ovement> ().grounded = true;
}
}
// Update is called once per frame
void Update () {
}
}
$$anonymous$$ovement is a class, $$anonymous$$ov is a GameObject. Your setting the GameObject $$anonymous$$ov equal to a component of a GameObject, not the GameObject itself, therefore, $$anonymous$$ov needs to be of similar type to the component you are setting it to. In this case, it's "$$anonymous$$ovement mov;" ins$$anonymous$$d of "GameObect mov".
Your answer
Follow this Question
Related Questions
Jump Force/Velocity is Wrong and Strange. Help 1 Answer
Unity 2D Enemy jump Question (Frog jump) 1 Answer
Multi-touch buttons 0 Answers
Need help with Character Jump! 0 Answers