- Home /
Question by
GSP · Oct 13, 2010 at 11:46 AM ·
iphonetouch
Improve performance of touch script?
Hi i am trying this code to move several spheres on a plane using the touch. but i was facing a problem that, by touching the object it taking time to detect the touch.so if anybodyt helps me to improve the performance of the touch i feel to great.
using UnityEngine; using System.Collections;
public class DragRigidbody : MonoBehaviour {
public float spring = 50.0f;
public float damper = 5.0f;
public float drag = 10.0f;
public float angularDrag = 5.0f;
public float distance = 0.2f;
public bool attachToCenterOfMass = false;
private SpringJoint springJoint;
private Touch touch;
void Update ()
{
// Make sure the user touched the screen
if (Input.touchCount == 0) {
return;
}
Camera mainCamera = FindCamera ();
// We need to actually hit an object
RaycastHit hit = new RaycastHit ();
touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
if (!Physics.Raycast (mainCamera.ScreenPointToRay (touch.position), out hit, 100.0f))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint) {
GameObject go = new GameObject ("Rigidbody dragger");
//Rigidbody is automatically added by SpringJoint
springJoint = (SpringJoint)go.AddComponent ("SpringJoint");
go.rigidbody.isKinematic = true;
}
springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
Vector3 anchor = transform.TransformDirection (hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint (anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine ("DragObject", hit.distance);
}
}
/*private int count;
void start ()
{
count = 0;
}*/
IEnumerator DragObject (float distance)
{
if (touch.phase == TouchPhase.Began) {
float oldDrag = springJoint.connectedBody.drag;
float oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
Camera mainCamera = FindCamera ();
while (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
Ray ray = mainCamera.ScreenPointToRay (touch.position);
springJoint.transform.position = ray.GetPoint (distance);
yield return 0;
}
if (springJoint.connectedBody) {
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
}
Camera FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613075515im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
A node in a childnode? 1 Answer
Save Position script. 2 Answers
C# Non-Static Member Rigidbody2D.MovePosition 1 Answer
Script for tank fire 0 Answers