looking for help.

Updated on March 2, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on March 1, 2018

hey guys im looking for some advice, i’ve been following the brackeys unity tutorial and although been doing everything ive been shown i still get things going wrong like the player hitting an obstacle and shooting up into the air. also my game score is starting at 9999 and counting down, please someone tell me what ive done wrong. thank you

  • Liked by
Reply
6 on March 1, 2018

Can I please see your Player and Score script?

on March 1, 2018

it says admin has disabled this function when i try uploading images.

on March 1, 2018

public class playermovement : MonoBehaviour {
public Rigidbody rb;
public float momentum = 500f;
public float backwards = -500f;
public float right = 500f;
public float left = -500f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
rb.AddForce(0, 0, momentum *Time.deltaTime);
if (Input.GetKey(“s”))
{
rb.AddForce(0, 0, backwards);
}if(Input.GetKey(“d”))
{
rb.AddForce(right, 0, 0,ForceMode.VelocityChange);
}if (Input.GetKey(“a”))
{
rb.AddForce(left, 0, 0, ForceMode.VelocityChange);
}

}
public playermovement movement;
void OnCollisionEnter(Collision collisioninfo)
{
if(collisioninfo.collider.tag == “obsticle”)
{
movement.enabled = false; // this will stop player moving after
// hitting obsticle

}
}
}

public class gamescore : MonoBehaviour {

public Transform player;
public Text scoretext;

// Update is called once per frame
void Update () {
scoretext.text = player.position.z.ToString(“0”);

}
}

Wise
on March 1, 2018

In the future, could you paste the code between the

[Code] and [/Code] tags (w/o caps). Unformatted code is a bit difficult to follow.

Thanks.

 

As to your problems, Your player class stores a reference to a RigidBody, as well as to a playermovement. I’m guessing you’re setting those in the editor, but double check just to make sure.

 

In your collision check method, all you do is disable the aforementioned referenced playermovement script. The RigidBody is still active and being acted upon by the world, so that’s most likely whats causing it to fly up when you hit something.

 

For the score issue. Without seeing where the player is in the scene I can’t answer that. You just have the score set to the player’s global Z coordinate. So the only reason it should show 9999 and count down, is because the player is at Z 9999, and moving backward.

on March 2, 2018

thank you mate i understand whats going on now , what you mean about copying code between code and tags?

 

Wise
on March 2, 2018

That little box by the “Upload Image” button is for pasting formatted code.

It helps make your code easier to read by changing it from this:

 

public class playermovement : MonoBehaviour {
public Rigidbody rb;
public float momentum = 500f;
public float backwards = -500f;
public float right = 500f;
public float left = -500f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
rb.AddForce(0, 0, momentum *Time.deltaTime);
if (Input.GetKey(“s”))
{
rb.AddForce(0, 0, backwards);
}if(Input.GetKey(“d”))
{
rb.AddForce(right, 0, 0,ForceMode.VelocityChange);
}if (Input.GetKey(“a”))
{
rb.AddForce(left, 0, 0, ForceMode.VelocityChange);
}

}
public playermovement movement;
void OnCollisionEnter(Collision collisioninfo)
{
if(collisioninfo.collider.tag == “obsticle”)
{
movement.enabled = false; // this will stop player moving after
// hitting obsticle

}
}
}

public class gamescore : MonoBehaviour {

public Transform player;
public Text scoretext;

// Update is called once per frame
void Update () {
scoretext.text = player.position.z.ToString(“0”);

}
}

 

To this:

public class playermovement : MonoBehaviour {
     public Rigidbody rb;
     public float momentum = 500f;
     public float backwards = -500f;
     public float right = 500f;
     public float left = -500f;
// Use this for initialization
     void Start () {
    }
// Update is called once per frame
        void Update () {
        rb.AddForce(0, 0, momentum *Time.deltaTime);
        if (Input.GetKey(“s”))
        {
        rb.AddForce(0, 0, backwards);
        }if(Input.GetKey(“d”))
        {
        rb.AddForce(right, 0, 0,ForceMode.VelocityChange);
        }if (Input.GetKey(“a”))
        {
        rb.AddForce(left, 0, 0, ForceMode.VelocityChange);
        }
    }
     public playermovement movement;
     void OnCollisionEnter(Collision collisioninfo)
     { 
         if(collisioninfo.collider.tag == “obsticle”)
         { 
          movement.enabled = false; // this will stop player moving after
 // hitting obsticle
        }
     }
}
public class gamescore : MonoBehaviour {
    public Transform player;
     public Text scoretext;
// Update is called once per frame
     void Update () {
         scoretext.text = player.position.z.ToString(“0”);
    }
}
on March 2, 2018

oh right, ok no problem if im looking for help again( most likely) ill post code that way 🙂

Show more replies
  • Liked by
Reply
Cancel
0 on March 1, 2018

ive worked out the -9999 score issue. i had the player z axis set at -9999 lol just cant figure out the collision and player shooting up in the air issue?

  • Liked by
Reply
Cancel