Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Smooth Rotation

Discussion in 'Scripting' started by ARares, Jul 17, 2016.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I tried to make a recoil scipt when is soot the FirstPersonCharacter change rotation with -0.1f, and i want to be smooth, but is isn't working. Someone can help me?

    Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RecoilPG : MonoBehaviour {
    5.  
    6.  
    7.     private float counter = 0;
    8.     public float delayTime = 0.11f;
    9.  
    10.     public GameObject FPC; //FirstPersonCharacter
    11.  
    12.     void Start ()
    13.     {
    14.  
    15.     }
    16.    
    17.     void Update ()
    18.     {
    19.         if (Input.GetKey(KeyCode.Mouse0) && counter > delayTime)
    20.         {
    21.             counter = 0;
    22.             FPC.transform.Rotate(0 -0.1f, 0, 0);
    23.         }
    24.  
    25.         counter += Time.deltaTime;
    26.     }
    27. }
    28.  
     
  2. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    I am not much of a programmer, but I found this script that shakes the screen:

    https://gist.github.com/ftvs/5822103

    I modified it for what I believe to be your purpose.

    Code (CSharp):
    1. public string fireButton = "Fire1"; //Assigning the left mouse button, so you can change in the inspector.
    2.     public  Transform FPC;
    3.     public float recoil = 1; //Amount of recoil
    4.     public float delayTime = 0.11f;
    5.  
    6.     Vector3 originalPos;
    7.  
    8.     private float counter = 0;
    9.  
    10.  
    11.     void Awake()
    12.     {
    13.         if (FPC == null)
    14.         {
    15.             FPC = GetComponent(typeof(Transform)) as Transform;
    16.         }
    17.    
    18.     }
    19.  
    20.     void OnEnable()
    21.     {
    22.         originalPos = FPC.localEulerAngles; //Sets the original rotation of your object, so as to return
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.      
    28.         if (Input.GetButton(fireButton) && counter > delayTime)
    29.         {
    30.             counter = 0;
    31.             FPC.localEulerAngles = originalPos + Vector3.right * recoil;
    32.             /*FPC.localEulerAngles = originalPos + Random.insideUnitSphere * recoil;
    33.             This is the modified orgiginal shaking. It rotates in a random location*/
    34.         }
    35.         else
    36.         {
    37.             FPC.localEulerAngles = originalPos; //resets rotation to original
    38.         }
    39.         counter += Time.deltaTime;
    40.     }
    This code is designed to be attached to a child. You will not be able to rotate the object that this script is attached to, and I do not know how to change it so you can.

    I am not sure what you mean by smoothing, but a firearm has rapid and violent recoil. Check out some youtube videos on firearm recoil.

    Here is a good video explaining recoil:
     
    Last edited: Jul 18, 2016
  3. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    is not what i want, but thx, i want a script when i press Fire1 the camera rotation x to decrease with 0.1
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The steps to this are a little more complicated. First, in the FPS, which I assume is the Camera/CharacterController. In this, you need to have a recoil offset.

    The script you have here, simply controls the recoil offset feeding it to the FPS controller.

    Furthermore... you should have this in a Coroutine, because there isn't much sense to apply this when it is not active.

    Code (csharp):
    1.     IEnumerator Recoil(){
    2.         // the maximum recoil amount
    3.         float recoilMax = -0.1f;
    4.         // the time we started our shot
    5.         float recoilStartTime = Time.time;
    6.         // the time when the recoil will be at its highest
    7.         float recoilUpTime = Time.time + delayTime * 0.25f;
    8.         // the time we are completed with the recoild
    9.         float recoilReturnTime = Time.time + delayTime;
    10.         // use this when calculating the amount of time passed.
    11.         float amount = 0;
    12.  
    13.         FPCScript fpcScript = FPC.getComponent<FPCScript>();
    14.    
    15.         // lerp the time until we get our max recoil amount
    16.         // 0.0 to 0.25 of the recoil delayTime
    17.         while(Time.time < recoilMaxTime){
    18.             // use the start time to the up time for values
    19.             amount = Mathf.InverseLerp(recoilStartTime, recoilUpTime, Time.time);
    20.             // use this amount to calculate from 0 to the recoil amount
    21.             fpcScript.recoilOffset = Vector3.right * Math.Lerp(0, recoilMax, amount);
    22.             // wait until next frame
    23.             yield return null;
    24.         }
    25.  
    26.         // lerp the time from the max recoil to the end of the delay.
    27.         // 0.25 to 1 of the recoil delayTime
    28.         while (Time.time < recoilReturnTime) {
    29.             // use the up time to the return time to get the value you need.
    30.             amount = Mathf.InverseLerp(recoilUpTime, recoilReturnTime, Time.time);
    31.             // use this amount to calculate from recoil amount to 0
    32.             fpcScript.recoilOffset = Vector3.right * Math.Lerp(recoilMax, 0, amount);
    33.             // wait until next frame
    34.             yield return null;
    35.         }
    36.  
    37.     }
    After you set the camera's location and position, and movement, you need to set the camera to rotate using recoilOffset
     
  5. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I traied this but i have that errors:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RecoilPG : MonoBehaviour {
    5.    
    6.     void Update ()
    7.     {
    8.         if (Input.GetKey(KeyCode.Mouse0))
    9.         {
    10.             StartCoroutine(Recoil);
    11.         }
    12.     }
    13.  
    14.     IEnumerator Recoil()
    15.     {
    16.         // the maximum recoil amount
    17.         float recoilMax = -0.1f;
    18.         // the time we started our shot
    19.         float recoilStartTime = Time.time;
    20.         // the time when the recoil will be at its highest
    21.         float recoilUpTime = Time.time + delayTime * 0.25f;
    22.         // the time we are completed with the recoild
    23.         float recoilReturnTime = Time.time + delayTime;
    24.         // use this when calculating the amount of time passed.
    25.         float amount = 0;
    26.  
    27.         FPCScript fpcScript = FPC.getComponent<FPCScript>();
    28.  
    29.         // lerp the time until we get our max recoil amount
    30.         // 0.0 to 0.25 of the recoil delayTime
    31.         while (Time.time < recoilMaxTime)
    32.         {
    33.             // use the start time to the up time for values
    34.             amount = Mathf.InverseLerp(recoilStartTime, recoilUpTime, Time.time);
    35.             // use this amount to calculate from 0 to the recoil amount
    36.             fpcScript.recoilOffset = Vector3.right * Math.Lerp(0, recoilMax, amount);
    37.             // wait until next frame
    38.             yield return null;
    39.         }
    40.  
    41.         // lerp the time from the max recoil to the end of the delay.
    42.         // 0.25 to 1 of the recoil delayTime
    43.         while (Time.time < recoilReturnTime)
    44.         {
    45.             // use the up time to the return time to get the value you need.
    46.             amount = Mathf.InverseLerp(recoilUpTime, recoilReturnTime, Time.time);
    47.             // use this amount to calculate from recoil amount to 0
    48.             fpcScript.recoilOffset = Vector3.right * Math.Lerp(recoilMax, 0, amount);
    49.             // wait until next frame
    50.             yield return null;
    51.         }
    52.  
    53.     }
    54. }
    55.  
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    so, there are 2 actual problems...

    1), when you start a coroutine, it should look like this:

    StartCoroutine(MyCoroutineName()); // note the extra parentheses....

    2) I wrote a reference to a script that doesn't exist. FPCScript should be replaced with your controller script.
     
  7. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    But here, i changet with :"FirstPersonController fpcScript = FPC.getComponent<FirstPersonController>();", but where is "fpcScript"?
    FirstPersonController fpcScript = FPC.getComponent<FirstPersonController>();
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    fpcScript is a variable it is of type "FirstPersonController"

    Where did you get the FirstPersonController from?
     
  9. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    Unity Characters
     
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Under standard Assets, I see the FirstPersonController. In the Update, the first line is RotateView()

    At about 239, it calls m_MouseLook.LookRotation(transform, m_Camera.transform)

    Which sets the rotation of the camera to the user. So here is the change I did there.....

    Code (csharp):
    1.         public Vector3 recoilOffset;
    2.         private void RotateView()
    3.         {
    4.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    5.             m_Camera.transform.Rotate(recoilOffset);
    6.         }
    Now, I tinkered with the script I did, and I had a few typos, so I corrected those real quick...
    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class Recoil : MonoBehaviour {
    8.     public GameObject FPC;
    9.     private float delayTime = 0.5f;
    10.     private bool recoiling = false;
    11.  
    12.     void Update() {
    13.         if (!recoiling && Input.GetMouseButton(0))
    14.             StartCoroutine(Recoil());
    15.     }
    16.  
    17.     IEnumerator Recoil()
    18.     {
    19.         recoiling = true;
    20.         // the maximum recoil amount
    21.         float recoilMax = -5f;
    22.         // the time we started our shot
    23.         float recoilStartTime = Time.time;
    24.         // the time when the recoil will be at its highest
    25.         float recoilUpTime = Time.time + delayTime * 0.25f;
    26.         // the time we are completed with the recoild
    27.         float recoilReturnTime = Time.time + delayTime;
    28.         // use this when calculating the amount of time passed.
    29.         float amount = 0;
    30.  
    31.         FirstPersonController fpcScript = FPC.GetComponent<FirstPersonController>();
    32.  
    33.         // lerp the time until we get our max recoil amount
    34.         // 0.0 to 0.25 of the recoil delayTime
    35.         while (Time.time < recoilUpTime)
    36.         {
    37.             // use the start time to the up time for values
    38.             amount = Mathf.InverseLerp(recoilStartTime, recoilUpTime, Time.time);
    39.             // use this amount to calculate from 0 to the recoil amount
    40.             fpcScript.recoilOffset = Vector3.right * Mathf.Lerp(0, recoilMax, amount);
    41.             // wait until next frame
    42.             yield return null;
    43.         }
    44.  
    45.         // lerp the time from the max recoil to the end of the delay.
    46.         // 0.25 to 1 of the recoil delayTime
    47.         while (Time.time < recoilReturnTime)
    48.         {
    49.             // use the up time to the return time to get the value you need.
    50.             amount = Mathf.InverseLerp(recoilUpTime, recoilReturnTime, Time.time);
    51.             // use this amount to calculate from recoil amount to 0
    52.             fpcScript.recoilOffset = Vector3.right * Mathf.Lerp(recoilMax, 0, amount);
    53.             // wait until next frame
    54.             yield return null;
    55.         }
    56.         recoiling = false;
    57.     }
    58.  
    59. }
     
  11. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I have that error when i shoot:



    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class RecoilPG : MonoBehaviour {
    8.  
    9.     public GameObject FPC;
    10.     private float delayTime = 0.5f;
    11.     private bool recoiling = false;
    12.  
    13.     void Update()
    14.     {
    15.         if (!recoiling && Input.GetMouseButton(0))
    16.             StartCoroutine(Recoil());
    17.     }
    18.  
    19.     IEnumerator Recoil()
    20.     {
    21.         recoiling = true;
    22.         // the maximum recoil amount
    23.         float recoilMax = -5f;
    24.         // the time we started our shot
    25.         float recoilStartTime = Time.time;
    26.         // the time when the recoil will be at its highest
    27.         float recoilUpTime = Time.time + delayTime * 0.25f;
    28.         // the time we are completed with the recoild
    29.         float recoilReturnTime = Time.time + delayTime;
    30.         // use this when calculating the amount of time passed.
    31.         float amount = 0;
    32.  
    33.         FirstPersonController fpcScript = FPC.GetComponent<FirstPersonController>();
    34.  
    35.         // lerp the time until we get our max recoil amount
    36.         // 0.0 to 0.25 of the recoil delayTime
    37.         while (Time.time < recoilUpTime)
    38.         {
    39.             // use the start time to the up time for values
    40.             amount = Mathf.InverseLerp(recoilStartTime, recoilUpTime, Time.time);
    41.             // use this amount to calculate from 0 to the recoil amount
    42.             fpcScript.recoilOffset = Vector3.right * Mathf.Lerp(0, recoilMax, amount);
    43.             // wait until next frame
    44.             yield return null;
    45.         }
    46.  
    47.         // lerp the time from the max recoil to the end of the delay.
    48.         // 0.25 to 1 of the recoil delayTime
    49.         while (Time.time < recoilReturnTime)
    50.         {
    51.             // use the up time to the return time to get the value you need.
    52.             amount = Mathf.InverseLerp(recoilUpTime, recoilReturnTime, Time.time);
    53.             // use this amount to calculate from recoil amount to 0
    54.             fpcScript.recoilOffset = Vector3.right * Mathf.Lerp(recoilMax, 0, amount);
    55.             // wait until next frame
    56.             yield return null;
    57.         }
    58.         recoiling = false;
    59.     }
    60. }
    61.