unity--c#控制小球运动

9 篇文章 0 订阅
本文介绍了如何在Unity中建立桌球场景模型,调整材质颜色,以及使用C#编写代码控制小球的销毁、弹跳和移动。涉及到的关键技术包括Unity对象操作、材质编辑和基本C#脚本编写。
摘要由CSDN通过智能技术生成

一、先在unity中建立一个所需的模型

先放入一个plane,在在四周放上cube并缩放至所需长度围成一个类似于桌球的桌子,再放入一个sphere即小球。

二、调色

在project中的assets里如图建立一个Maierial,可以进行rename改名字。将建立好的拖入scene中你想要改变颜色的模块上,点击,右边可以改变颜色。

三、最后成型的样子

我建的比较粗糙,大概能理解即可,也可以把四周对齐,比较美观。

四、建立代码存放处

在Assets中建立create一个folder(文件夹),明明为Scripts,在这里边存放我们的c#代码。

五、控制小球运的c#代码

在scripts里建立一个c#文件,命名test或者你能记得明白的(再次申明:要养成良好的命名习惯)

1.销毁:四秒后销毁小球

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

public class test : MonoBehaviour
{
   
    void Start()//开始调用一次
    {
       
        Destroy(gameObject, 4f);//销毁
    }

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

2.点击销毁

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

public class test : MonoBehaviour
{
   
    // Start is called before the first frame update
    void Start()//开始调用一次
    {
        
    }

    // Update is called once per frame
    void Update()
    {
       
    }
    private void OnMouseDown()
    {
        Destroy(gameObject);
    }
}

3.点击空格销毁

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

public class test : MonoBehaviour
{
  
    // Start is called before the first frame update
    void Start()//开始调用一次
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            Destroy(gameObject);
          
        }
        
    }
   
}

4、空格弹跳

先点击小球,在右边界面点击Add component搜素Rigidbody就是刚体

其中use Gravity是使用重力,勾上。

在c#里将Rigidbody引进去,命名rb

Rigidbody rb;//刚体

初始化一下

rb = GetComponent<Rigidbody>();//初始化

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

public class test : MonoBehaviour
{
    Rigidbody rb;//刚体
   

    // Start is called before the first frame update
    void Start()//开始调用一次
    {
        rb = GetComponent<Rigidbody>();//初始化
       

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
           
            rb.AddForce(Vector3.up * 500);//上弹
        }
        
    }
  
}

此时,小球有重力了,我们就可以玩一下小球啦。

再放入一个cube,勾选is trigger

5.小球前后左右移动

edit里project settings的input Manager,点开Axes,Horizontal是相当于a和d键,left和right,Vertical是相当于w和s键

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

public class test : MonoBehaviour
{
    Rigidbody rb;//刚体
    Vector3 move;
    public float spend = 5f;//速度

    // Start is called before the first frame update
    void Start()//开始调用一次
    {
        rb = GetComponent<Rigidbody>();//初始化
        
    }

    // Update is called once per frame
    void Update()
    {
        
        move.x = Input.GetAxis("Horizontal");
        move.z = Input.GetAxis("Vertical");
        move = new Vector3(move.x, 0, move.z);
        rb.AddForce(move * spend);
    }
   
}

,down和up。名字不可以拼错。

ctrl+shift+/:注释

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值