Getting Started with Unity - Game Object Creation and Animation Settings

1. Game object creation

Create directly

  • Drag game materials directly into the Scene window

By creating2D Objectcreate

  1. Right-click in the Hierarchy window and select 2D Object, then select Sprites. Select Square, Circle or other graphics according to the game object graphics.
  2. Drag the game material into the Sprite of the Sprite Renderer under the Inspector window. At this time, the game object will take on the style of the game material (to set the image size in advance, it is generally appropriate to set the Pixeis Per Unit to 16)

figure 1
figure 2

Collider settings

  • After clicking on the game object, click Add Component under the Inspector window, and add Rigidbody 2D and appropriate colliders (the colliders include square collider Box Collider 2D, circular collider Circle Collider 2D, and custom collider Capule Collider 2D). demand selection)

2. Animation settings

Create animators and animations

  • Right-click Create -> Animation in the resource directory and name the Animation
  • Drag Animation onto the object to be animated, and an Animator Controller will appear in the directory.

Use of Animator and Animation windows

  1. Click Window -> Animation -> Animation / Animator

Insert image description here

  1. Set the sampling rate: click on the upper right corner of the Animation window, click and then check Show Sample Rate

Insert image description here

  1. Click on the object to be animated and drag the created Animation into the Animator window

Insert image description here

  1. Repeat playback: Click on the Animation created in the directory and check Loop Time in Inspect

Insert image description here

Animation switching

  1. Click Parameters in the upper left corner of the Animator window, click the + sign to select the appropriate type to create

Insert image description here

  1. Click the connecting arrow between animations in Animator, uncheck Has Exit Time, then click Setting and set Transition Duration to 0 (animation switching is faster)

Insert image description here
4. Click the connecting arrow between animations in Animator, and add the created trigger in Inspector -> Conditions ->

3. Multiple animation switching (taking Int type as an example)

  1. Click Parameters in the upper left corner of the Animator window, click the + sign to select the Int type, and create a state
  2. Click the connecting arrow between animations in Animator, go to Inspector -> Conditions -> Add state, select Equals, and then select the animation to switch
  3. Create a C Sharp script control
Class AnimController
{
    
    
	private float dirX = 0f;
	private Animator anim;
	private SpriteRenderer sprite;
	private enum MovementState {
    
     idle, running, jumping, falling }
	
	void Start()
	{
    
    
		anim = GetComponent<Animator>();
	}
	
	void Update()
	{
    
    
		UpdateAnimationState();
	}
	
	public void UpdateAnimationState()
	{
    
    
		MovementState state;
		if(diX > 0f)
		{
    
    
			state = MovementState.running;
			sprite.flipx = false;//反转动画的X轴
		}
		else if(dirX < 0f)
		{
    
    
			state = MovementState.running;
			sprite.flipx = true;
		}
		else
		{
    
    
			state = MovementState.idle;
		}
		
		if(rb.velocity.y > .1f)
		{
    
    
			state = MovementState.jumping;
		}
		else if(rb.velocity.y < -.1f)
		{
    
    
			state = MovementState.falling;
		}
		
		anim.SetInterger("state",(int)state);
	}
}


Guess you like

Origin blog.csdn.net/weixin_73402838/article/details/131047768