Unity操作-脚本使物体移动+旋转

1.构建物体和对应的脚本

 2.完成脚本编辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ObjectMoving : MonoBehaviour
{
    // 初始化物体移动速度
    public float speed = 1;
    void Start()
    {
         
    }
 
 
    void Update()
    {
        //按下键盘上下左右操作键移动
        //上
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            transform.position += transform.forward * speed;
        }
        //下
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            transform.position += transform.forward * speed;
        }
        //左
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.eulerAngles = new Vector3(0, 270, 0);
            transform.position += transform.forward * speed;
        }
        //右
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.eulerAngles = new Vector3(0, 90, 0);
            transform.position += transform.forward * speed;
        }
    }
}

  3.旋转场景物体构建

 

 4.旋转脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class RotationRotate : MonoBehaviour
{
    // 初始化旋转坐标
    public Vector3 rotate;
    void Start()
    {
         
    }
 
    void Update()
    {
        //旋转坐标获取
        Vector3 vector = rotate * Time.deltaTime;
        //执行旋转
        this.transform.Rotate(vector, Space.Self);
    }
}

  

 

posted @   打工仔-也想飞  阅读(474)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示