Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6. public GameObject Spot;
  7. public float Speed=10.0f;
  8. public float rotation = 200.0f;
  9. public float PlayerHeight = 2;
  10. // Update is called once per frame
  11. void Update () {
  12. var x = Input.GetAxis("Horizontal") * Time.deltaTime * rotation;
  13. var z = Input.GetAxis("Vertical") * Time.deltaTime * Speed;
  14.  
  15. transform.Rotate(0, x, 0);
  16. transform.Translate(0, 0, z);
  17.  
  18. if (Input.GetKeyDown(KeyCode.Mouse0))
  19. {
  20. getTerrainPos();
  21.  
  22. }
  23.  
  24. }
  25. void LateUpdate()
  26. {
  27. Vector3 pos = transform.position;
  28. pos.y = Terrain.activeTerrain.SampleHeight(transform.position)+ PlayerHeight;
  29. transform.position = pos;
  30. }
  31. private void getTerrainPos()
  32. {
  33. Vector3 infront;
  34. infront = transform.position + transform.forward;
  35. infront.y = Terrain.activeTerrain.SampleHeight(infront) + 0.1f;
  36. if (Mathf.FloorToInt(infront.x) == Mathf.RoundToInt(infront.x))//if = then round down else round up
  37.  
  38. infront.x = Mathf.FloorToInt(infront.x);
  39.  
  40. else
  41.  
  42. infront.x = Mathf.CeilToInt(infront.x);
  43.  
  44. if (Mathf.FloorToInt(infront.z) == Mathf.RoundToInt(infront.z))//if = then round down else round up
  45.  
  46. infront.z = Mathf.FloorToInt(infront.z);
  47.  
  48. else
  49.  
  50. infront.z = Mathf.CeilToInt(infront.z);
  51. Instantiate(Spot,infront,Quaternion.identity);
  52. Debug.Log(infront);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement