Unity of the classic game programming: Sphere Wars

Copyright Notice:

  • Original article published on the blog Park "You dream maker culture," the blog space (URL: http://www.cnblogs.com/raymondking123/) and micro-channel public number "dream maker culture"
  • You are free to reprint, but must include the full copyright notice!

First of all:

  • alt + mouse drag view rotations
  • Hold the middle mouse button and moving the mouse, can pan the view
  • Press and hold the right mouse button, wasdqe 6 can be moved in a direction

  • In Hierarchy view, the left mouse button to select an object, press shift, then the left mouse button to select another object, to select a plurality of objects, an operation. Component can be added at this time for all the right-hand Inspector adding components GameObject

  • unity default games in meters
  • The default is 10 m length and width plane default Capsule 1 m 2 plus a hemispherical cylinder 1m composition cube Sphere
  • The unity Skybox also provide some (weak light in all directions) skylight

  • Global coordinate system and the scene is always consistent coordinate system (as the world coordinate system selection)
  • local coordinate system always remains parallel to the direction of the object itself

  • persp is a perspective mode, near the far smaller
  • Iso is the parallel projection

  • materials Material

  • Color as the object light obtained by the color finally displayed to the user (color components are multiplied, the reason why the object is red because it reflects red light)

  • Time.timeScale // size of the passage of time. This can be used for slow-motion effects
  • When timeScale is 1.0 times as fast as real-time. When timeScale is 0.5, two times slower than real time.
  • When timeScale set to zero, if all functions are independent of the frame rate, the game basically suspended.
  • In addition realtimeSinceStartup, timeScale affect all classes of time Time and delta time measurement variable.
  • If you reduce timeScale, Time.fixedDeltaTime will be reduced by the same amount. When set to zero, does not call - fixedupdate

  • Small ball moves:
    private void FixedUpdate()
    {
        Time.timeScale =0.5f;      //子弹时间,加速减速均可用该值实现
        if (Input.GetKey(KeyCode.LeftArrow))            //unity事先就检测到了所有按键,所以在这可以直接判断按键是否按下,用if判断就可以同时施加不同方向上的力   //GetKey是检测按键的持续情况,GetKeyUp和GetKeyDown是只有在按键的上、下边缘才会检测,可以做触发器,但不宜实时控制运动
        {
            rigidbody.AddForce(-moveForce * Time.fixedDeltaTime, 0, 0);   //乘以FixedDeltaTime的意义就在于时间可以缩放,加了个权重
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rigidbody.AddForce(moveForce * Time.fixedDeltaTime, 0, 0);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            rigidbody.AddForce(0, 0, moveForce * Time.fixedDeltaTime);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            rigidbody.AddForce(0, 0, -moveForce * Time.fixedDeltaTime);
        }
        if (Input.GetKey(KeyCode.Space))
        {
            rigidbody.AddForce(0, jumpForce * Time.fixedDeltaTime, 0);
        }
    }
  • Physic Material // physics material is used to adjust the rebound effect of friction and collision objects. // a property belonging collider
  • Properties: Function:
  • Dynamic friction when moving the frictional force has been used. Usually a value from 0 to 1. Zero value feels like ice, the value of 1 will make it very fast to stop, unless a large amount of gravitational force or pushing objects.
  • Static friction stationary object used when the friction force on the surface. Typically a value between 0 and 1. Zero value feels like ice, the value of 1 will cause the object to move very difficult.
  • Rebound surface how resilient? A value of 0 will not rebound. A value of 1 rebound without any loss of energy, but you can expect some approximations, but this may be a slight increase in energy simulation.
  • How friction combination of two colliding objects friction.
    - Average values are the average of two friction.
    - Minimum use of these two values in the smallest one.
    - maximum use of these two values in the biggest one.
    - multiplying the friction values are multiplied with each other.
  • How to bounce a combination of elastic collision of two objects. It has the same combination pattern of the friction pattern
  • The amount of surface friction is prevented from slipping off each other. When you try to stack objects, this value is very important. There are two forms of friction, static and dynamic. When static friction using a stationary object. It will prevent the object began to move. If sufficient force is applied to the object, it starts moving. At this time, the dynamic friction will play a role. Dynamic friction will now try to slow down the speed of the object in contact with another object.
  • When two objects come into contact, according to the selected mode of applying the same effect on the elasticity and friction both of them. When a collision is in contact with two different combinations of mode settings, there is a special case. In this particular case, with the highest priority features. The following order of priority: Average <Min <multiplied <maximum value. For example, if a material has an average value but the other has a maximum value, will have to use a combined function is the maximum value, because it has a higher priority. // force is mutual, so that the coefficient of friction when the two methods are different objects, different friction calculation, but applied to the two bodies of equal and opposite frictional force

  • Let the cameras follow the ball moving, if as a child, will receive the impact of small ball movement and rotation, we do not wish to receive rotary impact, so we just need to move the camera to follow the position of the ball can be: // a parallelogram mobile

public class MainCamera : MonoBehaviour
{
    public Transform player;

    private Vector3 offset;
    private Vector3 playerStartPosition;
    private Vector3 cameraStartPosition;
    // Start is called before the first frame update
    void Start()
    {
        playerStartPosition = player.position;
        cameraStartPosition = this.transform.position;

        offset = this.transform.position - player.position;
    }

    // Update is called once per frame
    void Update()
    {
   //     this.transform.position = cameraStartPosition + (player.position - playerStartPosition);            //用Offset和相机起始位置偏移均可,因为是个平行四边形
        this.transform.position = player.position + offset;
    }
}
  • In order not to affect the speed of the ball when eating props, props need to be set to trigger

  • collider performance optimization: // collider mesh model corresponding to the mesh, but not for drawing but to compute the impact of all the static objects collider generates a static mesh, when the dynamic object is a static object collides, the reaction was carried out how dynamic objects based on the grid
  • After all of the calculated static unity impactor and an impactor scene holding them in the buffer, so that all static and moving collision body can not be saved for each frame calculated collision body // save the overhead buffer to unity, not each frame is calculated static collision body again
  • Our mistake is to allow rotation of the props
  • Whenever we move, rotate, scale a static impactor, unity will again recalculate all static impactor and update the cache static impactor, and the cache is recalculated cost
  • We can move, rotate or scale the dynamic impact and unity is not re-cache any impactor collision body // dynamic collision is calculated in real time
  • We encourage unity collision body moving, as long as we declare the collider before moving to dynamic
  • To do this, we just use Rigidbody components, and any game object with Collider Rigidbody are considered to be dynamic, with no rigidbody any game objects and components with Collider's are considered to be static, but we props is considered to be static, so unity will be recalculated static impactor each frame buffer
  • solution:
  • Add Rigidbody as a prop, in fact, become dynamic collision device,
  • At this cube will fall, because there is Rigidbody gravity, we can set rigidbody.useGravity = false, which prevents cube falling, but this time the cube though not gravity, but any course be affected by other types of physical force and will bring physical overhead
  • A better solution is to set: Rigidbody.IsKinemaitc = true;
  • Kinematic rigid body is not affected by a physical force, but only the affected graphics transformation (such as animations and assembly Transform)
    that used on the object with good trigger or impactor, or automatic movement of the elevator platform is graphical animation (not physical calculation) to achieve

  • to sum up:
    • Static objects should not have Rigidbody
    • Dynamic objects should Rigidbody
    • Rigidbody affected by physical forces should use the default setting (Kinematic to false)
    • Rigidbody affected by the pattern should be set Kinematic transformation to true;

Guess you like

Origin www.cnblogs.com/raymondking123/p/11314972.html