Swinging monkey

Is there a better way to do this? I’m trying to add some interactivity to a 2D children’s book. Simple effects. Like making a monkey swing on a branch. I have set up hinge joints for the tail, body and arms. The monkey swings just fine. I then created a separate object (cube) onto which I placed the code below. Now, whenever the cube collides with the monkey’s body, the monkey reacts. All good so far. My question is, can this be done without the intermediate object of the cube? Can I not just swing the monkey on touch depending on where the drag position is?

Thanks for the help.

function Update () {
    for(touch in iPhoneInput.touches) {
         if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
          transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
          }
         else {
          transform.position = (new Vector3 (touch.position.x, touch.position.y, -40));
        }
    }
}

Ok, so this does the job and ads velocity to the monkey in x. But this does it only in one direction. What if I want to swing it in -x depending on the direction of the touch? What if I want to swing it harder depending on how fast I swipe? Or slow the monkey down?

I also tried this snippet of code instead of velocity, but this moves the monkey’s body to the touch position, thus breaking the joint (separating body from the tail) until I release.

monkey.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z - 5));

Any other ideas?

Cheers

var monkey : GameObject;

var swipeDeltay:float;

function Update () {
for (var touch : iPhoneTouch in iPhoneInput.touches){
print(touch.position);
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
if (hit.transform == monkey.transform){
if(touch.phase == iPhoneTouchPhase.Began || touch.phase == iPhoneTouchPhase.Moved) {
var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0);
monkey.rigidbody.velocity = Vector3(5,0,0);
}
}
}
}
}

You say it only swings in one direction, which is exactly what you tell it to do here:

monkey.rigidbody.velocity = Vector3(5,0,0);

If you want it to be based on swiping, use something like this:

monkey.rigidbody.velocity = Vector3(touch.deltaPosition.x * Time.deltaTime * modifier,0,0);

where modifier is some value you use to control the sensitivity. Try that or use the deltaPosition with ApplyForce()

Thanks Earl

That’s great! Works perfectly.

I just have a small follow up question… This piece of code (below), is placed on my interactive objects in different scenes. On average I have four/five interactive objects per scene. I could have a single control script and call this function once, and do everything in one script, but this gets a little messy, so I prefer to individually code scripts for each object. Is this a bad approach? Is there a performance hit? Could I call this bit of code in one separate script and then reference it in all the others?

Just looking for the correct / most optimum way of doing things.

Thank you!

function Update () {
for (var touch : iPhoneTouch in iPhoneInput.touches){
print(touch.position);
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {