[100 little knowledge points of Unity stepping on the pit] | Unity uses Quaternion.AngleAxis to randomly one direction

Unity uses random rotation in one direction
insert image description here

Unity small science

Old rules, first introduce the popular science knowledge of Unity :

  • Unity is a real-time 3D interactive content creation and operation platform.
  • All creators, including game development , art , architecture , automotive design , film and television , turn their ideas into reality with Unity .
  • The Unity platform provides a complete set of software solutions for creating, operating and monetizing any real-time interactive 2D and 3D content, supporting platforms including mobile phones , tablets , PCs , game consoles , augmented reality and virtual reality devices.
  • Unity can also be simply understood as a game engine , which can be used to make games professionally !

Unity steps on the pit to learn small knowledge points

Unity uses Quaternion.AngleAxis to randomize a direction

In the process of doing a project, sometimes you will encounter the function of randomly obtaining a direction. Here is a brief introduction to a method Quaternion.AngleAxis .

public static Quaternion AngleAxis(float angle, Vector3 axis);

Create a rotation axis around the rotation angle degrees.

We can use the Random.Range() method to randomly assign parameters to obtain a random quaternion, and then we can change the direction according to this number, as you can see at a glance with the following simple example.

Each time you click the left mouse button, a bullet in a random direction can be generated. The effect is as follows:
insert image description here

The complete code is as follows:

using UnityEngine;

public class RangeDemo : MonoBehaviour
{
    
    
    public GameObject bulletPrefab;

    [Header("速度")]
    public float speed = 5f;


    void Update()
    {
    
    
        if (Input.GetMouseButtonDown(0))
        {
    
    
            //随机一个方向
            Quaternion dir = Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up);

            //生成子弹
            GameObject go = Instantiate(bulletPrefab,transform.position, dir);

            //三秒后销毁物体
            Destroy(go,3);
        }
    }
}

How to move bullets

   void Update()
    {
    
    
        transform.position += transform.forward;
    }

Change the parameter range in AngleAxis to control the random direction range, for example, change it to 180 as follows:

insert image description here

Please add image description

   void Update()
    {
    
    
        if (Input.GetMouseButtonDown(0))
        {
    
    
            //随机一个方向
            Quaternion dir = Quaternion.AngleAxis(Random.Range(0, 180), Vector3.up);

            //生成子弹
            GameObject go = Instantiate(bulletPrefab,transform.position, dir);

            //三秒后销毁物体
            Destroy(go,3);
        }
    }

insert image description here

Guess you like

Origin blog.csdn.net/zhangay1998/article/details/124339402