via(11): 마우스로 obj원하는 위치에 놓기
마우스로 obj원하는 위치에 놓기
- insObj가 정의가 되지않았다면
- 마우스로 클릭시 obj를 소환한다.
- insObj가 정의가 되었다면, if(insObj != null)
- 그 소환한 insobj를 마우스가 닿는 지점마다로 위치를 옮긴다.
- insObj.transform.position = 마우스가 닿는 지점이다.
땅에 파묻혀있으면 이상하니까 좀 올려주세요
- 올려주세요 = 마우스가 닿는 지점에서 y축으로 + 0.5f
{
insObj.transform.position = new Vector3(hitPoint.point.x, hitPoint.point.y+0.5f, hitPoint.point.z);
}
마우스를 클릭하면 그자리에 냅둬주세요
if (Input.GetMouseButtonDown(0))
{
insObj = null;
}
최종
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class Player : MonoBehaviour
{
GameObject insObj;
public int _moveSpeed;
public int _addforce = 100;
public Transform[] cameraTr;
int cameraNum;
int _jumpNum;
public GameObject obj;
public GameObject pefabs;
public int _rotSpeed;
void Start()
{
cameraNum = 0;
_jumpNum = 0;
}
void Update()
{
Move();
Jump();
See();
RayPoint();
}
void RayPoint()
{
if(insObj == null)
{
if (Input.GetMouseButtonDown(0))
{
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rHit;
if (Physics.Raycast(r, out rHit, 100))
{
//GameObject insObj = Instantiate(obj.gameObject, rHit.point, Quaternion.Euler(0,0,0));
insObj = Instantiate(obj.gameObject);
}
}
}
else
{
Ray raypointer = new Ray();
raypointer = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitPoint;
if(Physics.Raycast(raypointer, out hitPoint, Mathf.Infinity))
{
insObj.transform.position = new Vector3(hitPoint.point.x, hitPoint.point.y+0.5f, hitPoint.point.z);
}
if (Input.GetMouseButtonDown(0))
{
insObj = null;
}
}
}
//이동하는 함수
void Move()
{
if (Input.GetKey(KeyCode.UpArrow))
{
this.gameObject.transform.Translate(0, 0, _moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow))
{
this.gameObject.transform.Translate(0, 0, -_moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
this.gameObject.transform.Rotate(0, -_rotSpeed * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
this.gameObject.transform.Rotate(0, _rotSpeed * Time.deltaTime, 0);
}
}
//점프하는 함수
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 0)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 2)
return;
}
//카메라 시점변경
void See()
{
if (Input.GetKeyDown(KeyCode.V))
{
if (cameraNum == 0)
{
Camera.main.transform.position = cameraTr[1].transform.position;
Camera.main.transform.rotation = cameraTr[1].transform.rotation;
cameraNum = 1;
}
else if (cameraNum == 1)
{
Camera.main.transform.position = cameraTr[0].transform.position;
Camera.main.transform.rotation = cameraTr[0].transform.rotation;
cameraNum = 0;
}
}
}
void OnCollisionEnter(Collision collision)
{
_jumpNum = 0;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Obstacle")
{
print("부딪힘!!");
}
if (other.tag == "Goal")
{
print("종료");
}
}
}
tip
- transform이나 게임오브젝트 등 유니티 내부 변수를 활용할 경우에는 =!null,activeself 을 많이 사용함
댓글남기기