Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- var moveSpeed : float = 15;//character move speed
- var mainChar : GameObject;//main character/player object
- var mainCam : GameObject;//main camera object
- var mainCharLoc : Vector3;//location of main character object, later established
- var mainCamLoc : Vector3;//location of main camera object, later established
- var deadZone : float = .1;//deadzone for camera follow
- var camSpeedCoef : float = 10;//camera move speed
- function Start () {
- if(mainChar==null)//finds the game objects
- mainChar = GameObject.FindWithTag("Player");
- if(mainCam==null)
- mainCam = GameObject.FindWithTag("MainCamera");
- }
- function Update () {
- //setting up controls for the character
- var charVertTrans : float = Input.GetAxis("Vertical") * moveSpeed;
- var charHoriTrans : float = Input.GetAxis("Horizontal") * moveSpeed;
- charVertTrans *= Time.deltaTime;
- charHoriTrans *= Time.deltaTime;
- mainChar.transform.Translate(charHoriTrans,charVertTrans,0);
- //reads the position of the objects
- mainCharLoc = mainChar.transform.position;
- mainCamLoc = mainCam.transform.position;
- //creates the floats later used for the camera transform
- var camVertTrans : float;
- var camHoriTrans : float;
- //camera movement speed arguments
- //this is the dead zone of the character for the horizontal dimension
- //basically, if the camera is close enough to the character horizontally, nothing happens
- if(mainCamLoc.x < (mainCharLoc.x + deadZone) && mainCamLoc.x > (mainCharLoc.x - deadZone))
- camHoriTrans = 0;
- //if the camera is to the right the dead zone, it is supposed to move (left) towards the character
- //in relation to how far away it is
- //(basically the distance between them to the power of the camera speed coef)
- else if(mainCamLoc.x > (mainCharLoc.x + deadZone))
- camHoriTrans = -1 * Mathf.Pow((mainCamLoc.x - mainCharLoc.x),camSpeedCoef);
- //same thing only the camera is to the right of the deadzone and moves left
- else if(mainCamLoc.x < (mainCharLoc.x - deadZone))
- camHoriTrans = 1 * Mathf.Pow((mainCharLoc.x - mainCamLoc.x),camSpeedCoef);
- //same thing but for the vertical dimension
- if(mainCamLoc.y < (mainCharLoc.y + deadZone) && mainCamLoc.y > (mainCharLoc.y - deadZone))
- camVertTrans = 0;
- else if(mainCamLoc.y > (mainCharLoc.y + deadZone))
- camVertTrans = -1 * Mathf.Pow((mainCamLoc.y - mainCharLoc.y),camSpeedCoef);
- else if(mainCamLoc.y < (mainCharLoc.y - deadZone))
- camVertTrans = 1 * Mathf.Pow((mainCharLoc.y - mainCamLoc.y),camSpeedCoef);
- //moves the camera
- mainCam.transform.Translate(camHoriTrans,camVertTrans,0);
- //even though there isnt a direct correlation between these,
- //they read nearly the same (to the thousandths place) even if I change the coef drastically
- //and so my camera almost follows the char to the T aside from the dead zone
- print("Cam: " + camHoriTrans + " Char: " + charHoriTrans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement