unity 物体移动

实验目的

(1)掌握3D物体中脚本组件的创建。

(2)掌握通过脚本代码让物体移动的三种方法。

(3)掌握“世界坐标系”及“物体自身坐标系”的概念

(4)掌握输入控制的方法

实验内容及步骤

(1)在Unity中复制实验2的场景(有油桶、plane等)

(2)在油桶对象中创建脚本组件,并编写脚本,实现:当运行unity时,按下键盘的w、s、a、d键

,油桶可以向自身坐标系的前后左右移动。

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

public class Move : MonoBehaviour
{
    public float speed;
    Rigidbody rb;
    public Transform target;
    // Start is called before the first frame update
    private void Awake()
    {
        Debug.Log(" this is" + gameObject.name + "Awake");
    }
    void Start()
    {
        Debug.Log(" this is" + gameObject.name + "start");
        rb = GetComponent<Rigidbody>();
        rb.MovePosition(new Vector3(0,0,0));
    }
    private void OnEnable()
    {
        Debug.Log("this is" + gameObject.name + "OnEnable");
    }
    private void OnGUI()
    {
        Debug.Log("this is" + gameObject.name + "ongui");
    }
    // Update is called once per frame
    void Update()
    {
       if (Input.GetKey(KeyCode.W))
        {
           transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime, Space.Self);
       }
        if (Input.GetKey(KeyCode.A))
        {
           transform.Translate(new Vector3(0, 0, -1) * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(new Vector3(0, -1, 0) * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(new Vector3(0, 0, 1) * Time.deltaTime, Space.Self);
        }
        Debug.Log("this is" + gameObject.name + "Update");
    }
    private void FixedUpdate() 
    {
        //if (Input.GetKey(KeyCode.W));
        //{
        //    Debug.Log("Input:" + Input.GetAxis("Horizontal"));
        //    transform.Translate(new Vector3(Input.GetAxis("Horizontal"), 0, 0) * Time.deltaTime, Space.Self);
       // }
        
        //transform.position = Vector3.MoveTowards(transform.position, target.position,0.1f);
        //transform.Translate(new Vector3(0, 1, 0)*Time.deltaTime , Space.Self);//使用物体坐标系
        //transform.position = transform.position + new Vector3(0,0,1) * Time.deltaTime * speed;
        //根据世界坐标轴移动
        //transform.position = transform.position + transform.forward * Time.deltaTime * speed;
        //根据自身的坐标轴移动
        Debug.Log("this is" + gameObject.name + "FixedUpdate");
    }
    private void OnDestroy()
    {
        Debug.Log("this is" + gameObject.name + "ondestroy");
    }
}

实验总结

1、收获

  1. 学会了使用编写unity脚本,脚本是一种特殊的组件,是由程序语言编写的一段命令。通过这些指令编译游戏的对象行为。
  2. Vecotor3,表示三维向量,在做3d项目的时候,经常会使用到向量。
  3. Transform组件是游戏对象必备的一个组件,主要控制一些对象在unity场景中位置旋转和大小的比例。
  4. 在unity中可以通过Time类获取和事件相关的信息,可以用来计算帧速度,调整事件流时速度。

2、存在的问题

  1. 在使用W,A,S,D改变物体自身方式我使用的是第一种比较笨的方法,将物体的移动坐标改为了相应的数值
  2.    //if (Input.GetKey(KeyCode.W));

        //{

        //    Debug.Log("Input:" + Input.GetAxis("Horizontal"));

        //    transform.Translate(new Vector3(Input.GetAxis("Horizontal"), 0, 0) * Time.deltaTime, Space.Self);

       // }

这种方法存在缺陷Input.GetKey(KeyCode.W) 检测的是“W”键是否被按下,而 Input.GetAxis("Horizontal") 检测的是水平轴上的输入(通常是“A”和“D”键)。这种设计逻辑上可能不太直观,因为用户可能期望“W”键与某种向前的移动关联起来,而不是水平移动。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蕊。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值