Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help with trigger script

Discussion in 'Editor & General Support' started by kingdom216, May 23, 2021.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    32
    I am in need of script to enable my game ball to be reset to start position after colliding with a goal trigger, and after colliding with a boundary trigger. I am a novice, but I have set up a game controller to keep score and run a time clock, but I need the goal and boundary triggers to stop time and for the scores to be separated by teams. Right now both goal triggers are connected to the same score text. Each boundary will have a different reset point as well. Any help is greatly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    18,391
    This is going to be a combination of scripting, interfacing with what you already have, and properly setting up the scene and prefabs, and hence it is not possible for someone to just retype it all for you here.

    Luckily there is Youtube!

    Screen Shot 2021-05-23 at 2.04.58 PM.png
     
  3. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    32
    Thank you, but I have seen these on youtube, that is what prompted me to come here. I know how to do what these tutorials are teaching, but I am not sure how to expand beyond these lessons. I have tags set up on the triggers, and a debug statement for the boundaries, but not a specific reset game object position, also I have the goal trigger set up to count goals with the score text, but not how to reset the ball after a score.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    18,391
    Probably easiest is to make some kind of BallWatcherAndRestorer that records the position at start, then resets it when conditions are correct, eg., the trigger trips.

    If it has a Rigidbody then you'd need to null out its velocities and spin.
     
  5. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    32
    Thank you I will try this
     
  6. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    32
    I figured it out, this is the code that I pieced together from a couple different ideas.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Boundary : MonoBehaviour
    {
    Vector3 originalPos;
    void Start()
    {
    originalPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);

    }

    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "Boundary")
    {
    gameObject.transform.position = originalPos;
    gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
    gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    }
    }
    }
     
    Last edited: May 28, 2021
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    18,391
    Looking through your code, that is pretty much exactly how I would have tried it. Nice!

    ALSO: if you get a chance, copy/paste the above with code tags and then it will be nice and shiny for future generations to admire. :)
     
unityunity