Search Unity

problems with triggers

Discussion in 'Getting Started' started by Smitty3264, Oct 24, 2020.

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    ah geez it took me a whole afternoon of research to come up with the wrong code. I think I have exhausted my resources so I'm turning it over to you guys for help.


    I have a cylinder as a trigger that's intended to move a pool ball to the center of the table.
    instead ball moves instantly to the center of the cylinder.



    Here it is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6.  
    7. public class Trigger2 : MonoBehaviour
    8. {
    9.  
    10.     Vector3 targetPos = new Vector3(13f, 0f, 0.7f);
    11.     public float speed = 5f;
    12.     public float maxDistanceDelta = (13f);
    13.  
    14.     void OnTriggerEnter(Collider ballCollider)
    15.     {
    16.         if (ballCollider.gameObject.CompareTag("Ball"))
    17.  
    18.  
    19.            ballCollider.transform.position = Vector3.MoveTowards(transform.position, targetPos, (speed * maxDistanceDelta * Time.deltaTime));
    20.     }
    21. }
     
    Last edited: Oct 25, 2020