Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Ball resets position when scoring but speed doesn't

I made a soccer game and added some "on trigger colliders".

Made the ball reset it's position to the middle of the field when scoring,but the speed that the ball had when entering the goal post still applies to the ball when reseted(so when i score the ball is still flying when i reset it to the middle.)

Also is my first game ,new to programming :)

Best Answer

  • MouledouxMouledoux Member
    edited June 2020 Accepted Answer

    You're close with that.

    The error you're getting is because you removed the 'void' when you renamed the OnTriggerEnter method, which you should keep, we just want to move the code in there to a NEW method so it's easier to manage.


    // Edit: Fixed formatting =p

    void OnTriggerEnter(Collider col)
    {
         if(col.CompareTag("goal"))
        {
            ResetBall();
        }
    }
    
    void ResetBall()
    {
        Rigidbody rb = GetComponent<Rigidbody>();
        rb.velocity = Vector3.zero;
        transform.position  = startingPosition;
    }
    


Answers

  • MrSlugMrSlug Member

    Hi @Spartan,

    That sounds like a velocity issue, are you using a Rigidbody for this?

    Can you post some code? We can help more with code to review.

    Thanks,

    Slug

  • ,This is the code,i want the ball to reset it s speed too.

    i ll try and add the scoring system by myself later <#

  • MouledouxMouledoux Member
    edited June 2020

    Can you post the code that moves the ball too?

    All you need to to is set the velocity to 0, but we need to see where the velocity is set to begin with.


    Edit: Just remembered that this is a ball, not receiving direct input, my B.

    1. Move your reset position into a new function called ResetBall()
    2. add this line to it: GetComponent<Rigidbody>().velocity = Vector3.zero;
    3. Put ResetBall() in your OnTriggerEnter
  • I m verry new to unity and c# i don t understand,could you give me photo/text with the corrected things ?

    I m more used to c++ but idk if unreal is good for begginers

    Also i get an error "Method must have a return type"

Sign In or Register to comment.