UnityProjekt/Assets/Scripts/WeaponFireController.cs

49 lines
1.7 KiB
C#
Raw Normal View History

2020-11-06 20:20:17 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class WeaponFireController : MonoBehaviour
{
public GameObject prefab;
2020-11-06 20:20:17 +00:00
public Transform firePoint;
private ObjectPool _pool;
private ParticleSystem _particle;
2020-11-06 20:20:17 +00:00
// Start is called before the first frame update
void Start()
{
_pool = new ObjectPool(prefab, 10);
_particle = firePoint.GetComponent<ParticleSystem>();
2020-11-06 20:20:17 +00:00
}
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
var theRocket = _pool.GetObject();
2020-11-06 20:20:17 +00:00
var rocketTransform = theRocket.transform;
rocketTransform.position = firePoint.position;
2020-11-19 23:48:50 +00:00
//https://forum.unity.com/threads/look-rotation-2d-equivalent.611044/
2020-11-19 23:48:50 +00:00
// vector from this object towards the target location
var vectorToTarget = Camera.main.ScreenToWorldPoint(Input.mousePosition) - rocketTransform.position;
// rotate that vector by 90 degrees around the Z axis
Vector3 rotatedVectorToTarget = Quaternion.Euler(0, 0, 90) * vectorToTarget;
// get the rotation that points the Z axis forward, and the Y axis 90 degrees away from the target
// (resulting in the X axis facing the target)
Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, rotatedVectorToTarget);
rocketTransform.rotation = targetRotation;
theRocket.SetActive(true);
2020-11-06 20:20:17 +00:00
}
if (Input.GetButtonDown("Fire2"))
_particle.Play();
else if (Input.GetButtonUp("Fire2"))
_particle.Stop();
2020-11-06 20:20:17 +00:00
}
}