jyhlearning

导航

unity案例

实现摄像机根据鼠标绕游戏对象旋转

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

public class Scripts_07_11 : MonoBehaviour
{
    public Transform target;
    public float distance = 20.0f;
    float x, y, yMinLimit=-20f, yMaxLimit=80f,xSpeed=250.0f, ySpeed=120.0f;
    // Start is called before the first frame update
    void Start()
    {
        Vector2 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        if (gameObject.GetComponent<Rigidbody>())
            gameObject.GetComponent<Rigidbody>().freezeRotation = true;
    }
    float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
    // Update is called once per frame
    void Update()
    {
        if(target != null)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            Quaternion roation = Quaternion.Euler(y, x, 0);
            Vector3 position = roation * new Vector3(0.0f, 0.0f, -distance) + target.position;//四元数和向量相乘表示这个向量绕这个四元数转动后的向量
            gameObject.transform.rotation = roation;
            gameObject.transform.position = position;//删去位置改变可以实现视角转动效果
        }
        Debug.Log(Input.GetAxis("Mouse X"));
    }
}

播放动画

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

public class Scripts_07_12 : MonoBehaviour
{
    public string anim_name0 = "HumanoidCrouchdle", anim_name1 = "HumanoidCrouchWalkLeft", anim_name2 = "Humanoidldle", anim_name3 = "HumanoidMidAir";
    private GameObject obj;
    // Start is called before the first frame update
    void Start()
    {
        obj = GameObject.Find("ThirdPersonController");
        obj.GetComponent<Animation>().wrapMode = WrapMode.Loop;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
            obj.GetComponent<Animation>().Play(anim_name0);
        if(Input.GetKeyDown(KeyCode.B))
            obj.GetComponent<Animation>().Play(anim_name1);
        if (Input.GetKeyDown(KeyCode.C))
            obj.GetComponent<Animation>().Play(anim_name2);
        if (Input.GetKeyDown(KeyCode.D))
            obj.GetComponent<Animation>().Play(anim_name3);
    }
}

剪切动画

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

public class Scripts_07_13 : MonoBehaviour
{
    public GameObject obj;
    // Start is called before the first frame update
    void Start()
    {
        obj = GameObject.Find("ThirdPersonController");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        if (GUILayout.Button("播放完整动画")) 
            obj.GetComponent<Animation>().Play("HumanoidCrouchTurnLeft");
        if (GUILayout.Button("切割动画0-50帧"))
            PlayCuttingAnimation(obj, 0, 50);
    }
    void PlayCuttingAnimation(GameObject obj, int startFrame, int endFrame)
    {
        AnimationClip clip = obj.GetComponent<Animation>().clip;
        obj.GetComponent<Animation>().AddClip(clip, "cutClip", startFrame, endFrame);
        obj.GetComponent<Animation>().Play("cutClip");
    }
    void PlayCombinedAnimation(GameObject manObject, int startFrame =0, int endFrame =0, int startFrame1 =0,int endFrame1= 0 )
    {
        AnimationClip clip = manObject.GetComponent<Animation>().clip;
        manObject.GetComponent<Animation>().AddClip(clip, "startChip", startFrame, endFrame, false);
        manObject.GetComponent<Animation>().AddClip(clip, "endChip", startFrame1, endFrame1, false);
        manObject.GetComponent<Animation>().PlayQueued("startChip", QueueMode.PlayNow);//等先前的动画结束后才会开始播放,避免跳帧现象QueueMode.PlayNow:立即开始播放。这可以用于只想快速创建重复动画的情况。
        manObject.GetComponent<Animation>().PlayQueued("endChip", QueueMode.CompleteOthers);//等先前的动画结束后才会开始播放,CompleteOthers:等先前的动画结束后才会开始播放
    }
}

Slider控制播放进度

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

public class Scripts_07_14 : MonoBehaviour
{
    public string anim_name;
    private GameObject obj;
    public float hSliderValue = 0.0f, animLength = 0.0f;
    private new Animation animation;
    // Start is called before the first frame update
    void Start()
    {
        obj = GameObject.Find("man");
        animation = obj.GetComponent<Animation>();
        animLength = animation[anim_name].length;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        string show = "当前动画长度" + hSliderValue + "(s)" + "/" + animLength + "(s)";
        GUILayout.Label(show);
        hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0f, 5.0f, GUILayout.Width(200));
        PlaySliderAnimation(hSliderValue);
    }
    void PlaySliderAnimation(float times)
    {
        if(!animation.IsPlaying(anim_name))//判断当前正在播放的动画
        {
            animation.Play(anim_name);
        }
        animation[anim_name].time = times;//设置当前播放动画的时间
    }
}

使用线渲染器绘制三段连在一起的线

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

public class Scripts_07_21 : MonoBehaviour
{
    private GameObject LineRenderGameObject;
    private LineRenderer lineRenderer;
    private int lineLength = 4;
    private Vector3 v0 = new Vector3(1.0f, 0.0f, 0.0f);
    private Vector3 v1 = new Vector3(2.0f, 0.0f, 0.0f);
    private Vector3 v2 = new Vector3(3.0f, 0.0f, 0.0f);
    private Vector3 v3 = new Vector3(4.0f, 0.0f, 0.0f);

    // Start is called before the first frame update
    void Start()
    {
        LineRenderGameObject = GameObject.Find("GameObject");
        lineRenderer = LineRenderGameObject.GetComponent<LineRenderer>();
        lineRenderer.positionCount = lineLength;//设置线中的点的数量
        lineRenderer.startWidth = 1.0f;
    }

    // Update is called once per frame
    void Update()
    {
        lineRenderer.SetPosition(0, v0);//设置线中点的位置
        lineRenderer.SetPosition(1, v1);
        lineRenderer.SetPosition(2, v2);
        lineRenderer.SetPosition(3, v3);
    }
}

使用网格渲染器渲染游戏对象

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

public class Scripts_07_22 : MonoBehaviour
{
    Vector3 v0 = new Vector3(5, 0 , 0);
    Vector3 v1 = new Vector3(0, 5 , 0);
    Vector3 v2 = new Vector3(0, 0 , 5);

    Vector3 v3 = new Vector3(-5, 0, 0);
    Vector3 v4 = new Vector3(0, -5, 0);
    Vector3 v5 = new Vector3(0, 0, -5);

    Vector2 u0 = new Vector2(0, 0);
    Vector2 u1 = new Vector2(0, 5);
    Vector2 u2 = new Vector2(5, 5);

    Vector2 u3 = new Vector2(0, 0);
    Vector2 u4 = new Vector2(0, 1);
    Vector2 u5 = new Vector2(1, 1);

    // Start is called before the first frame update
    void Start()
    {
        mesh.Clear();
        MeshFilter meshFilter = GameObject.Find("Cube").GetComponent<MeshFilter>();
        Mesh mesh = meshFilter.mesh;
        mesh.vertices = new Vector3[] {v0, v1, v2, v3, v4, v5};
        mesh.uv = new Vector2[] {u0, u1, u2, u3, u4, u5};
        mesh.triangles = new int[] { 0, 1, 2, 3, 4, 5 };
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

posted on 2022-08-13 19:30  jyhlearning  阅读(86)  评论(0编辑  收藏  举报