so I was working on recoils system for my game, everything worked fine so I closed my project and did something else but when I came back and opened my project it suddenly decided not to work, so I removed it from the object and then added it again, it then worked just fine then I decided to do one thing disable and enable it, it didn’t work after I did that. so I really don’t know what’s happening if someone could help me that would be greatly appreciated. here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunRecoil : MonoBehaviour
{
public Transform Camera;
public float recoilMin, recoilMax;
bool canHold, canUseRecoil;
float timeBetweenShots;
void Start()
{
canUseRecoil = true;
}
void Update()
{
canHold = GetComponent<GunScript>().canHold;
timeBetweenShots = GetComponent<GunScript>().timeBetweenShots;
canUseRecoil = GetComponent<GunScript>().canShoot;
if (canHold == true && Input.GetMouseButton(0) && canUseRecoil == true || canHold == false && Input.GetMouseButtonDown(0) && canUseRecoil == true)
Recoil();
}
void Recoil()
{
float recoil = Random.Range(-recoilMin, -recoilMax);
Camera.transform.GetComponent<playerLook>().xRotation += recoil;
}
In order to unravel the problem you are having, you must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
I forgot I did a recoil system a few years back. I pulled it up, blew the cowbwebs off it and made a fresh export. Enjoy!
Full package attached. This is the recoil driver script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RecoilSequencer : MonoBehaviour
{
[Header( "Make the curve domains from 0 to 1.")]
[Header( "Output value is degrees of deflection.")]
[Header( "Adjust time interval separately below.")]
public AnimationCurve RecoilUp;
public AnimationCurve RecoilRight;
[Header( "How long is entire recoil sequence?")]
public float TimeInterval = 0.25f;
[Header( "Which object is having its .localRotation driven.")]
public Transform RecoilPivot;
// @kurtdekker
float recoiling;
public void RecoilBegin()
{
if (recoiling == 0)
{
recoiling = Time.deltaTime;
}
}
void DriveRecoil(float fraction)
{
float up = RecoilUp.Evaluate(fraction);
float right = RecoilRight.Evaluate(fraction);
// special number to FORCE you to have zero output when fraction is zero,
// to keep you from making borked curves and wondering WTF.
if (fraction == 0)
{
up = 0;
right = 0;
}
up = -up; // convenience
RecoilPivot.localRotation = Quaternion.Euler(up, right, 0);
}
void Update ()
{
if (recoiling > 0)
{
float fraction = recoiling / TimeInterval;
recoiling += Time.deltaTime;
if (recoiling > TimeInterval)
{
recoiling = 0;
fraction = 0; // return to time = 0
}
DriveRecoil(fraction);
}
}
}