Search Unity

Spring Joint 2D object can't be pulled via mouse if it is a child of a rotating parent?

Discussion in 'Physics' started by tfishell, Feb 26, 2019.

  1. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    edit: I think I basically solved the issue by not making Jonah a child of the whale. (I'm sure there will be other problems in the future though.) I'm just a little sad I couldn't even get a response. :p

    (I wasn't sure whether to post this here or the Scripting forum. If it doesn't belong here, I can of course post it in Scripting.)

    I'm working on a simple goofy religious "Angry Birds"-style game feat. Jonah shooting out of the Whale's mouth. I found a script to rotate the whale towards the mouse; and info on using the Spring Joint 2D to send Jonah flying (like with Angry Birds) - detect click with the mouse, drag away, then let the mouse up and the player goes flying. But for some reason, when I make Jonah and the anchor children of the whale, Jonah won't move with the mouse pointer and will seemingly only move a small distance when mouse lets up (because of the initial distance between Jonah and the anchor).

    The spring works fine if Jonah isn't a child of the whale gameobject, or if I turn off the whale rotation script component, so I assume there's a conflict between the scripts when Jonah is a child of the whale. Jonah is closer to the camera than the whale so I don't think it's a box collider 2D detection issue. On rigidbody2D, Jonah is kinematic until he starts flying, then he becomes dynamic.

    Gif of when Jonah works.
    Gif of when Jonah doesn't go sailing.

    Any advice or missing info? Thanks.

    Whale rotation script:

    Code (CSharp):
    1.  
    2. public class WhaleRotation : MonoBehaviour {
    3.  
    4.     [System.NonSerialized]
    5.     float speed = 5f;
    6.  
    7.     [System.NonSerialized]
    8.     private int offset = -90; //offset because axis starts pointing forward/right whereas whale points up
    9.  
    10.     [System.NonSerialized]
    11.     float minRotation = -10; //shortest rotation to the right
    12.     [System.NonSerialized]
    13.     float maxRotation = -80; //farthest rotation to the right
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         //run the method to face the mouse
    19.         RotateToMouse();
    20.     }
    21.  
    22.     void RotateToMouse()
    23.     {
    24.         //subtract the end from the start to get the direction
    25.         Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    26.  
    27.         //normalize direction
    28.         direction = direction.normalized;
    29.  
    30.         //calculate the angle by using TOA+angle? and converting to degrees
    31.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    32.  
    33.         //apply the offset
    34.         angle = angle + offset;
    35.  
    36.         //clamp so whale can't rotate past certain points
    37.         angle = Mathf.Clamp(angle, maxRotation, minRotation);
    38.  
    39.         //get the rotation from the angle (- offset because whale starts by looking up) and its axis
    40.         Quaternion rotate = Quaternion.AngleAxis(angle, Vector3.forward); //"forward" is the z axis
    41.  
    42.         //have a starting point for comparison
    43.         Quaternion stationary = Quaternion.Euler(0, 0, 0);
    44.  
    45.         //transform.rotation = rotate;
    46.         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, speed * Time.deltaTime);
    47.     }
    48. }
    Jonah "Angry Birds" script:

    Code (CSharp):
    1.  
    2. public class JonahController : MonoBehaviour {
    3.  
    4.     public Transform go_whale;
    5.     WhaleController whaleControl;
    6.     Rigidbody2D rb;
    7.     SpringJoint2D sj;
    8.     BoxCollider2D bc;
    9.  
    10.     private bool isPressed = false;
    11.     private bool rotateWithWhale = true;
    12.     public float releaseTime = 0.1f;
    13.  
    14.     void Start()
    15.     {
    16.         whaleControl = go_whale.GetComponent<WhaleController>();
    17.         rb = GetComponent<Rigidbody2D>();
    18.         sj = GetComponent<SpringJoint2D>();
    19.         bc = GetComponent<BoxCollider2D>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (isPressed)
    25.         {
    26.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    27.             rb.position = mousePos;
    28.         }
    29.     }
    30.  
    31.     void OnMouseDown()
    32.     {
    33.         isPressed = true;
    34.         rb.isKinematic = true;
    35.     }
    36.  
    37.     void OnMouseUp()
    38.     {
    39.         isPressed = false;
    40.         rb.isKinematic = false;
    41.         rotateWithWhale = false;
    42.         transform.parent = null;
    43.         StartCoroutine(Release());
    44.     }
    45.  
    46.     IEnumerator Release()
    47.     {
    48.         yield return new WaitForSeconds(releaseTime);
    49.         sj.enabled = false;
    50.         bc.isTrigger = false;
    51.     }
    52. }
    53.  
     
    Last edited: Feb 26, 2019