Advertisement
Guest User

CameraScript

a guest
Nov 28th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraScript : MonoBehaviour
  6. {
  7. Transform lastWall;
  8.  
  9.  
  10. public float distFromPlayer = 5;
  11. public float speed = 20;
  12. public LayerMask wallMask;
  13. public Transform player;
  14.  
  15. void Start ()
  16. {
  17.  
  18. }
  19.  
  20. void FixedUpdate ()
  21. {
  22. Vector3 targetPos = player.position - player.forward * distFromPlayer;
  23. targetPos.y = player.position.y + 3;
  24. transform.position = Vector3.Lerp(transform.position, targetPos, speed * Time.fixedDeltaTime);
  25. transform.LookAt(player.position);
  26.  
  27. RaycastHit hit;
  28. Physics.Linecast(transform.position, player.position, out hit, wallMask);
  29. Debug.DrawLine(transform.position, player.position);
  30.  
  31. if (lastWall != null && lastWall != hit.transform)
  32. {
  33. SetAlphaOnObj(lastWall, 1f);
  34. }
  35.  
  36. if(hit.transform != null)
  37. {
  38. SetAlphaOnObj(hit.transform, 0.5f);
  39.  
  40. lastWall = hit.transform;
  41. }
  42. }
  43.  
  44. void SetAlphaOnObj(transform trans, float alpha)
  45. {
  46. Material mat = trans.GetComponent<MeshRenderer>().materials[0];
  47. Color c = mat.color;
  48. c.a = alpha;
  49. mat.color = c;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement