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?
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.
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);
}
}
}
}
}
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)) {