Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. public class SpellCaster : NetworkBehaviour
  7. {
  8. [SerializeField]
  9. GameObject spellPrefab;
  10.  
  11. [SerializeField]
  12. Transform spellCastPoint;
  13.  
  14.  
  15. private Camera mainCamera;
  16.  
  17. // Use this for initialization
  18. void Start ()
  19. {
  20. mainCamera = FindObjectOfType<Camera>();
  21. }
  22.  
  23. void Reset()
  24. {
  25. spellCastPoint = transform.Find("SpellCastPoint");
  26. }
  27.  
  28. // Update is called once per frame
  29. void Update ()
  30. {
  31. if (!isLocalPlayer)
  32. return;
  33.  
  34. Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
  35. Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
  36. float rayLength;
  37.  
  38. if (groundPlane.Raycast(cameraRay, out rayLength))
  39. {
  40. Vector3 pointToLook = cameraRay.GetPoint(rayLength);
  41. Debug.DrawLine(cameraRay.origin, pointToLook, Color.red);
  42.  
  43. transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
  44. }
  45.  
  46. if (Input.GetMouseButtonDown(0))
  47. {
  48. CmdSpawnSpell();
  49. }
  50. }
  51.  
  52. [Command] // Functions for the server to deal with
  53. void CmdSpawnSpell()
  54. {
  55. GameObject instance = Instantiate(spellPrefab, spellCastPoint.position, spellCastPoint.rotation) as GameObject;
  56.  
  57. NetworkServer.Spawn(instance);
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement