Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Geyser Mechanic

Discussion in '2D' started by Whadafuxhup, Jul 27, 2019.

  1. Whadafuxhup

    Whadafuxhup

    Joined:
    Jun 28, 2019
    Posts:
    9
    Hi all you lovely competent people :D I'm trying to design a geyser which will shoot players into the sky but I'm running into a couple problems! Most importantly the geyser won't return back to it's starting position by itself (without the player colliding with it after jumping off). Here's my script -

    Code (CSharp):
    1. public class GeyserScript : MonoBehaviour
    2. {
    3.     public Transform pos1, pos2;
    4.     public float speed;
    5.     public Transform startPos;
    6.  
    7.     Vector3 nextPos;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         nextPos = startPos.position;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void OnTriggerStay2D(Collider2D collision)
    17.     {
    18.  
    19.        
    20.        
    21.             if (transform.position == pos1.position)
    22.             {
    23.                 nextPos = pos2.position;
    24.             }
    25.            
    26.  
    27.             transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    28.        
    29.     }
    30.  
    31.     void OnCollisionExit2D(Collision2D collision)
    32.  
    33.     {
    34.        
    35.             if (transform.position == pos2.position)
    36.             {
    37.                 nextPos = pos1.position;
    38.             }
    39.             transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    40.        
    41.     }
    42.     private void OnDrawGizmos()
    43.     {
    44.         Gizmos.DrawLine(pos1.position, pos2.position);
    45.     }
    46.  
    47.  
    48. }
    I'm also planning on having the player get shot upwards off the top of the geyser which I can't figure out although that problem isn't urgent.
     
  2. migueltuliompg

    migueltuliompg

    Joined:
    Feb 12, 2018
    Posts:
    34
    Does your player have a rigidbody?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832
    If you don't have a Rigidbody2D on this GameObject then you've implicitly set them as static i.e. non-moving so you should not be moving them. If you've got a Rigidbody2D on it then you've implicitly stated that it'll control the Transform and you should not be modifiying it in any way. Make all movements via the Rigidbody2D instead using Rigidbody2D.MovePosition, velocity changes or forces.

    Also, it's bad comparing floats without a margin of error i.e. doing an approximate compare. Here you're comparing three floats (transform.position) which isn't good. You should test if the sqr-magnitude of the difference between the two positions is above some small threshold instead.