How to access Cinemachine Basic MutliChannel Perlin Noise? - unity3d

how can I access the component of the "CinemachineFreeLook Camera's CinemachineBasicMultiChannelPerlin' for me to change some variables in the C#?
Like changing the Amplitude Gain or Frequency Gain

From the CinemachineVirtualCamera script, I understand that the components of the virtual cameras are attached on a hidden child gameobject
/// <summary>This is the name of the hidden GameObject that will be created as a child object
/// of the virtual camera. This hidden game object acts as a container for the polymorphic
/// CinemachineComponent pipeline. The Inspector UI for the Virtual Camera
/// provides access to this pipleline, as do the CinemachineComponent-family of
/// public methods in this class.
/// The lifecycle of the pipeline GameObject is managed automatically.</summary>
public const string PipelineName = "cm";
So I suggest you to get the component like this:
using UnityEngine;
using Cinemachine;
public class YourScript: MonoBehaviour
{
[SerializeField]
private CinemachineVirtualCamera m_vcam = null;
[System.NonSerialized]
private CinemachineBasicMultiChannelPerlin m_noiseComp = null;
void Start()
{
m_noiseComp = m_vcam.GetComponentInChildren<CinemachineBasicMultiChannelPerlin>();
if(noiseComp == null)
Debug.LogError("No MultiChannelPerlin on the virtual camera.", this);
else
Debug.Log($"Noise Component: {noiseComp}");
}
}

Related

Unity - issue with AudioClip array

