Но происходит просто эффект тряски, но сама камера не меняет градус поворота по X
Recoil.cs
Синтаксис:
Используется csharp
public void recoil(GameObject cam, float x, float y, float z,GameObject gun)
{
if (Input.GetMouseButton(0))
{
if (gun.transform.parent != null)
{
if (gun.activeSelf == true)
{
if (gun.GetComponent<Gun>().shootState != Gun.ShootState.Reloading && gun.GetComponent<Gun>().shootState != Gun.ShootState.NoAmmo)
{
Transform camTransform = cam.GetComponent<Transform>();
camTransform.rotation *= Quaternion.Euler(x, 0, 0);
}
else if (gun.GetComponent<Gun>().shootState == Gun.ShootState.Reloading && gun.GetComponent<Gun>().shootState == Gun.ShootState.NoAmmo)
{
}
}
else if (gun.activeSelf == false)
{
}
}
}
}
{
if (Input.GetMouseButton(0))
{
if (gun.transform.parent != null)
{
if (gun.activeSelf == true)
{
if (gun.GetComponent<Gun>().shootState != Gun.ShootState.Reloading && gun.GetComponent<Gun>().shootState != Gun.ShootState.NoAmmo)
{
Transform camTransform = cam.GetComponent<Transform>();
camTransform.rotation *= Quaternion.Euler(x, 0, 0);
}
else if (gun.GetComponent<Gun>().shootState == Gun.ShootState.Reloading && gun.GetComponent<Gun>().shootState == Gun.ShootState.NoAmmo)
{
}
}
else if (gun.activeSelf == false)
{
}
}
}
}
На всякий случай, скрипт поворота камеры:
CameraRotation.cs
Синтаксис:
Используется csharp
public class CameraRotation : MonoBehaviour
{
[Header("Настройки камеры")]
[SerializeField]
public float sensitivity = 5.0f;
[SerializeField]
public float smoothing = 2.0f;
// the chacter is the capsule
public double maxRotationX = 80.0;
public float _MRotationX = 1;
public GameObject character;
// get the incremental value of mouse moving
private Vector2 mouseLook;
// smooth the mouse moving
private Vector2 smoothV;
private Quaternion camRotation;
// Use this for initialization
void Start()
{
camRotation = transform.localRotation;
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
// md is mosue delta
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
// the interpolated float result between the two float values
//Get X value and limit it
camRotation.x += Input.GetAxis("Mouse Y") * smoothing * (-1);
camRotation.y += Input.GetAxis("Mouse X") * smoothing;
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
// incrementally add to the camera look
mouseLook += smoothV;
camRotation.x = Mathf.Clamp(camRotation.x, -70, 80);
// vector3.right means the x-axis
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
transform.localRotation = Quaternion.Euler(camRotation.x, 0, 0);
}
}
{
[Header("Настройки камеры")]
[SerializeField]
public float sensitivity = 5.0f;
[SerializeField]
public float smoothing = 2.0f;
// the chacter is the capsule
public double maxRotationX = 80.0;
public float _MRotationX = 1;
public GameObject character;
// get the incremental value of mouse moving
private Vector2 mouseLook;
// smooth the mouse moving
private Vector2 smoothV;
private Quaternion camRotation;
// Use this for initialization
void Start()
{
camRotation = transform.localRotation;
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
// md is mosue delta
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
// the interpolated float result between the two float values
//Get X value and limit it
camRotation.x += Input.GetAxis("Mouse Y") * smoothing * (-1);
camRotation.y += Input.GetAxis("Mouse X") * smoothing;
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
// incrementally add to the camera look
mouseLook += smoothV;
camRotation.x = Mathf.Clamp(camRotation.x, -70, 80);
// vector3.right means the x-axis
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
transform.localRotation = Quaternion.Euler(camRotation.x, 0, 0);
}
}
Так же если можете подскажите как наиболее качественее сделать отдачу.