using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayDemo_Shoot : MonoBehaviour
{
private Vector3 vecRayPoint;
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
//給射击位置赋值
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
vecRayPoint = hit.point;
}
//射击
GameObject goBullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
goBullet.AddComponent<Rigidbody>();
goBullet.transform.position = Camera.main.transform.position;
goBullet.GetComponent<Rigidbody>().AddForce((vecRayPoint - goBullet.transform.position) * 10f,ForceMode.Impulse);
Destroy(goBullet, 3f);
}
}
}