I have this script, to rotate the 3D object, i put it in the 3D object, it works fine by touching the whole screen, I need it to work, just by touching the 3D object, and not the whole screen.
How can i fix it.
Any help please…thanks
//Start code
var rotationSpeed = 2.0;
var lerpSpeed = 1.0;
private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var targetSpeedX = new Vector3();
function OnMouseDown()
{
dragging = true;
}
function Update() {
if (Input.touchCount == 1)
{
var theTouch : Touch = Input.GetTouch(0);
if (theTouch.phase == TouchPhase.Moved)
{
OnMouseDown();
}
if ((theTouch.phase == TouchPhase.Ended) || (theTouch.phase == TouchPhase.Canceled))
{
dragging = false;
}
}
if (Input.touchCount > 1)
{
dragging = false;
}
//Debug.Log ("Speed = " + speed);
//Debug.Log ("Dragging = " + dragging);
if ((theTouch.phase == TouchPhase.Moved) dragging)
{
speed = new Vector3(theTouch.position.x, theTouch.position.y, 0);
avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
}
if (Input.GetMouseButton(0) dragging)
{
speed = new Vector3(-Input.GetAxis (“Mouse X”), Input.GetAxis(“Mouse Y”), 0);
avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
}
else
{
if (dragging) {
speed = avgSpeed;
dragging = false;
}
var i = Time.deltaTime * lerpSpeed;
speed = Vector3.Lerp( speed, Vector3.zero, i);
}
transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
}
//End code snippet
Use a raycast from the camera to where they touched to see if their touch is hitting the object.
You will need to either put that object on a layer by itself, and use a layer mask so the raycast will only hit that object. Or you could just get the nearest hit and check if it is the right object (then the player won’t be able to rotate it behind obstacle colliders).
If you do get a hit, then start dragging the object.
I put this code to the 3D object, and assign layer 8 to the 3D object.
but does not work.
any help…please, thanks
// JavaScript example.
// bit shift the index of the layer to get a bit mask
var layerMask = 1 << 8;
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask))
print (“The ray hit the player”);
I put this code, but does not work, i need just when i touch the object, not the whole screen…
var mask : LayerMask = 1<<9;
function Update () {
if (Physics.Raycast (transform.position, transform.forward, 100, mask.value)) {
Debug.Log(“Hit something”);
}
}
does not work too…