I'm trying to make an audio player in Unity.
I first created a ResourceManager, that has a
AudioClip[] array.
public class ResourceManager : MonoBehaviour
{
public AudioSource audioSrc;
public AudioClip[] clips;
private int trackNum = 0;
public void Awake()
{
audioSrc = GetComponent<AudioSource>();
audioSrc.Stop();
...omitted...
public void LoadClip()
{
audioSrc.clip = clips[trackNum];
}
}
So that they appear in the Clips drag-and-drop box.
However, I want to make the AudioClip array static in ResourceManager static, so other scripts (e.g. PlayPause.cs can access clips statically (via function calls).
But the problem is once I make clips static, the drag-and-drop for disappears.
Is there a way to fix that? Or are there some better design patterns? Thanks.
You can e.g. simply do
public class ResourceManager : MonoBehaviour
{
...
// This is the one in the Inspector
// Usually I keep the Inspector private and only provide public access
// where needed via properties
[SerializeField] private AudioClip[] clips;
// This will be set in Awake to the ones from the Inspector
// readonly for others, only this class can assign it
public static AudioClip[] Clips { get; private set; }
public void Awake()
{
Clips = clips;
...
}
}
The alternative suggested is make the ResourceManager a so called singleton like e.g.
public class ResourceManager : MonoBehaviour
{
// Here you store your own instance reference
// and provide public readonly access
public static ResourceManager Instance { get; private set; }
[SerializeField] private AudioClip[] clips;
public AudioClip[] Clips => clips;
...
public void Awake()
{
// Make sure only one instance exists at a time
if(Instance && Instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
// optional: Don't destroy this instance when the scene is switched
// Careful: Make sure this applies also to the AudioSource instance!
// For now I assume it is either attached to this object or a child
// if not simply do the same DontDestroyOnLoad on it as well
DontDestroyOnLoad (this.gameObject);
...
}
public void LoadClip()
{
audioSrc.clip = clips[trackNum];
}
}
And then access everywhere ReaourcesManager.Instance.Clips
Or a built-in alternative to the singleton is using FindObjectOfType so any other script in the Scene can actually simply access
FindObjectOfType<ResourcesManager>().clips
without you having to change anything at all ;)
Except as said I would make it
[SerializeField] private AudioClip[] clips;
public IReadonlyList<AudioClip> Clips => clips;
depending a bit of course what exactly you want to do with the clips but this way the array can not be altered by any other script

Unity scriptable object only serializing the unity objects

I am trying to make a spell system. My base class for the system looks like this:
public abstract class MagicBook : ScriptableObject {
public Sprite bookSprite;
public float manaCost;
public float coolDown;
public bool rapidFire;
[HideInInspector] public bool coolDownElapsed;
public virtual void OnActivate() {
}
public virtual void OnActivate(Vector3 position, Quaternion rotation) {
}
}
Then I have another class extending from MagicBook where I override the OnActivate function. My problem now is that in the inspector only the variable bookSprite is showing and all the other values are not there. I tried adding an [SerializeField] in front of the variables and define them new in the extending class. But they still dont show. Has anyone an idea why they are not showing or how I can fix this?
Thanks for your help and time.
Did you create Asset for ScriptableObject?
Add attribute to extended class:
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/NewBook", order = 1)]
And then create asset with
Another idea:
Probably you have custom drawer for this object, but...
Turn your inspector into debug mode and check if properties are visible there.
Cannot reproduce this issue with Unity 2020.1.0f1.
public class NewBook : MagicBook
{
}

Unity: Distance between two game-objects in the scene view

I am new to Unity and as I am creating my interface, I am trying to find what's the distance between two game objects. I know how to do in a C#/javascript script, however I am strugling to find this information in the scene view. Any key I could press while moving an object to see the distance to its neighbours ?
In a script, you can use Vector3.Distance between vector3 to get the distance between two points. Every gameObject has a position that is represented in a Vector3.
Below is a script example that shows you how it works. You just have to drag the script onto a gameObject in your scene and drag in the inspector another gameobject in your scene. The script should run even when your not in play mode because of the [ExecuteInEditMode].This way you will see the distanceBetweenObjects update in real time without actually having to hit play.
using UnityEngine;
[ExecuteInEditMode]
public class DistanceBetweenTwoObjects : MonoBehaviour
{
public GameObject obj;
public float distanceBetweenObjects;
private void Update()
{
distanceBetweenObjects = Vector3.Distance(transform.position, obj.transform.position);
Debug.DrawLine(transform.position, obj.transform.position, Color.green);
}
private void OnDrawGizmos()
{
GUI.color = Color.black;
Handles.Label(transform.position - (transform.position -
obj.transform.position)/2, distanceBetweenObjects.ToString());
}
}
The method OnDrawGizmos will draw text in between the 2 objects showing the distance value to help make it more user friendly.
There isn't any built-in functionality for this, but it's fairly trivial to add such a readout into the display using [ExecuteinEditMode], which causes scripts to run even when the game is not in play mode. This script, for example, should readout the distances on different axes and in total:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class EditorDistanceDisplay : MonoBehaviour
{
public GameObject target1;
public GameObject target2;
public float distanceX;
public float distanceY;
public float distanceZ;
public float distanceTotal;
void Start()
{
}
void Update()
{
Vector3 delta = target2.transform.position - target1.transform.position;
distanceX = delta.x;
distanceY = delta.y;
distanceZ = delta.z;
distanceTotal = delta.magnitude;
}
}

Change gameobject sprite from other scene

I am new and learning coding. How do I change a sprite from another game scene through a buttoned code.
using UnityEngine;
public class PlayerChoice : MonoBehaviour
{
public GameObject plo;
public Sprite boy;
public Sprite girl;
public void Boy()
{
plo.GetComponent<SpriteRenderer>().sprite = boy;
}
public void Girl()
{
plo.GetComponent<SpriteRenderer>().sprite = girl;
}
}
You actually can't do it the way you are trying.
This is a bit tricky for a beginner and you would have to read a bit to get behind it.
This is some starting point where you can dive in:
You would have to to make a gameobject that lives between scenes with
DontDestroyOnLoad(targetGameObject);
see: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
On this gameobject you could have a script which holds all variables that are needed in the next scene.
When scenes are switched access that object and get your values;

Load scene with param variable Unity

In my game there is a map view that contains a 50x50 grid of tiles. When you click on the tile you get sent to that tiles view and attack things, etc. The only difference between these "tiles" as far as the code will be concerned is the tiles ID, aka. which number on the grid. That number will be passed to the server on init to handle the rest.
Obviously, with that being the only difference in tiles it would be a mistake to create a scene "1", scene "2"...scene "2500" and call SceneManager.LoadScene to switch to the specific tile view.
I could use DontDestroyOnLoad(); when the tile is clicked to preserve the tile ID on scene switch but 1) it only accepts gameobjects not just an int variable 2) I don't need/want to preserve that variable for anything more than the init in tile view. So while it could work it seems like overkill.
Is there a better practice for essentially just passing a parameter to a scene load?
You can make a static class that holds the information for you. This class will not be attached to any GameObject and will not be destroyed when changing scene. It is static which means there can only be ONE of it; you can't write StaticClassName scn = new StaticClassName() to create new static classes. You access them straight through StaticClassName.SomeStaticMethod() for example and can be accessed from anywhere. See this example on how to store a value in a variable, change scene and then use it in that scene (please note the added using UnityEngine.SceneManagement;):
A normal Unity script attached to a GameObject in Scene "Test":
using UnityEngine;
using UnityEngine.SceneManagement;
public class TestingScript : MonoBehaviour
{
void Start()
{
StaticClass.CrossSceneInformation = "Hello Scene2!";
SceneManager.LoadScene("Test2");
}
}
A new static class (not inheriting from monobehaviour) that holds information:
public static class StaticClass
{
public static string CrossSceneInformation { get; set; }
}
A script attached to a game object in scene "Test2":
using UnityEngine;
public class TestingScript2: MonoBehaviour
{
void Start ()
{
Debug.Log(StaticClass.CrossSceneInformation);
}
}
You don't need to have the entire class static (if you for some reason need to create more instances of it). If you were to remove the static from the class (not the variable) you can still access the static variable through StaticClass.CrossSceneInformation but you can also do StaticClass sc = new StaticClass();. With this sc you can use the class's non-static members but not the static CrossSceneInformation since there can only be ONE of that (because it's static).
Just wanted to share a premade solution for this as the question is already answered, so others or maybe even the OP use it.
This script uses a key-value approach for storing and modifying variables inside a static class that means it is available across scenes so you can use it as a cross-scene persistent storage, so all you need to do is import the script to your Unity project and use the API (check below for an example):
using System.Collections.Generic;
/// <summary>
/// A simple static class to get and set globally accessible variables through a key-value approach.
/// </summary>
/// <remarks>
/// <para>Uses a key-value approach (dictionary) for storing and modifying variables.</para>
/// <para>It also uses a lock to ensure consistency between the threads.</para>
/// </remarks>
public static class GlobalVariables
{
private static readonly object lockObject = new object();
private static Dictionary<string, object> variablesDictionary = new Dictionary<string, object>();
/// <summary>
/// The underlying key-value storage (dictionary).
/// </summary>
/// <value>Gets the underlying variables dictionary</value>
public static Dictionary<string, object> VariablesDictionary => variablesDictionary;
/// <summary>
/// Retrieves all global variables.
/// </summary>
/// <returns>The global variables dictionary object.</returns>
public static Dictionary<string, object> GetAll()
{
return variablesDictionary;
}
/// <summary>
/// Gets a variable and casts it to the provided type argument.
/// </summary>
/// <typeparam name="T">The type of the variable</typeparam>
/// <param name="key">The variable key</param>
/// <returns>The casted variable value</returns>
public static T Get<T>(string key)
{
if (variablesDictionary == null || !variablesDictionary.ContainsKey(key))
{
return default(T);
}
return (T)variablesDictionary[key];
}
/// <summary>
/// Sets the variable, the existing value gets overridden.
/// </summary>
/// <remarks>It uses a lock under the hood to ensure consistensy between threads</remarks>
/// <param name="key">The variable name/key</param>
/// <param name="value">The variable value</param>
public static void Set(string key, object value)
{
lock (lockObject)
{
if (variablesDictionary == null)
{
variablesDictionary = new Dictionary<string, object>();
}
variablesDictionary[key] = value;
}
}
}
And you can use it on a script that is inside your Main Menu scene let's say:
public class MainMenuScript : MonoBehaviour
{
void Start()
{
// Load a sample level
LoadLevel(12);
}
public void LoadLevel(int level)
{
GlobalVariables.Set("currentLevelIndex", level);
UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
}
}
And the other one is inside Game scene:
public class GameScript : MonoBehaviour
{
void Start()
{
int levelIndex = GlobalVariables.Get<int>("currentLevelIndex");
Debug.Log(levelIndex); // Outputs 12
}
}
So, the data is assigned in the Main Menu scene is accessible from the Game or any other scene.
Learn more about the script with usage examples >
Maakep! Perfect and easy code!
But your method to load scene not working.
You can use another method:
UnityEngine.SceneManagement.SceneManager.LoadScene("Vuforia-4-Spheric");