Search Unity

Issue with camera script

Discussion in '2D' started by chicagoGSH, Oct 17, 2014.

  1. chicagoGSH

    chicagoGSH

    Joined:
    Jul 5, 2014
    Posts:
    3
    Im having an issue with my camera script (posted here as well)

    There is a jitter/shaking that happens when my player lands from a double jumps. The reason behind double jump (vs single jump) is that the camera actually moves up. Where as a normal jump the camera stays grounded to the bottom wall game object (basically my half screen buffer doesnt get hit... 'delta' I think).

    How can I get rid of this shaking/jitter? To better explain "shaking/jitter" I would say its like a giant hitting the ground and causing it to shake (go up and down).


    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6.  
    7. namespace TikiBeeGame {
    8. class EndlessLevelCameraController : MonoBehaviour {
    9. //public float speed = 2f;
    10. public float dampTime = 0.15f;
    11. private Vector3 velocity = Vector3.zero;
    12. public Transform rightEndMarker = null;
    13. public Transform leftEndMarker = null;
    14. public Transform topEndMarker = null;
    15. public Transform bottomEndMarker = null;
    16. public bool useFixedUpdate = false;
    17.  
    18. private float leftWall = 0;
    19. private float rightWall = 0;
    20. private float bottomWall = 0;
    21. private float topWall = 0;
    22.  
    23. void Start() {
    24.  
    25. }
    26. void updateWalls() {
    27.  
    28. leftWall = camera.WorldToScreenPoint(leftEndMarker.position).x;
    29. rightWall = camera.WorldToScreenPoint(rightEndMarker.position).x - Screen.width;
    30. bottomWall = camera.WorldToScreenPoint(bottomEndMarker.position).y;
    31. topWall = camera.WorldToScreenPoint(topEndMarker.position).y - Screen.height;
    32. }
    33.  
    34.  
    35. void LateUpdate() {
    36. if (!useFixedUpdate)
    37. updateCameraPosition();
    38. }
    39.  
    40.  
    41. void FixedUpdate() {
    42. if (useFixedUpdate)
    43. updateCameraPosition();
    44. }
    45. // Update is called once per frame
    46. void updateCameraPosition() {
    47. updateWalls();
    48. if (PreferencesManager.CURRENT_PLAYER.transform) {
    49. Vector3 delta = PreferencesManager.CURRENT_PLAYER.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); //(new Vector3(0.5, 0.5, point.z));
    50. Vector3 destination = (transform.position + delta);
    51.  
    52. //destination = findBestPlacement(destination);
    53. if (destination.x > rightWall) {
    54. destination = new Vector3(rightWall, destination.y, 0);
    55. }
    56. if (destination.x < leftWall) {
    57. destination = new Vector3(leftWall, destination.y, 0);
    58. }
    59. if (destination.y > topWall) {
    60. destination = new Vector3(destination.x, topWall, 0);
    61. }
    62. if (destination.y < bottomWall) {
    63. destination = new Vector3(destination.x, bottomWall, 0);
    64. }
    65. }
    66.  
    67. // here to always set the Z
    68. destination = new Vector3(destination.x, destination.y, transform.position.z);
    69.  
    70. transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
    71. }
    72. }
    73.  
    p.s.
    I also have been looking for a tutorial on the Achievement and Leaderboard api's if anyone has a link
     
    Last edited: Oct 23, 2014