Search Unity

iPhone FPS Camera Script Help

Discussion in 'Scripting' started by TheChronicPhenix, May 31, 2011.

  1. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Ok so I've been working on this for awhile but I just recently had a great idea, instead of trying to rotate it based on a timer and swipes across the screen, what about just rotating it exactly along with the touch (except dividing the number by something so that you could change the sensitivity). Well I got it working but it wasn't very smooth, so I tried multiplying the number by Time.deltaTime, but then when I tried to move the camera it spazzes out and doesn't want to work (it shakes violently back and forth, just like if you had locked the rotation but was trying to force it to rotate). But I can't figure out why thats happening. Any help? Thanks.

    Code (csharp):
    1. var touch : Touch;
    2. var offset : Vector2;
    3. var tPos : Vector2;
    4. var oPos : Vector2;
    5. var camX : Transform;
    6. var camY : Transform;
    7. var sensetivity : Vector2;
    8. var rotate : boolean = false;
    9.  
    10. function Start(){
    11. RotateCam();
    12. }
    13.  
    14. function Update () {
    15. for(var i : int = 0; i < Input.touchCount; i++){
    16. touch = Input.GetTouch(i);
    17. tPos = touch.position;
    18. oPos.x = (tPos.x - offset.x) / sensetivity.x;
    19. oPos.y = (tPos.y - offset.y) / sensetivity.y;
    20. if(oPos.x > 360){
    21. offset = touch.position;
    22. }
    23. else if(oPos.x < -360){
    24. offset = touch.position;
    25. }
    26. if(touch.phase == TouchPhase.Began){
    27. offset = touch.position;
    28. }
    29. if(touch.phase == TouchPhase.Moved){
    30. rotate = true;
    31. }
    32. if(touch.phase == TouchPhase.Ended){
    33. rotate = false;
    34. }
    35. }
    36. }
    37.  
    38. function RotateCam(){
    39. while(true){
    40. if(rotate){
    41. camY.rotation.y = oPos.x * Time.deltaTime;
    42. camY.rotation.z = 0;
    43. //camX.rotation.x = oPos.y * Time.deltaTime;
    44. //camX.rotation.z = 0;
    45. }
    46. yield;
    47. }
    48. }