Search Unity

Help on Collision

Discussion in 'Editor & General Support' started by mosiGames, Jan 14, 2019.

  1. mosiGames

    mosiGames

    Joined:
    Apr 16, 2018
    Posts:
    3
    New to Unity and need some help about dealing with collisions properly.
    I want the "Player" to stick on the Object (Planet) on collision. This works. But when i want to move away from the Object, there is another collision from the object i was befor and it behaves weird. (moves slightly away from the object, see gif)

    collision_problem.gif

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class SwipeScript : MonoBehaviour
    7. {
    8.     private Vector2 startPosition, endPosition, direction;
    9.     private float touchTimeStart, touchTimeFinish, timeInterval;
    10.  
    11.     [Range(1f, 3f)] public float throwForce = 1f;
    12.  
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    25.         {
    26.             touchTimeStart = Time.time;
    27.             startPosition = Input.GetTouch(0).position;
    28.         }
    29.  
    30.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
    31.         {
    32.             touchTimeFinish = Time.time;
    33.            
    34.             timeInterval = touchTimeFinish - touchTimeStart;
    35.             endPosition = Input.GetTouch(0).position;
    36.  
    37.             direction = startPosition - endPosition;
    38.  
    39.             GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    40.             GetComponent<Rigidbody2D>().transform.parent = null;
    41.             GetComponent<Rigidbody2D>().AddForce(-direction * throwForce);
    42.  
    43.         }
    44.     }
    45.  
    46.     void OnCollisionEnter2D(Collision2D col)
    47.     {
    48.         print("Collision");
    49.         if (col.gameObject.tag == "Planet")
    50.         {
    51.             Debug.Log("Planet Collision");
    52.             GetComponent<Rigidbody2D>().velocity = Vector3.zero;
    53.             GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
    54.             transform.SetParent(col.transform);
    55.         }
    56.     }
    57.  
    58.     void OnCollisionExit2D(Collision2D col)
    59.     {
    60.         print("CollisionExit");
    61.         if (col.gameObject.tag == "Planet")
    62.         {
    63.             //col.transform.parent = null;
    64.         }
    65.     }
    66. }
    67.  
    Maybe there is another way to do this or did i miss something?

    Glad for any help from you guys.
     

    Attached Files: