Unity3D experience (1) - Soften the camera follows

Overview of issues

No matter what type of game, the lens is an indispensable element. We tend to want to move the lens to follow the main character, but sometimes derived a lot of problems, such as too rigid lens shift (ie, not smooth, but dynamic and abrupt end Hom), or the protagonist is forever fixed in the center of the lens (not displayed the dynamic character).

solution

In fact, the essence of the problem is: moving the lens of lack of texture, in other words, transform our human eye does not match the vision. In real life, we must see things first move, after a delay our eyes will follow moving; on the other hand, we are moving the field of view of time, that it must be from slow to fast, from fast again slow "softening" process, rather than a sudden conversion shakes vision and then suddenly horde, it would seem very awkward.
In conclusion, we wish to main camera also gives physical model allowed simulation "in the physical world, the human eye conversion mode." Specific process is as follows:

  1. Camera to hang up the script and the rigid body , in rigidbody in the bodytype selected dynamic, attention is set friction;
  2. Into the script, get rigid components, and obtain the coordinates of the protagonist and the camera;
  3. Calculated intensity applied (detailed explanation see below);
  4. Coding the force is applied;
  5. Test threshold meets expectations. (Refer to the critical value, when the viewing angle deviates from the center of the main character conversion begins far view)

Code

The code is not difficult to achieve, the key to the game according to their own needs, 施力算法and 临界值make changes.

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

public class Camera : MonoBehaviour
{
    public GameObject go;
    private float gox;
    private float goy;
    private float x;
    private float y;
    private float speed=7;
    [HideInInspector]
    public Rigidbody2D rg;

    private void Awake()
    {
        rg = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        gox = go.transform.position.x;
        goy = go.transform.position.y;
        x = transform.position.x;
        y = transform.position.y;
        if (gox - x > 6&&x<98) rg.AddForce(new Vector2(gox - x, 0) * speed);
        if (x - gox > 6&&x>-96) rg.AddForce(new Vector2(gox - x, 0) * speed);
        if (y - goy > 1&&y>-26) rg.AddForce(new Vector2(0, goy - y) * speed);
        if (goy - y > 1&&y<26) rg.AddForce(new Vector2(0, goy - y) * speed);
    }
}

Figure above, the threshold is 6 x direction, a y-direction is the critical value, when | gox-x | camera after the start of application intensity exceeds a critical value.
It is noted that when quickly reach the scene the camera is no longer applied to the edge of the force, it is necessary to set x, y direction extreme value (extreme value to a value less than the actual edge of the scene to a certain extent, otherwise the inertia of the camera will Release Scenes)

发布了8 篇原创文章 · 获赞 1 · 访问量 332

Guess you like

Origin blog.csdn.net/weixin_42921101/article/details/100082941