I have very little experience with unity and recently learned lerping. I was trying to rotate a gun model when I click on right click so that I can make it look like It’s aiming using lerping and I got the Vector3 to work but for some reason it rotates all of the time and instead of when it just rotates
Messy Code:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class GunController : MonoBehaviour
{
[Header("Gun Settings")]
public float fireRate = 0.1f;
public int clipSize = 30;
public int reservedAmmoCapacity = 270;
//Variables that change throughout code
private bool _canShoot;
public int _currentAmmoInClip;
private int _ammoInReserve;
//Aiming
public Vector3 normalLocalPosition;
public Vector3 aimingLocalPosition;
public Quaternion normalLocalRotation;
public Quaternion aimingLocalRotation;
public float aimSmoothing = 10;
[Header("Mouse Settings")]
public float mouseSensitivity = 1;
private Vector2 _currentRotation;
public float weaponSwayAmount = 10;
private void Start()
{
_currentAmmoInClip = clipSize;
_ammoInReserve = reservedAmmoCapacity;
_canShoot = true;
}
private void Update()
{
DetermineAim();
DetermineRotation();
if (Input.GetMouseButton(0) && _canShoot && _currentAmmoInClip > 0)
{
_canShoot = false;
_currentAmmoInClip--;
StartCoroutine(ShootGun());
}
else if (Input.GetKeyDown(KeyCode.R) && _currentAmmoInClip < clipSize && _ammoInReserve > 0)
{
int amountNeeded = clipSize - _currentAmmoInClip;
if (amountNeeded >= _ammoInReserve)
{
_currentAmmoInClip += _ammoInReserve;
_ammoInReserve -= amountNeeded;
}
else
{
_currentAmmoInClip = clipSize;
_ammoInReserve -= amountNeeded;
}
}
}
private void DetermineRotation()
{
Vector2 mouseAxis = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseAxis *= mouseSensitivity;
_currentRotation += mouseAxis;
_currentRotation.y = Mathf.Clamp(_currentRotation.y, -90, 90);
transform.localPosition += (Vector3)mouseAxis * weaponSwayAmount / 1000;
transform.root.localRotation = Quaternion.AngleAxis(_currentRotation.x, Vector3.up);
transform.parent.localRotation = Quaternion.AngleAxis(-_currentRotation.y, Vector3.right);
}
private void DetermineAim()
{
Vector3 targetPos = normalLocalPosition;
if (Input.GetMouseButton(1))
targetPos = aimingLocalPosition;
Quaternion desiredRotation = Quaternion.Lerp(transform.localRotation, aimingLocalRotation, Time.deltaTime * aimSmoothing);
transform.localRotation = desiredRotation;
Vector3 desiredPosition = Vector3.Lerp(transform.localPosition, targetPos, Time.deltaTime * aimSmoothing);
transform.localPosition = desiredPosition;
}
private IEnumerator ShootGun()
{
RaycastForEnemy();
yield return new WaitForSeconds(fireRate);
_canShoot = true;
}
private void RaycastForEnemy()
{
RaycastHit hit;
if (Physics.Raycast(transform.parent.position, transform.parent.forward, out hit, 1 << LayerMask.NameToLayer("Enemy")))
{
try
{
Debug.Log("Hit an Enemy!");
Rigidbody rb = hit.transform.GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.None;
rb.AddForce(transform.parent.transform.forward * 500);
}
catch
{
}
}
}
}
Any suggestions are appreciated! Thank you.