In my scene I have 2 camera(split screen). it's possible change the trasparency of a layer only for one camera? for examples the "layer1" have alphatrasparency = 0.5 for right camera while left camera show "layer1" without trasparency.
Ostensibly no
There's a way to do it, though. It's a bit of a hack though, as it doesn't depend on physics layers, but rather the presence of a custom monobehavior script. Here's what I remember about this off the top of my head (I can dig up an implementation later, if needed).
Step 1: you will need a MonoBehaviour script attached to every game object you want to have rendered differently. This script is absolutely essential.
Step 2: this script will contain one function (you can use an existing behaviour script if all the objects already have one in common and you can just add this function to that script). Call it whatever you want, but something like AboutToBeRendered(Camera cam)
Step 3: create another script and attach it to both cameras. This script will also have one function in it: OnPreRender()
Step 4: In this OnPreRender method you will need to do:
find all game objects from Step 1
get their component with the AboutToBeRendered method
invoke that method, passing the camera as the paramter
Step 5: Writing the AboutToBeRendered method.
determinie which of the two cameras was passed to the method
set the material's color to be transparent or not, as needed
OnPreRender() is only called on scripts with a camera component on the same GameObject, indicating that this camera is about to render the scene. But what we actually want is for the object about to be rendered to know that it's about to be rendered and by which camera. Which is why we need the script in step 1.
I suppose you could skip step 1 and only look at every object in the scene and look at its physics layer, but that's going to be more expensive to figure out than "get me all instances of this component." You could do it based on Tag instead, as FindObjectsWithTag is generally considered to be pretty fast, but if you're already using the tag for something else, you're out of luck, and there's no comparable method for getting objects in a given physics layer.
However, you'd have to tweak the material's alpha value in the camera script rather than letting the object itself decide what value it should be.
In either case, the object's material would need to support transparency. When I did this I was preventing the object from being rendered entirely, so I just disabled its MeshRenderer component.
Related
I'm new to unreal engine, I'm trying to add large force to an object with a box collider but after it collide with other object (just another instance) the overlap inside each others and become like one object and moving with each others.
Cab anyone explain their behavior and how i should resolve this?
What happens here is that both objects collide with each other continously. To fix that you could try to deactive the OnOverlap()-Event on either the overlapping Object or the colliding object.
In blueprints you can achieve that by setting the Generate Overlap Events-Variable of one of the colliding static meshes of the overlapping objects to false.
In C++ you could simply remove the dynamic event callback for one of the colliding objects like that:
CollidingComponent->OnComponentBeginOverlap.RemoveDynamic(this, &ACollidingActor::OnBeginOverlap);
Where CollidingComponent is the component of your object, which causes the overlap event to trigger.
Like #Alex said, they overlap with each other, over and over. If you didn't know, you can add breakpoints to your blueprint nodes, just like in your code, by right-clicking a node and select Enable Breakpoint (or smth like that). Your game will stop when reaching it and switch to that exact point in your blueprint. You can then hover over that node and see every variables value going in and out of it.
Hope this helps you learing to use the Unreal Engine.
The Unity Manual describes the order in which the Script functions are called. However, I was wondering if there were any rules regarding the order in which the GameObjects themselves are considered in Unity.
GameObjects are basically the nodes of Unity's scene graph and (assuming the scene itself was the root node) they form a tree. I was wondering if that tree structure imposed any rules on the order in which GameObjects are considered.
As already mentioned, the manual describes that Awake() is always called before Start() which is always called before the first call to Update() and so on. However, these relations in time are (mostly) given in scope of a single script on a single GameObject. I want to know if there is also a rule stating the order in which Start() (or any other method) is called on all the GameObjects in the scene.
Specifically I wanted to know:
Are parents always considered before their children are?
Are siblings considered in the same order they are displayed in the scene graph?
Is Script Execution Order enforced only in scope of a single GameObject, or does it consider all GameObjects?
I built a small test project in Unity which basically consists of a 3x3x3 tree of GameObjects, each having 3 scripts.
I found the following answers:
No. Some GameObjects can be considered before their parents are, while some parents can be considered before their children are. And this order can change when reloading the scene or manipulating the scene graph.
No. Siblings can be updated in any order. This order can change when reloading the scene or manipulating the scene graph.
It is enforced over all GameObjects in the scene. If SEO sets script A to be executed before script B, then all instances of script A will be considered before any instance of script B is. Meaning, all instances of A call their Awake() before any B call their Awake(), then all instances of A call their Start() before any instance of B call their Start() and so on.
I am trying to create a basic spawn sequence- the block must be created, moveDownLeft, and then removeLeft. moveDownLeft and removeLeft work fine on their own when the block is added using self.addChild(block1) previously, however I need to have self.addchild within the sequence.
The only way that I can see to do this is use runBlock, and I looked at this question when I got an error using that: Swift: SKAction.runBlock -> Missing argument for parameter 'completion' in call BUT WHY?
So now I am left with this:
block1.runAction(SKAction.sequence([SKAction.runBlock({ self.addChild(self.block1) }), moveDownLeft, removeLeft]))
And nothing in the sequence works because the block is not created in the first place. Why is this happening?
Your code fragment is too short but it looks like a typical chicken and egg problem.
node can only run actions once it has been added as child and thus becomes part of the scene graph
your node is supposed to run an action that will eventually add itself to the scene graph but it's not in the scene graph yet so it won't run that action
Add the node as child first, then run the action. If you need the node to be inactive for some time, simply set it's visible property to NO for the duration. You kay also ned to change other properties, ie postpone creation of the physics body.
What is the difference between those two methods? Why should i prefer one?
1)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().active = true;
2)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().SendMessage("setActive");
thanks!
Sending a message searches through all the components of the gameObject and invokes any function that has the same name as the message. Not a 100% sure but Im sure this uses reflection which is generally considered slow.
SetActive() or the active variable set the gameObject as active or not. If its not active it wont render in the scene and vice versa.
First of all it seems there are several inconsistencies with your code above:
1) Components (and MonoBehavior) don't have an active property (active belongs to GameObject), so the first line of code shouldn't compile. In addition the most recente version of unity don't have active anymore, it's substitued with activeSelf and activeInHierarchy.
And btw, both activeSelf and activeInHierarchy are read only, so you can't assing directly a value to them. For changing their value use SetActive method.
2)
The second line of code shouldn't work either (unless Unity does some magic behind the scenes) because SetActive methods belong to GameObject and not to your Rocket Component.
Now, I suppose your question was the difference between:
gameObject.SetActive(true);
and
gameObject.SendMessage("SetActive",true);
The result is the same, but in the second way Unity3D will use reflection to find the proper method to be called, if any. Reflection has an heavy impact on performance, so I suggest you to avoid it as much as possible.
I am making a game in Unity. There are currently two levels. I count to 30 seconds, when the time becomes 0, I want to load the next level automatically. But when the next level is loaded, the game screen freezes and I cannot move anything. The function I used to load the next level follows below (this function is in a script which an empty game object which will not be destroyed when loading a new level carries):
function loadNextlvl(){
var cur_level = Application.loadedLevel;
yield WaitForSeconds(5.0);
Application.LoadLevel(cur_level + 1);
}
What should I do?
My work with Unity has been hobby-driven only, but anytime I've used Application.LoadLevel I passed it a string of the level name, rather than a pointer. I see from the API that it's overloaded to take an int as well, but maybe for testing purposes, call it by name to see if that works.
Also, you need to tell Unity the levels you're going to be using. You can do this in the Build Settings off the file menu.
Lastly, you can try using Application.levelCount to see if you're within the bounds of levels.