【Unity3D】个人开发台球小游戏
作者:互联网
前期准备
-
台球桌尺寸:2.83 * 1.55 m
-
台球桌颜色:绿色
-
台球桌边框:木色
-
台球尺寸:d = 3.5 cm
-
台球颜色:从 Asset Store 下载
搭建场景
编写脚本
设置其中一个球为 Player,令其去碰撞别的球。桌面及桌框都带上 Physics Material,使其能够产生碰撞和摩擦。注意,将屏幕坐标转化为 Unity 的世界坐标时,是通过 Camera.main.ScreenToWorldPoint(Input.mousePosition) 来实现的,所以要将相机的 Projection 设置为 orthographic 模式。下面是整个的代码,主要学习碰撞体和触发器的实现。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Practice_ball : MonoBehaviour
{
public GameObject sphere;
public Transform launchPos;
public float forceVaule;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
var rigidbody = sphere.GetComponent<Rigidbody>();
if (rigidbody != null)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 forceDirection = mousePos - launchPos.position;
forceDirection = new Vector3(forceDirection.x, 0, forceDirection.z);
rigidbody.AddForce(forceDirection * forceVaule);
}
}
}
}
标签:rigidbody,Unity3D,台球,小游戏,台球桌,Input,using,forceDirection,public 来源: https://blog.csdn.net/weixin_49296842/article/details/115785778