- Home /
Moving player relative to anywhere of the screen you are touching on
Hi I am trying to make a movement controls that is based on Flying hamster the game. In which there is an option to choose which controls to use. (In this situation, it's the relative control by touching anywhere on the screen to move the player). Here's the link to a video on Flying Hamster, [http://www.youtube.com/watch?v=FPok9pNY0wY][1]
I am trying to add in touch Controls 1. And I need it to be based on 3d instead of 2d. I had already coded some of the component but what I get is a working code but not what I had expected. (for example, instead of being able to control anywhere on the screen, I am limited to bottom centre of the screen as it will reach the limit of the screen if i press anywhere else)
> /******Ipad 3rd Controls******/
> if(PlayerControlsState.ctrlState == PlayerControlsState.ctrlState.IpadThree)
> {
> if (!dashOn)
> {
> Debug.Log("pos" + pos +"position" + tr.localPosition);
> if(Input.GetMouseButtonDown(0))
> {
> StartPosition(); //track the curr pos of mouse
> }
> if (Input.GetMouseButton(0))
> { var touchEndPos : Vector2 = Vector2(Input.mousePosition.x / Screen.width,
> Input.mousePosition.y / Screen.height);
> var touchDiff : Vector2 = touchEndPos - touchStartPos;
> var touchDist : float = touchDiff.magnitude;
> var startEnd : Vector2 = touchStartPos - Vector2(tr.localPosition.x,
> tr.localPosition.y);
> Debug.Log(startEnd);
> if (touchDist > 0.01)
> {
> if (touchDiff.x > 1)
> {pos.x *= -2; }
> else
> {pos.x *= 2;}
> if (touchDiff.y > 1)
> {pos.y *= -2;}
> else
> {pos.y *= 2;}
> //pos *= 2;
> } //Debug.Log("touch distance :" + touchDist);
> touchEndPos.x *= tempScale.x;
> touchEndPos.y *= (tempScale.x + 10);//tempScale.y;
> touchEndPos.x += minPosClamp.x;
> touchEndPos.y += minPosClamp.y;
> touchEndPos.x *= 1.2;
> touchEndPos.y *= 0.32;
> //Debug.Log("End Pos :" + touchEndPos + "Pos :" + pos);
> tr.localPosition = Vector3.MoveTowards(tr.localPosition, pos, deltaSpeed);
> tr.localPosition = ClampPlayerPos(tr.localPosition,minPosClamp, maxPosClamp);
> }
> }
>}
Sorry if it's long, this is my second time using UnityAnswer and first time using the snippet [1]: http://www.youtube.com/watch?v=FPok9pNY0wY
Ok i manage to add the relative touch based on Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved and Input.touchCount from reading this http://unity3d.com/support/documentation/ScriptReference/Input.GetTouch.html
Now i just need to figure out if there is a better way to simulate the same result using mouse ins$$anonymous$$d