Search Unity

Pushing a Tire Swing Around Pivot Point in C#

Discussion in 'Scripting' started by artistshc, Aug 12, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Hi there. I want to be able to drag the mouse right or left on the tire swing in this picture....


    And have the swing be able to rotate left or right according to which way the player swiped with the mouse. I have an empty game object and this is the pivot point. Then I have the actual tire swing as the child game object. I have this script attached to the pivot point.

    Code (CSharp):
    1. #pragma strict
    2.     Quaternion startRotation = Quaternion.Euler(0,0,90);
    3.     Quaternion endRotation = Quaternion.Euler(0,0,-90);
    4.     float speed = 1.0f;
    5.    
    6.     void Update() {
    7.         float t = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
    8.         transform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
    9.     }
    Of course this code just swings left and right....I don't want it to do this unless the swing is pushed or pulled. I would love if the swing is pushed too hard that it goes a full 360 degrees around the pivot point.

    Please help. Thank you!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,824
    use rigidbodies and joints.

    You can have a Rigidbody up at the pivot point with a hingejoint attached to get the pivot:
    http://docs.unity3d.com/Manual/class-HingeJoint2D.html

    As for the rope and tire... depends on how you want the rope. If you want the rope to be floppy, you could do a series of hinge joints all the way down so that it could flop around.

    Or you could make the rope taught and use either a SpringJoint or a DistanceJoint to keep the tire at the ropes distance from the pivot:
    Spring: http://docs.unity3d.com/Manual/class-SpringJoint2D.html
    Dist: http://docs.unity3d.com/Manual/class-DistanceJoint2D.html

    You can google about pendulum and the sort in unity for tuts and conversations on the matter.
     
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Thank you so much. I guess I have some 'studying' to do. :)

     
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,572
    If you want a taught rope, you could also just use the Unit Circle and clamp the angles. It could be more lightweight than a physics object.
     
  5. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Thank you Landon. I'm learning so much today. This helps a lot...thank you!!!

     
  6. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Hi LordOfDuct - Thank you so much. I am using a Sprint Joint and it works great. What is the best way to push the swing. I have this script on it, but it drags the swing off of the tree...other than that it works....but I must be taking the wrong approach.

    Code (CSharp):
    1.     void OnMouseDrag()
    2.     {
    3.  
    4.         Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
    5.         point.z = transform.position.z;
    6.         transform.position = point;
    7.  
    8.  
    9.  
    10.     }
    11.  
    12.     void OnMouseUp(){
    13.         directionCollider.checkOutOfCabinet (gameObject);
    14.         //if gameObject is no longer in box then return
    15.  
    16.     }
    How would you go about pushing the swing. Thank you for your help!
    Rachel

     
  7. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Does anyone know how to push the swipe the mouse to push the swing? i.e. swipe right (on the swing), push the swing right, swipe left (on the swing) and push left. Thank you!
     
unityunity