Help with trigger script

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.

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!

7168696--858781--Screen Shot 2021-05-23 at 2.04.58 PM.png

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.

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.

Thank you I will try this

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().velocity = Vector3.zero;
gameObject.GetComponent().angularVelocity = Vector3.zero;
}
}
}

1 Like

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. :slight_smile: