Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

removing this scripts dependency on standard assets

Discussion in 'Scripting' started by PvTGreg, May 26, 2015.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Hi im currently using this script to make my camera follow the head bone of my player and it works amazing however tho i would like to remove the namespace and make it more of a "normal" script so that i can acces it easily how do i go about doing this? i tried making it use misbehavior but it just spewed errors at me saying that a lot of the variables didn't exist can anyone help me create something similar in functionality?
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets.Cameras
    5. {
    6.  
    7.     public class HeadFollow : PivotBasedCameraRig
    8.     {
    9.         [SerializeField] private float m_MoveSpeed = 3; // How fast the rig will move to keep up with target's position
    10.         [SerializeField] private float m_TurnSpeed = 1; // How fast the rig will turn to keep up with target's rotation
    11.         [SerializeField] private float m_RollSpeed = 0.2f;// How fast the rig will roll (around Z axis) to match target's roll.
    12.         [SerializeField] private bool m_FollowVelocity = false;// Whether the rig will rotate in the direction of the target's velocity.
    13.         [SerializeField] private bool m_FollowTilt = true; // Whether the rig will tilt (around X axis) with the target.
    14.         [SerializeField] private float m_SpinTurnLimit = 90;// The threshold beyond which the camera stops following the target's rotation. (used in situations where a car spins out, for example)
    15.         [SerializeField] private float m_TargetVelocityLowerLimit = 4f;// the minimum velocity above which the camera turns towards the object's velocity. Below this we use the object's forward direction.
    16.         [SerializeField] private float m_SmoothTurnTime = 0.2f; // the smoothing for the camera's rotation
    17.  
    18.         private float m_LastFlatAngle; // The relative angle of the target and the rig from the previous frame.
    19.         private float m_CurrentTurnAmount; // How much to turn the camera
    20.         private float m_TurnSpeedVelocityChange; // The change in the turn speed velocity
    21.         private Vector3 m_RollUp = Vector3.up;// The roll of the camera around the z axis ( generally this will always just be up )
    22.  
    23.         protected override void FollowTarget(float deltaTime)
    24.         {
    25.             // if no target, or no time passed then we quit early, as there is nothing to do
    26.             if (!(deltaTime > 0) || m_Target == null)
    27.             {
    28.                 return;
    29.             }
    30.  
    31.             // initialise some vars, we'll be modifying these in a moment
    32.             var targetForward = m_Target.forward;
    33.             var targetUp = m_Target.up;
    34.  
    35.             if (m_FollowVelocity && Application.isPlaying)
    36.             {
    37.                 // in follow velocity mode, the camera's rotation is aligned towards the object's velocity direction
    38.                 // but only if the object is traveling faster than a given threshold.
    39.  
    40.                 if (targetRigidbody.velocity.magnitude > m_TargetVelocityLowerLimit)
    41.                 {
    42.                     // velocity is high enough, so we'll use the target's velocty
    43.                     targetForward = targetRigidbody.velocity.normalized;
    44.                     targetUp = Vector3.up;
    45.                 }
    46.                 else
    47.                 {
    48.                     targetUp = Vector3.up;
    49.                 }
    50.                 m_CurrentTurnAmount = Mathf.SmoothDamp(m_CurrentTurnAmount, 1, ref m_TurnSpeedVelocityChange, m_SmoothTurnTime);
    51.             }
    52.             else
    53.             {
    54.                 // we're in 'follow rotation' mode, where the camera rig's rotation follows the object's rotation.
    55.  
    56.                 // This section allows the camera to stop following the target's rotation when the target is spinning too fast.
    57.                 // eg when a car has been knocked into a spin. The camera will resume following the rotation
    58.                 // of the target when the target's angular velocity slows below the threshold.
    59.                 var currentFlatAngle = Mathf.Atan2(targetForward.x, targetForward.z)*Mathf.Rad2Deg;
    60.                 if (m_SpinTurnLimit > 0)
    61.                 {
    62.                     var targetSpinSpeed = Mathf.Abs(Mathf.DeltaAngle(m_LastFlatAngle, currentFlatAngle))/deltaTime;
    63.                     var desiredTurnAmount = Mathf.InverseLerp(m_SpinTurnLimit, m_SpinTurnLimit*0.75f, targetSpinSpeed);
    64.                     var turnReactSpeed = (m_CurrentTurnAmount > desiredTurnAmount ? .1f : 1f);
    65.                     if (Application.isPlaying)
    66.                     {
    67.                         m_CurrentTurnAmount = Mathf.SmoothDamp(m_CurrentTurnAmount, desiredTurnAmount,
    68.                                                              ref m_TurnSpeedVelocityChange, turnReactSpeed);
    69.                     }
    70.                     else
    71.                     {
    72.                         // for editor mode, smoothdamp won't work because it uses deltaTime internally
    73.                         m_CurrentTurnAmount = desiredTurnAmount;
    74.                     }
    75.                 }
    76.                 else
    77.                 {
    78.                     m_CurrentTurnAmount = 1;
    79.                 }
    80.                 m_LastFlatAngle = currentFlatAngle;
    81.             }
    82.  
    83.             // camera position moves towards target position:
    84.             transform.position = Vector3.Lerp(transform.position, m_Target.position, deltaTime*m_MoveSpeed);
    85.  
    86.             // camera's rotation is split into two parts, which can have independend speed settings:
    87.             // rotating towards the target's forward direction (which encompasses its 'yaw' and 'pitch')
    88.             if (!m_FollowTilt)
    89.             {
    90.                 targetForward.y = 0;
    91.                 if (targetForward.sqrMagnitude < float.Epsilon)
    92.                 {
    93.                     targetForward = transform.forward;
    94.                 }
    95.             }
    96.             var rollRotation = Quaternion.LookRotation(targetForward, m_RollUp);
    97.  
    98.             // and aligning with the target object's up direction (i.e. its 'roll')
    99.             m_RollUp = m_RollSpeed > 0 ? Vector3.Slerp(m_RollUp, targetUp, m_RollSpeed*deltaTime) : Vector3.up;
    100.             transform.rotation = Quaternion.Lerp(transform.rotation, rollRotation, m_TurnSpeed*m_CurrentTurnAmount*deltaTime);
    101.         }
    102.     }
    103. }
    104.  
     
  2. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    i dont really care about it working in the editor either
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    It sounds like you want to "flatten" out the class and its parent? You're going to have to go into the PivotBasedCameraRig class and dig into its variables, and copy and paste those into the new script, except for the ones that are being overwritten in the "child" script.

    A more important question, though..... why? You say....

    ....what do you mean by "access it easily"? What's not easy about accessing it as it is now?
     
  4. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    i need to acced the variable m_target and i also want to be able to use this script without the standard assets installed
     
  5. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    i used to have this code inside of that script which was easy but messy id like now to have them seperate thats the main reason i want the previous script to be a bit more clear for me

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ThirdPerson : MonoBehaviour {
    5.     public Shader transparent;
    6.     public Shader Normal;
    7.     public GameObject Model;
    8.     public bool thirdPerson;
    9.     public KeyCode thirdPersonToggle = KeyCode.KeypadEnter;
    10.     public Transform FirstPerson;
    11.     public Transform Thirdperson;
    12.     WeaponHandler wh;
    13.  
    14.  
    15.     void Start(){
    16.         wh = GameObject.FindGameObjectWithTag ("WeaponHandler").GetComponent<WeaponHandler> ();
    17.     }
    18.     void Update()
    19.     {
    20.         if (wh.isGunEquiped == false && thirdPerson == false) {
    21.             Model.GetComponent<SkinnedMeshRenderer> ().materials [2].shader = Normal;
    22.         } else if(wh.isGunEquiped == true && thirdPerson == false){
    23.             Model.GetComponent<SkinnedMeshRenderer>().materials[2].shader = transparent;
    24.         }
    25.         if (Input.GetKeyDown (thirdPersonToggle)) {
    26.             thirdPerson = !thirdPerson;
    27.         }
    28.        
    29.         if (thirdPerson == true) {
    30.             //m_Target = Thirdperson;
    31.             Model.GetComponent<SkinnedMeshRenderer>().materials[1].shader = Normal;
    32.             Model.GetComponent<SkinnedMeshRenderer>().materials[2].shader = Normal;
    33.         }else{
    34.             //m_Target = FirstPerson;
    35.             Model.GetComponent<SkinnedMeshRenderer>().materials[1].shader = transparent;
    36.             //Model.GetComponent<SkinnedMeshRenderer>().materials[2].shader = transparent;
    37.         }
    38.     }
    39. }
    40.  
     
  6. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    ok so i have brung in all of the variables that it requires from the other scripts but i cant seem to find the target rididbody variable

    Code (CSharp):
    1. if (targetRigidbody.velocity.magnitude > m_TargetVelocityLowerLimit)
    2.                 {
    3.                     // velocity is high enough, so we'll use the target's velocty
    4.                     targetForward = targetRigidbody.velocity.normalized;
    5.                     targetUp = Vector3.up;
    6.                 }
    7.                 else
    8.                 {
    9.                     targetUp = Vector3.up;
    10.                 }
    11.                 m_CurrentTurnAmount = Mathf.SmoothDamp(m_CurrentTurnAmount, 1, ref m_TurnSpeedVelocityChange, m_SmoothTurnTime);
     
  7. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    can anyone give me a hand here or can you point me to a similer script?
     
  8. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    never mind i didint realise how stupid i was being i can just do this....

    Code (CSharp):
    1.  
    2.     public class HeadFollow : MonoBehaviour
    3.     {
    4.     public Transform target;
    5.     public float speed;
    6.     void Update() {
    7.         float step = speed * Time.deltaTime;
    8.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    9.     }
    10.     }