This is only about implementation ,UI The interface is considered by itself
1. lead
public class PlayerMove : MonoBehaviour
{
private Rigidbody player;
public float speed = 5;
private int groundLayerIndex = -1;
public GameObject FireBtn;
void Start()
{
player = GetComponent<Rigidbody>();
groundLayerIndex = LayerMask.GetMask("ground");
}
void FixedUpdate()
{
// Controlling person WSAD Key movement
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
player.MovePosition( transform.position + new
Vector3(h,0,v)*speed*Time.deltaTime);
// Prevent people from turning due to X-ray detection when clicking the button
RectTransform rctTr = FireBtn.GetComponent<RectTransform>();
// If Canvas by Overlay No need to pass Camera parameter , Otherwise it needs to be transmitted Camera
//Canvas canvas = GetComponent<Canvas>();
//Camera camera = canvas.renderMode == RenderMode.ScreenSpaceOverlay
? null : Camera.main;
bool isContain =
RectTransformUtility.RectangleContainsScreenPoint(rctTr, Input.mousePosition,
null);
if (isContain)
{
// Click on the fire button
return;
}
else
{
// Click the left mouse button , Put the character in the click direction
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 100, groundLayerIndex))
{
Vector3 target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(Vector3.Lerp(transform.position, target,
3f * Time.deltaTime));
}
}
}
}
}
2. bullet
public class BoltMove : MonoBehaviour
{
// Self motion control of bullet body
private Rigidbody bolt;
public float speed = 10;
void Start()
{
bolt = GetComponent<Rigidbody>();
bolt.velocity = transform.forward * speed;
}
}
3. FireStarter
public class FireControl : MonoBehaviour
{
public Transform boltSpwan;
public GameObject bolt;
// Click event of fire button
public void Fire()
{
Instantiate(bolt,boltSpwan.position,boltSpwan.rotation);
}
}
Technology
Daily Recommendation