При нажатии левой кнопки мыши камера поворачивается на рандомный x и y,потом пускается raycast.
Таким образом я реализовал разброс, НО, после того как камера повернулась на рандомные значения, мне нужно вернуть её обратно на начальную позицию.
Подскажите как сделать это!
Вот скрипт:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RifleB : MonoBehaviour {
private Camera _camera;
void Start()
{
_camera = GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.Rotate(Random.Range(-2, 2), Random.Range(-2, 2), 0);
Vector3 point = new Vector3(
_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
StartCoroutine(SphereIndicator(hit.point));
transform.Rotate(0, 0, 0);
}
}
}
private IEnumerator SphereIndicator(Vector3 pos)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(sphere);
}
}
using System.Collections.Generic;
using UnityEngine;
public class RifleB : MonoBehaviour {
private Camera _camera;
void Start()
{
_camera = GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.Rotate(Random.Range(-2, 2), Random.Range(-2, 2), 0);
Vector3 point = new Vector3(
_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
StartCoroutine(SphereIndicator(hit.point));
transform.Rotate(0, 0, 0);
}
}
}
private IEnumerator SphereIndicator(Vector3 pos)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(sphere);
}
}