Write ARPG mobile system (a) a timely and properly affixed in Unity

(1. written here as well as movement of the moving mobile camera system comprises no animation character 2. 3. Part I is a station b "Fu teacher" soul engraved black write this mobile video system, so highly similar)

## numerical figures do first input and output modules
for the characters in the game, it is actually moving it toward a position in a certain direction to its movement or the speed in a certain direction.
So how come this direction is it, for the players, it is set by your keys to maneuver.

public string keyUp = "w";
public string keyDown = "s";
public string keyLeft = "a";
public string keyRight = "d";

So we need to set a good start in the direction of the game keys, then after obtaining the keys, we should let the key signal is pressed into a value to represent the direction. Therefore, we first define two float variables as follows:

public float Dup;
public float Dright;

Dup is the value in the positive direction of the front (direction up), Dright is the right direction to positive values ​​(direction right), we give the corresponding values ​​of these two variables in the Update function:

    void Update()
    {
        Dup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
        Dright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);
    }

In this way, when you set a good bit key is pressed in the game, Dup and Dright will be able to think of one thing at -1 or 1 change, then see this value, it is easy - coordinate system .
Yes, Dup and Dright now presented values can allow us to build a player-coordinate origin to the player face to the y-axis direction, the right direction for the players to the x-axis coordinate system, then our ideas came out : the player to input the key signal is converted to point coordinate system, and the coordinate system is a point we all know that can indicate the direction, that we can get the direction the player wants.
And after getting this direction coordinate system, we should think, if we can get a point in the coordinate system, then let characters in the game towards this point forward, you can not move yet!
Yes, in this way, we can solve the roles and moving toward two problems at the same time.
But now there is a loophole that we are now Dup and Dright value is in the process of mutation, either 1 or -1, so we get the most points in the coordinate system only eight, our role the steering and forward will be particularly significant, which is obviously not what we want, so we need to value Dup and Dright can become slowly increment / decrement, then here we need to introduce the "SmoothDamp" method.

    private float targetDup;
    private float targetDright;
    private float velocityDup;
    private float velocityDright;

Four sets of values defined target value, respectively, a front direction, a right direction of the target value, the memory space used in forward direction operation, a rightward operation memory space.
Then, we just need to change the code, mutations Dup and Dright not what we want, they are just the ultimate goal, so this mutation value is assigned targetDup and targetDright it.

targetDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
targetDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);

At a time when Dup and Dright us deal with:

Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 1.0f);
Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 1.0f);

Explain it, using four parameters in SmoothDamp method, the first parameter is to adjust the value of the second parameter is to be adjusted to the final target (current Velocity, understood as progress), the third of ref value is the memory space with its operations, it will own parameters passed, we do not have the specific knowledge, ensure that it is the same type of argument can (SmoothDamp method is also used to float variables), the fourth parameter is the whole change the time course used.
After this run the game we will be able to find Dup as the input and the value of the key signal Dright will begin easing up, met our expectations, and we can adjust the SmoothDamp last time parameter method to get the result we need ( I ended up using was 0.1f).
After using methods SmoothDamp Dup and Dright value becomes easing
Thus input and output modules has been completed, we can start writing the motion controller. (Here attached Code)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
    [Header("===== Key settings =====")]
    public string keyUp = "w";
    public string keyDown = "s";
    public string keyLeft = "a";
    public string keyRight = "d";

    [Header("===== Output signals =====")]
    public float Dup;
    public float Dright;

    [Header("===== Others =====")]
    public bool inputEnabled = true;

    private float targetDup;
    private float targetDright;
    private float velocityDup;
    private float velocityDright;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        targetDup = ((Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0));
        targetDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);

        if (inputEnabled == false)
        {
            targetDup = 0;
            targetDright = 0;
        }

        Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);
    }
}


#### supplement a mobile switching system
if you want to ban a player and then move, the general practice is to move directly to the script SetActive off, but this is the case reopened because the script input module in value can still be changed so may cause the value of chaos so we should correct shutdown method is to use the targetDup and targetDright cleared this soft switching:

    public bool inputEnabled = true;

A bool value is defined: a movable switch, then there are two ways.
One is to modify targetDup and targetDright formula:

targetDup = ((Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0)) * (inputEnabled ? 1.0f : 0);
targetDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0)* (inputEnabled ? 1.0f : 0);

If you think this formula too complex formula, it can also be used directly if a change to their values:

if (inputEnabled == false)
{
    targetDup = 0;
    targetDright = 0;
}
Published an original article · won praise 0 · Views 17

Guess you like

Origin blog.csdn.net/YDX_yq/article/details/104373270