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.
Related
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 would like to understand how to create a script to climb objects of different heights like in this video this.
I know you have to use LayerMask to understand if you are in front of an object, but I don't understand the script that brings the character from below to above the object or on the other side (if it is a wall to climb over) .
For a ladder, I thought I'd put "gravity = false" and "transform.up * Input vertical" to go up or down. But to position yourself above a wall in this way, what script do it's used?
And how does the same animation be usable on walls of different heights as in the video ?
Use an animator, setup your animations in your state machine, in the animator click ‘apply root motion’ and your animation will apply to your character position.
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.
I currently have a 2D scene with a orthographic camera and I can move my player with my WASD keys which is great. I am wanting to add functionality of click to move as well but I am sort of lost on a approach. I have read/watched some tutorials and everything seems to revolve around the Nav/Mesh system.
My issue though is that my current scene for the ground and walls have Sprite Renderers and/or BoxColliders on them and I cannot have a Sprite Renderer and a Mesh Renderer on the same GameObject. Here is a quick screenshot of what I have :
Now I understand that I can easily create a click to move with a
Camera.main.ScreenToWorldPoint(Input.mousePosition);
and move towards that position with
Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
The challenge for me now and the knowledge that I am wanting is if I have something like in the screenshot how can I add some sort of path finding system if I was below the house and clicked above the house that my character would walk around the house to get there?
Do I even need to edit my current sprites for the ground? I had an idea that I would just create extra GameObjects add a Mesh Filter and Mesh Renderer to it with "None" for the Materials and place them like puzzle pieces around my scene which would represent the areas I would want my player to move.
Is that approach I am thinking viable? Is it too much? Is there an easier way?
You can use "NavMeshAgent" to move your player. The "NavMeshAgent" component is attached to a mobile character in the game to allow it to navigate the scene using the NavMesh.
Once you have baked the NavMesh Its easy to use it -
navMeshAgent.SetDestination(target);
Reference -
Video Tutorial to create and use nav mesh, Unity Script reference, Navigation and Path Finding
Follow these steps to learn how to bake a Nav Mesh -
Create a 3D cube and scale it to (20,1,20) to make it floor(also rename it to floor).
Create another 3D cube, place it inside the floor and scale it by 5 on Y axis(rename to house).
Duplicate the cube from step2 and change its position so it doesn't overlap with other house.
Go to window > Navigation. This will open the Navigation panel with object tab selected.
SELECT the floor object in hierarchy. And click on "Navigation static" checkbox.
A popup will ask to enable navigation static for children, click "Yes".
Go to "Bake" tab in navigation panel and click on "bake" button at bottom.
You should be able to see the generated Nav Mesh in blue color.
Screenshot for the same -
I have made a thing in unity2d where an object moves according to the location of the mouse compared to the location of the object, to do this there is a vector going from the object to the mouse, all movement is either parrallel to this or perpendicular to it.
This worked fine before adding rotation of the sprite (added as shown below)
void RoterModMus()
{
fRotationIGrader = Vector2.Angle (Vector2.up, vVectorToMouse.normalized);
if(vVectorToMouse.normalized.x<0)
{
transform.rotation =Quaternion.Euler(0, 0, fRotationIGrader);
}
else{transform.rotation =Quaternion.Euler(0, 0, -fRotationIGrader); }
}
When i stop running this funtion in update the movement works again.
Is there a way to either rotate the sprite without rotating the object, or to make this not hurt movement??
I havn't been able to find anything on any of those questions, and i cant figure it out (sorry for danish code)
The code checks the angle between up and the mouse (up is zero in unity) and sets the rotation of the object to that or minus that
Awwkaw
One solution is to add an empty gameObject with the sprite component as a child to your object and then rotate the child AKA empty gameObject with sprite.