Where is the camera component located for ACharacter? - unreal-engine4

So I just started playing around with Unreal Engine 4. I would like to learn as much as I can, so I started with a blank C++ project.
I created a new Character class for my player character, then created a Blueprint based on this class.
The character Blueprint (or some of it's components seem to have a UCameraComponent attached to it, since after making the keybindigs for movement and look up/turn I could already use my mouse to navigate the camera.
My question is, where is this UCameraComponent located? When I open the Blueprint, it seems like it doesn't have a CameraComponent in there. I also tried searching for it in the source code of ACharacter, but couldn't find anything.
I would like to adjust the camera position related to the character because right now this camera is right inside my character mesh.

You have to add it to your class manually.
In YourCharacter.h:
UPROPERTY(EditAnywhere, Category = "Components")
USpringArmComponent* SpringArm = nullptr;
UPROPERTY(EditAnywhere, Category = "Components")
UCameraComponent* Camera = nullptr;
In YourCharacter.cpp constructor:
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(RootComponent);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm);

Related

Why is the Mesh menu empty?

I am new to unreal and am facing a weird problem. I would like to insert a mesh on my character but the mesh menu is completely empty. Below is the image of how it looks.
As you can see, this blueprint class is derived from a class named 'MainCharacter'. Here is the relevant code snippet.
Implementation (c++) file
FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
FPSMesh->SetupAttachment(FPSCameraComponent);
Header file
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
USkeletalMeshComponent *FPSMesh;
The problem started when I switched the USkeletalMeshComponent to a UStaticMeshComponent. I started getting an empty mesh screen. I switched it back to a USkeletalMeshComponent and I am still getting this empty menu.
Any Tips?
Deleting the Blueprint and recreating it worked. No idea why.

How to create blur effect to all the scene except for one object (focus on that object)?

I want to create a blur effect similar to the picture below:
the picture is taken from this site:
https://forum.unity.com/threads/how-to-blur-specific-layers-only.555520/
I tried the post-processing profile and played around with the depth of field in the post-processing volume but it blurs all the scene.
I came across a YouTube Video that explains how to implement similar results to what I am looking for but things have changed drastically in the settings. For example, at 1:57 (minute and 57 seconds) he clicks on Add Additional Camera Data which I am struggling to find in the latest versions of LWRP.
I am using Unity version 2019.2.9f1
How can I achieve the result in the picture above? (Blurring all the scene except for one object)
Your guidance will be appreciated.
NOTE: My project is in VR using SteamVR and VRTK
Though your question is a bit broad I took some time to show you how it can be done.
First of all you will need to import the Post Processing package via the PackageManager
Window &rightarrow; PackageManager
Make sure to be in the All Packages view, search for post, find the Post Processing package and hit Install
Now first of all go to the Layer settings (Layers &rightarrow; Edit Layers)
and add two additional Layers: e.g. PostProcessing and Focused
Now to the cameras. Afaik it makes no difference whether you are in VR or not, usually you have one MainCamera that is moved along with your headset. If there should be two of them in your project setup just repeat the same steps for the second camera.
Make sure the MainCamera doesn't render the two added Layers &rightarrow; remove them from the Culling Mask
Add a new Camera FocusCamera as child to the existing MainCamera. This way it is automatically moved along with the main Camera.
RightClick on MainCamera &rightarrow Camera
It should have all the settings equal to the MainCamera except:
Clear Flags : Don't Clear
If you set it to Depth Only the focused object will always be rendered on top of everything, even if it is actually behind other objects in 3D space. You decide which effect you want here ;)
CullingMask : only Focused
Depth : Anything higher than the MainCamera so this camera is rendered on top of it
Make sure to remove the AudioListener component.
Finally add a new PostProcessingVolume to the scene. I would add it as child to the FocusCamera! Why? - Because this way it is automatically disabled together with the FocusCamera!
RightClick on FocusCamera &rightarrow; 3D Object &rightarrow; Post Processing Volume
Set its Layer to the added PostProcessing
enable Is Global so the distance to the volume doesn't matter and add a new profile by hitting new &rightarrow; Unity &rightarrow; Depth of field
In your case you want to overwrite the Focus Distance so check the box on the left and set a value close to the camera like e.g. 0.5
Until now nothing has really changed in your scene.
Now go to the MainCamera and, a component PostProcessingLayer and set the Layer to our added layer PostProcessing
Now everything should be blurred in your scene!
Almost ready to go! Now Disable the FocusCamera and add this script to it
using UnityEngine;
public class FocusSwitcher : MonoBehaviour
{
public string FocusedLayer = "Focused";
private GameObject currentlyFocused;
private int previousLayer;
public void SetFocused(GameObject obj)
{
// enables this camera and the postProcessingVolume which is the child
gameObject.SetActive(true);
// if something else was focused before reset it
if (currentlyFocused) currentlyFocused.layer = previousLayer;
// store and focus the new object
currentlyFocused = obj;
if (currentlyFocused)
{
previousLayer = currentlyFocused.layer;
currentlyFocused.layer = LayerMask.NameToLayer(FocusedLayer);
}
else
{
// if no object is focused disable the FocusCamera
// and PostProcessingVolume for not wasting rendering resources
gameObject.SetActive(false);
}
}
// On disable make sure to reset the current object
private void OnDisable()
{
if (currentlyFocused) currentlyFocused.layer =previousLayer;
currentlyFocused = null;
}
}
This will allow you to focus a certain GameObject on runtime by changing its layer to the Focused layer we added, the only one that is rendered by the FocusCamera. So this object will be rendered on top of the image without any blur effect!
For demonstration I just added this simple script to every cube object in order to enable focus on mouse enter and disable it on mouse exit:
using UnityEngine;
public class FocusMe : MonoBehaviour
{
[SerializeField] private FocusSwitcher focus;
private void OnMouseEnter()
{
focus.SetFocused(gameObject);
}
private void OnMouseExit()
{
// reset the focus
// in the future you should maybe check first
// if this object is actually the focused one currently
focus.SetFocused(null);
}
}
And here is what it looks like
as said I don't know exactly what your VR setup looks like. If you have to MainCameras simply add two child cameras to them. You still will need only one PostProcessingVolume and only one FocusSwitcher so you would probably move them to another object and handle the camera disabling etc differently but I hope the idea gets clear enough.
Use a separate camera for objects you don't want to blur and set a higher depth value.
Set the ClearFlags to depth only and in the CullingMask select the layer of that one object(or more objects). Obviously you would require to have a different layer for unblurred objects.

