Advertisement
Darkhour99

camera follow issues

Oct 4th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. var moveSpeed : float = 15;//character move speed
  4. var mainChar : GameObject;//main character/player object
  5. var mainCam : GameObject;//main camera object
  6. var mainCharLoc : Vector3;//location of main character object, later established
  7. var mainCamLoc : Vector3;//location of main camera object, later established
  8. var deadZone : float = .1;//deadzone for camera follow
  9. var camSpeedCoef : float = 10;//camera move speed
  10.  
  11. function Start () {
  12.     if(mainChar==null)//finds the game objects
  13.         mainChar = GameObject.FindWithTag("Player");
  14.     if(mainCam==null)
  15.         mainCam = GameObject.FindWithTag("MainCamera");
  16. }
  17.  
  18. function Update () {
  19.     //setting up controls for the character
  20.     var charVertTrans : float = Input.GetAxis("Vertical") * moveSpeed;
  21.     var charHoriTrans : float = Input.GetAxis("Horizontal") * moveSpeed;
  22.    
  23.     charVertTrans *= Time.deltaTime;
  24.     charHoriTrans *= Time.deltaTime;
  25.    
  26.     mainChar.transform.Translate(charHoriTrans,charVertTrans,0);
  27.    
  28.     //reads the position of the objects
  29.     mainCharLoc = mainChar.transform.position;
  30.     mainCamLoc = mainCam.transform.position;
  31.    
  32.     //creates the floats later used for the camera transform
  33.     var camVertTrans : float;
  34.     var camHoriTrans : float;
  35.    
  36.     //camera movement speed arguments
  37.     //this is the dead zone of the character for the horizontal dimension
  38.     //basically, if the camera is close enough to the character horizontally, nothing happens
  39.     if(mainCamLoc.x < (mainCharLoc.x + deadZone) && mainCamLoc.x > (mainCharLoc.x - deadZone))
  40.         camHoriTrans = 0;
  41.     //if the camera is to the right the dead zone, it is supposed to move (left) towards the character
  42.     //in relation to how far away it is
  43.     //(basically the distance between them to the power of the camera speed coef)
  44.     else if(mainCamLoc.x > (mainCharLoc.x + deadZone))
  45.         camHoriTrans = -1 * Mathf.Pow((mainCamLoc.x - mainCharLoc.x),camSpeedCoef);
  46.     //same thing only the camera is to the right of the deadzone and moves left
  47.     else if(mainCamLoc.x < (mainCharLoc.x - deadZone))
  48.         camHoriTrans = 1 * Mathf.Pow((mainCharLoc.x - mainCamLoc.x),camSpeedCoef);
  49.    
  50.     //same thing but for the vertical dimension
  51.     if(mainCamLoc.y < (mainCharLoc.y + deadZone) && mainCamLoc.y > (mainCharLoc.y - deadZone))
  52.         camVertTrans = 0;  
  53.     else if(mainCamLoc.y > (mainCharLoc.y + deadZone))
  54.         camVertTrans = -1 * Mathf.Pow((mainCamLoc.y - mainCharLoc.y),camSpeedCoef);
  55.    
  56.     else if(mainCamLoc.y < (mainCharLoc.y - deadZone))
  57.         camVertTrans = 1 * Mathf.Pow((mainCharLoc.y - mainCamLoc.y),camSpeedCoef);
  58.    
  59.     //moves the camera
  60.     mainCam.transform.Translate(camHoriTrans,camVertTrans,0);
  61.    
  62.     //even though there isnt a direct correlation between these,
  63.     //they read nearly the same (to the thousandths place) even if I change the coef drastically
  64.     //and so my camera almost follows the char to the T aside from the dead zone
  65.     print("Cam: " + camHoriTrans + " Char: " + charHoriTrans);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement