Hey,
I have a really confusing problem.
I am trying to implement the follwing:
There is a marble in the scene. Once the player touches the marble, the marble should start to roll.
I have wrote the code, but nothing happens. So, I have rewrote the code for mouse input, and it works fine there.
I dont know what is the problem with the touch version.
Here is the touch version of the code:
enum BallState
{
phase1 = 0,
phase2 = 1
}
var ballState = BallState.phase1;
var playerLayer : int = 11;
var playerMask : int = 0;
var marbleTouch : Touch;
function Start()
{
playerMask = 1 << playerLayer;
}
function Update()
{
if (ballState == BallState.phase1)
{
if (Input.touches.Length > 0)
{
for (var touch : Touch in Input.touches)
{
//Look for a marble touch
var ray : Ray = Camera.main.ScreenPointToRay (touch.position);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity, playerMask))
{
marbleTouch = touch;
ballState = BallState.phase2;
break;
}
}
}
}
if (ballState == BallState.phase2)
rigidbody.AddTorque(50 * Time.deltaTime, 0, 50 * Time.deltaTime);
}
And here is the mouse version, which works fine:
enum BallState
{
phase1 = 0,
phase2 = 1
}
var ballState = BallState.phase1;
var playerLayer : int = 11;
var playerMask : int = 0;
function Start()
{
playerMask = 1 << playerLayer;
}
function Update()
{
if (ballState == BallState.phase1)
{
if(Input.GetMouseButton(0))
{
//Look for a marble touch
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity, playerMask))
{
ballState = BallState.phase2;
}
}
}
if (ballState == BallState.phase2)
rigidbody.AddTorque(50 * Time.deltaTime, 0, 50 * Time.deltaTime);
}
I have really no idea what is wrong, why the touch version is not working.
I would really appreciate any help.
Thanks!