- Home /
Rotating an object with touch
hi , I was looking for a way to rotate an object on my android app . I found a helpful video on youtube with this script . the script works right except I need to comment out the raycasting condition , it doesn't work with raycasting and so the object rotates by touching anywhere on the screen and not just by touching the object . do you see anything wrong or have any suggestions on how to make it work with raycasting? tnx . here's the code :
var targetItem : GameObject;
var GUICamera : Camera;
var ambient : GameObject;
/********Rotation Variables*********/
var rotationRate : float = 1.0;
private var wasRotating;
/************Scrolling inertia variables************/
private var scrollPosition : Vector2 = Vector2.zero;
private var scrollVelocity : float = 0;
private var timeTouchPhaseEnded: float;
private var inertiaDuration : float = 0.5f;
private var itemInertiaDuration : float = 1.0f;
private var itemTimeTouchPhaseEnded: float;
private var rotateVelocityX : float = 0;
private var rotateVelocityY : float = 0;
var hit: RaycastHit;
private var layerMask = (1 << 8) | (1 << 2);
//private var layerMask = (1 << 0);
function Start()
{
layerMask =~ layerMask;
}
function FixedUpdate()
{
if (Input.touchCount > 0)
{ // If there are touches...
var theTouch : Touch = Input.GetTouch(0); // Cache Touch (0)
var ray = Camera.main.ScreenPointToRay(theTouch.position);
var GUIRayq = GUICamera.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(ray,hit,50,layerMask))
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began)
{
wasRotating = false;
}
if (theTouch.phase == TouchPhase.Moved)
{
targetItem.transform.Rotate(0, theTouch.deltaPosition.x * rotationRate,0,Space.World);
wasRotating = true;
}
}
}}
Answer by smtabatabaie · May 22, 2012 at 09:51 AM
thanks for all answers and responds :D the problem was this line :
var GUIRayq = GUICamera.ScreenPointToRay(theTouch.position);
if I comment out this line the script will work , cause I hadn't assigned it and the script would stop at that line .
thank you for your answer and responds :D you helped me :)
Just wanted to throw in a quick comment - if you're doing it in C#, don't forget the "out" for a ray cast!
if(Physics.Raycast(ray, out hit,50f,layer$$anonymous$$ask)){}
i used this same code.. but im getting an error..
BCE0044: expecting }, found ''.
Please help me..