How to have custom script icons other than using "Assets/Gizmos" in Unity3D

I know this was asked a lot of times probably .. but it is very often answered wrong.
What I want is:
Use a custom icon for specific components/scripts in the Inspector (e.g. Figure 2 and Figure 3) and the ProjectView (e.g. Figure 1)
What I do so far:
For each component/class that shall have the icon I have an accroding Icon file in the folder
Assets/Gizmos/<Path>/<To>/<Namespace>/<ClassName> icon
and in the Import Settigns set TextureType to Editor GUI and Legacy GUI
This is working fine .. and until now the only way how I could achieve that (having in mind the below section What I definitely do NOT want).
But
However, I wondered if there is really no better way having to have a unique Icon file for each script. This makes the project/UnityPackage unnecessarily huge. Also if I rename a class I always have to rename the according icon file as well ... This imply doesn't feel right!
Most Unity build-in Behaviours and Components have a unique icon. But also external Packages coming from the new PackageManager have built-in icons and sometimes a Gizmos folder but it is not following the above naming rule ... so apparently the icon is configured somehow else for them.
Therefore my questions:
Is there any better way to have those icons for scripts/components?
Preferably scripted and reusing ONE single icon file instead of having the same icon in multiple differently named files.
And/or also Where/How are those icons defined for the scripts coming from the PackageManager?
!NOTE! What I definitely do NOT want:
Show the Icon also in the SceneView for all GameObjects having those components attached (e.g. Figure 4). This is caused by either selecting the icon for this script via the Inspector as in Figure 5 (as allways suggested e.g. in this post or here and even by Unity - Assign Icons ) or using OnDrawGizmos or DrawGizmo. This is not happening using the approach I use currently with the Gizmos folder!
Update
Because this was suggested in this answer: I also know that I could do that and turn them off via the Gizmos settings of the SceneView. But imagine I have like 25 different modules and various different icons each. I don't want to have to disable their Gizmos in the SceneView settings one by one on a per project basis! Even the provided script seems like a vast hackaround. Reflection would be the very last resort I would ever take. Also I'ld prefer to not even have those icons appear as possible Gizmos at all instead of disabling them all.
You can set the icon with figure 5 and then turn the gizmos for that icon off from the gizmos drop down.
Edit: Injunction with the step above you could try this script derived from here it uses reflection to find the class responsible for turning off the the gizmos and icons. This would execute any time your scripts recompiled to keep those icons off or if you added any new icons to the autohide icon file. Note: scriptClass will be an empty string for built in components eg.Camera, AudoSource
using UnityEditor;
using System;
using System.Reflection;
public class DisableAllGizmos
{
[UnityEditor.Callbacks.DidReloadScripts]
private static void OnScriptsReloaded()
{
var Annotation = Type.GetType("UnityEditor.Annotation, UnityEditor");
var ClassId = Annotation.GetField("classID");
var ScriptClass = Annotation.GetField("scriptClass");
var Flags = Annotation.GetField("flags");
var IconEnabled = Annotation.GetField("iconEnabled");
Type AnnotationUtility = Type.GetType("UnityEditor.AnnotationUtility, UnityEditor");
var GetAnnotations = AnnotationUtility.GetMethod("GetAnnotations", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
var SetIconEnabled = AnnotationUtility.GetMethod("SetIconEnabled", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
Array annotations = (Array)GetAnnotations.Invoke(null, null);
foreach (var a in annotations)
{
int classId = (int)ClassId.GetValue(a);
string scriptClass = (string)ScriptClass.GetValue(a);
int flags = (int)Flags.GetValue(a);
int iconEnabled = (int)IconEnabled.GetValue(a);
// this is done to ignore any built in types
if (string.IsNullOrEmpty(scriptClass))
{
continue;
}
// load a json or text file with class names
const int HasIcon = 1;
bool hasIconFlag = (flags & HasIcon) == HasIcon;
// Added for refrence
//const int HasGizmo = 2;
//bool hasGizmoFlag = (flags & HasGizmo) == HasGizmo;
if (/*Compare class names in file to scriptClass == true*/)
{
if (hasIconFlag && (iconEnabled != 0))
{
UnityEngine.Debug.LogWarning(string.Format("Script:'{0}' is not ment to show its icon in the scene view and will auto hide now. " +
"Icon auto hide is checked on script recompile, if you'd like to change this please remove it from the config",scriptClass));
SetIconEnabled.Invoke(null, new object[] { classId, scriptClass, 0 });
}
}
}
}
}
Shown in the inspector with a gizmo
Hide Icon from gizmos dropdown
Icon still appears in the inspector and in the project view but not in the scene
So I did a bit more research about built in types as well as packages coming from the package manager and the asset store. Anything that is external (packagemanager or assetstore) if it has a custom icon for the script and inspector it will Always have a gizmo in the scene view. As it has its icon set using your figure 5 example, as seen in the screenshots with the debug inspector.
Also if you want to set the icon with a script or hide it ,currently reflection is your only option as these APIs are not publicly accessible.
My Script showing the debug inspector for its script
PixelPerfect package script from the packagemanager in the debug inspector
PixelPerfect Icon showing in the scene
I was hoping to add this as a comment to your original question but not enough rep yet.

How do I replace a sprite in unity with another one?

Okay so I've been making a 2d platformer and had a terrible image/sprite for my player. Now I've got a better one and want to just replace the images but keep all the same values and data/scripts etc.
I've been trying to figure it out for awhile but to no avail. Thanks for any help
If you want to permanently change a sprite on your prefabs/objects you can drag the new sprite from your asset folder to into the "Sprite Renderer" Component of the object you want to change, replacing whatever is currently in the sprite box. Check out this image to see exactly where you want to drag the sprite:
O you can change it via script using a public variable:
public class ChangeSprite: MonoBehaviour
{
public Sprite newSprite;
private void ChangeSprite(){
gameObject.GetComponent<SpriteRenderer>().sprite == newSprite;
}
}
One way to do it, not sure it is the best way but it works, is to:
- import your new Sprite (let's call it SpriteB)
- select the GameObject where you have been using your first sprite (SpriteA)
- in the "Sprite Renderer" component of your selected GameObject, replace "Sprite=SpriteA" with your new sprite so that "Sprite=SpriteB"
Obviously you will have to repeat the operation for every GameObject where you may have used SpriteA.

Don't pass through UIToolkit buttons

I've been playing around with UIToolkit, and it's awesome. Though my clicks pass through the buttons, so when clicking the UI my raycast also starts. Is there a way to check if a click/touch is on the GUI?
Edit: I'm using Prime31's UIToolkit
Solved:
foreach (var touchable in _gui.touchableSprites) {
if ( !touchable.hidden && touchable.hitTest(new Vector2(clickPos.x, Screen.height - clickPos.y)) ) return;
}
Thanks.
Assuming you're using prime31's UIToolkit, take a look at the UIToolkit.cs(lines 81-95)
) script The author works an example showing how to handle highlighting his TouchableSprites. The same functionality can be adapted to doing a MouseOver event. I'm not sure if the author has added this functionality since 2011, I've not used UIToolkit in awhile.
Alternatively you can modify all of your ray tracing to return if the first layer hit is "UILayer" (UIToolkit's layer).
A third method, one that I use, is to create a global GUI element manager that stores a list of all GUI rects. When I add a new ray tracing function I call my manager class to see if the mouse cursor is contained in any GUI rect (there is a bit more sophistication that merges overlapping rects), if so, the ray trace is skipped.