Skip to content

Instantly share code, notes, and snippets.

@gkagm2
Last active June 20, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkagm2/aa5f0cfd1b4425c8f4199e517fa28550 to your computer and use it in GitHub Desktop.
Save gkagm2/aa5f0cfd1b4425c8f4199e517fa28550 to your computer and use it in GitHub Desktop.
RaycastAll code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class csRaycast : MonoBehaviour {
private float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float amtMove = speed * Time.deltaTime;
float hor = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * hor * amtMove);
//DrawRay
Debug.DrawRay(transform.position, transform.forward * 8, Color.red);
//Raycast
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 8))
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRayCastEx : MonoBehaviour {
public GameObject bullet;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Mouse0))
{
//Input.mousePosition : 현재 마우스 위치를 픽셀 좌표로 나타냅니다.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Debug.Log("hit!!" + hit.transform.name);
//hit.normal : 법선, hit.point : 충돌이 일어난 부분의 월드 포지션
Instantiate(bullet, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRayCastEx : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Debug.Log("hit!!");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyScript : MonoBehaviour {
Camera mainCamera;
// Use this for initialization
void Start () {
mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
if (transform.IsChildOf(hit.transform))
{
print("Child OK");
}
else
{
print("Child NO");
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class csRaycastAll : MonoBehaviour {
float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float amtMove = speed * Time.deltaTime;
float hor = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * hor * amtMove);
//DrawRay
Debug.DrawRay(transform.position, transform.forward * 8, Color.red);
//RaycastAll
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, transform.forward, 8.0f);
for(int i= 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
Debug.Log(hit.collider.gameObject.name);
}
}
}
GameObject Shooting = Instantiate(GunShot, rayHit.point, Quaternion.LookRotation(rayHit.normal));
Instantiate(GunShot, rayHit.point, Quaternion.LookRotation(rayHit.normal));
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRay : MonoBehaviour {
public Camera mainCamera;
Vector3 hitPos = Vector3.zero;
RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Mouse1))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
hitPos = hit.point;
Debug.Log(hitPos);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment