Hi I was just wondering what position will take priority with a child objects position. Does the position of the parent object or a position set through a script take priority?
I'm asking this because I have a player with a camera as a child object which by default follows the parent. I'm trying to implement a system where I can set the transform.position of the camera to the other player in the game when you finish, sort of like a spectator mode. The original player that the camera follows is constantly falling when they complete the game as they fall out the map, and I can't destroy the player as it holds information about their score that I need later.
Possibly the constantly moving player (as it falls) is stopping the camera from lerping to the other player in the game?
In this case you need to change the parent of the camera to the player you want to follow (since the current parent is constantly changing it's position by falling down). You could also set the rigidbody of the player that is falling to kinematic so it won't move and disable it's renderer so he is not visible.
Related
Alright, so I have a 2d rocket with a particle system shooting squares and triangles as thrust. The issue I am having is that, the particles that are emitted rotate with the rocket when the rocket and the particle system turn. The particle system is a child to the rocket. So if the rocket is going up, the particles emit downwards, but say I turn the rocket to be going down, those already emitted particles change their position to be above the rocket and start going up. How can I fix this?
Thank you!
One way to go about it is just to change simulation space to world.
With mode set to local, any changes to your parent game object, or the particle system itself, propagates down to all the living particles.
With world, new particles will match the position and rotation of your rocket, but the ones already emitted will retain their direction, as local changes to transform of ps/any other parent, will no longer affect them.
I am currently making a sidescrolling game in Unity 2D where an enemy rises up, and then dives down to the player, but then keeps going until it is off screen, like how this image shows. 1 I can get the enemy to collide with the player but I can't make it keeping in that direction until off screen. How would I be able to do this?
You have to make your collider to trigger (it's a checkbox on your collider)
That means your object will not be affected by physics when it touch another collider, but it can calls functions.
You have to replace OnCollisionEnter/OnCollisionEnter2D, etc by OnTriggerEnter/OnTriggerEnter2D, etc.
Be careful, the parameter of the function may change too (Collision/Collider).
I'm developing an enemy AI in a 2D Game that I'm working on. This enemy swims and I wanted to make a "floating effect" animation for the enemy, so I made an animation where the Y Axis of the game object bounces up and down.
I Use transform.Translate() to move the enemies in the game and it worked just fine until I made this animation. But, when the animation is playing, the enemy can't move in any direction.
public virtual void Move(float speed)
{
if (canMove)
{
transform.Translate(new Vector2(speed, 0) * Time.deltaTime);
}
}
Once you have a keyframe in any state of your animator for a certain property the animator will always overrule any changes done in a script because the animation updates are all done after Update. You could try to either move your code to LateUpdate.
Or in your specific case you do not want the x component of your position keyframed at all. Simply remove all the keyframes for the x (and z) component(s) of the position from the animations so only y has keyframes. This should solve your problem.
Alternatively use your movement script on a GameObject on a higher level in the hierachy as your Animator - meaning add a new GameObject, make the animated object a child of it and place your movement script instead on that parant object.
Does anyone know how to transition a game object to an existing animation?
Currently I have a cube object like this:
The cube Object has a preconfigured simple jumping animation with fixed location as Layer default state. Lets call this animation: "BoxJumping".
The player however can move the cube object to any position(using wsad) key.
When the player stop for 1 sec, I want the cube to transition back to the original position of the animation. Like this:
I can simply use:
private void playJumpAnim()
{
gameObject.GetComponent<Animation>().Play("BoxJumping");
}
However, it just directly move the cube to the preconfigure location which has been stored in the animation and play the animation without any smoothing transtion.
Does anyone know how to achieve this kind of transition?
A very simple solution is create an empty gameobject to be parent of the cube. When the Animator is a child, it performs the updates on local space.
Then you can move the cube by the parent gameobject.
I'm making a game (similar to Subway Surfers), and I have a floor made of many small cubes. And I want to make my game do something when the player stops colliding with the floor. But when I use OnTriggerExit (Collider other) I never know if the player stops colliding the floor or just moves from one piece of the floor to another:
OnTriggerExit (Collider other){
if (other.tag == "floor") {
if(/*Object isn't coliding with any other object with the tag "floor" (Or simply isn't coliding with the other piece of the floor) */){
//Do something
}
}
}
It depends on the needs of the game. Since you named Subway Surfer, I would go with the z-coordinate of the player.
I would add the heightFromGround field to the player class (calculated as the difference on the z-axis between the player and the platform underneath).
Then, if heightFromGround is higher than a threshold, you know the player is jumping.
This also allows you to control the Animation of the character based on it's height from the ground (so he can prepare to land) and transition to the running state.
To have different platforms at different heights, you need to know the platform currently underneath the player at runtime.
This is just an approach to avoid collision checking.
If you don't need that level of control on Animations and prefer to keep using collisions, you can simply add a field int touchingPlatforms = 1; and add 1 to it OnCollisionEnter, subtract 1 from it OnCollisionExit. The player is jumping if touchingPlatforms is 0.