Advertisement
Guest User

surface detection

a guest
Apr 28th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SurfaceDetection : MonoBehaviour
  6. {
  7.     public bool IsSurface;
  8.     Vector3 initialPos;
  9.  
  10.     private void Start()
  11.     {
  12.         initialPos = transform.localPosition;
  13.     }
  14.  
  15.     void Update()
  16.     {
  17.         if (Input.GetKeyDown(KeyCode.K))
  18.         {
  19.             if (IsSurface)
  20.             {
  21.                 transform.localPosition += new Vector3(10f, 0f, 0f);
  22.                 CheckGround();
  23.             }
  24.             else
  25.             {
  26.                 Debug.Log("is surface false");
  27.                 transform.localPosition = new Vector3(initialPos.x, 0f, 0f);
  28.                 transform.localPosition += new Vector3(0f, 0f, 5f);
  29.                 IsSurface = true;
  30.             }
  31.         }
  32.     }
  33.  
  34.     void CheckGround()
  35.     {
  36.         Debug.Log("check ground");
  37.         RaycastHit Hit;
  38.  
  39.         if (Physics.Raycast(transform.localPosition, Vector3.down, out Hit, Mathf.Infinity))
  40.         {
  41.             IsSurface = true;
  42.             Debug.Log(Hit.collider.name);
  43.         }
  44.         else
  45.         {
  46.             IsSurface = false;
  47.             transform.localPosition -= new Vector3(10f, 0f, 0f);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement