Unity abbreviated (2) - 2D moving

1. Enter

Unity input in three ways:

1.1 Direct detects which button is pressed

There are two direct detection:

A. Direct detection of the key advantages: simple, high efficiency disadvantages: Modify button trouble

if(Input.GetKeyDown(KeyCode.X))  //X代表键盘上的某个按键
   。。。

B. indirectly detecting the key advantages: easy to modify key

public KeyCode JumpButton = KeyCode.Space;  //提前设置好不同功能对应按键

if(Input.GetKeyDown(JumpButton))
   。。。

1.2. Detection horizontal input and vertical input

Divided into two types:

A. keyboard-type (for horizontal clearance version)

1.Vertical above the keyboard corresponding to the up and down arrows or W, S keys, when the trigger time

2.Horizontal above the keyboard corresponding to the left and right arrow or A, D key, when the trigger time

B. Mouse class (for aircraft barrage)

1.Mouse X mouse trigger to move along the X screen

2.Mouse Y trigger mouse movement along the screen Y

3.Mouse ScrollWheel when the mouse scroll wheel to scroll trigger

ps: Mouse Y and ScrollWheel operation feel bad, X is quite normal

When reading is divided into two:

  1. Input.GetAxisRaw ( "XXX"); return value is -1, 0 or 1
  2. Input.GetAxis ( "XXX");. Press the button return value is a process of change from a similar acceleration returns a value of 0.1 -> 0.3 -> 0.1 will then successively reduced .. Similar to the brakes and apply drive racing acceleration

Use examples:

float x = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector3.right * x * moveSpeed * Time.deltaTime, Space.World);

float y = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up * y * moveSpeed * Time.deltaTime, Space.World);

Vertical and Horizontal key input can be modified by opening the top bar Edit- Unity> Project Setting-> Input.

Note: XXX name "XXX" is careful not to misspell, otherwise invalid

Double game quickly change keys: https://blog.csdn.net/w1095471150/article/details/52642636


2. Move

Unity four common components and moves Vector3:

(Not specified properties are a function)

2.1.Transform components

  • translate - Mobile number (large inertia may not die wear, but if the mobile is provided too) from the (local coordinate system) relative to a direction
  • property position - position in the world space coordinates of the object

2.2.RigidBody components

  • Velocity Properties - administering to the rigid body velocity vector (instantaneous speed becomes a given velocity vector) (generally inertia)
  • AddForce - add a force to the object (if the object movement can force, is a process of change) (generally inertia)
  • MovePosition - Position to the moving object position (similar to translate)
  • SetDestination - set auto Pat target point

2.4.CharacterController components

  • Move - a more complex motion functions, each absolute motion
  • SimpleMove - at constant speed of the moving object

2.5.Vector 3

  • Lerp - linear interpolation between two vectors, "like a spring to follow the target object."
  • Slerp - spherical interpolation between two vectors, "between sunrise and sunset animation curve."
  • MoveToward - the current location toward the same goal and Vector3.Lerp, maxDistanceDelta speed limit.
  • SmoothDamp - Over time, gradually changing a vector towards the desired goal.

Unity3D displacement, rotating 3D mathematical model: https://blog.csdn.net/sxbluebird/article/details/43225401

3. feel optimization

Premise: to understand the meaning of each parameter rigidbody2D panel, according to the desired effect tune into their game they want

Refer to: https://blog.csdn.net/zhenghongzhi6/article/details/82381537

Content on the new version added Linear Drag linear resistance and other new features in the future

1. Add to move and jump animations, and even join hop Charge

2. Add sound effects

3. Adjust gravity parameters, linear resistance

Blog to share:

Unity performance optimization --Rigidbody2D Detailed: https://blog.csdn.net/SerenaHaven/article/details/78851089

Unity 2D platform game characters move Rigidbody2D explain its common API: http://www.pianshen.com/article/8423262368/

4. Meet your game

Different games require different effects of moves, moving the start to choose the right way will save a lot of late re-select change time and wasted effort.

Inertia can be divided into simple gravity, inertia gravity, inertia-gravity, inertia-gravity

2D game is divided into the following categories:

1. Fighting games (no inertia gravity): need to keep moving around, you may feel bad after adding inertia

2. Aircraft shooting games (no inertia weightless): Need a pile of bullets in the shuttle, so they need to be sensitive control, removal of inertia and gravity

3. Super Mario Jumper class horizontal board (inertial gravity)

4. The space simulation (inertial gravity)

Subdivided according to their own games, such as with inertia, inertia that extent, jump up in the air can move around and so on, these are flexible thinking.

Another one of my blog: Unity2D horizontal version of mobile jump Questions - some jump and double jump

Guess you like

Origin www.cnblogs.com/AMzz/p/11802547.html