unity教程之星球重力模拟
发表于2017-03-24
中午吃饭时,突然发想要怎麽模拟像七龙珠裡的界王星@@,趁还有记忆就赶快把它记录下来,上网查询了一下也已经有人做成英文视频教学,有兴趣的可以看英文版练英文听力XD,下面就给大家介绍下在unity中实现星球重力模拟的方法,一起来看看吧。
Unity Tutorial: Faux Gravity (walk on planets)
首先在场景中创建适合大小的 Sphere 模拟星球
接著创建代表玩家的 Capsule
完成基本物件创建后
开始来写程式脚本
建立 PlanetGravity.cs 脚本
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 | using UnityEngine;using System.Collections;public class PlanetGravity : MonoBehaviour{ //The gravity of the planet public float gravity = 10; private Transform m_transform; void Awake() { m_transform = transform; } public void AddGravity(Transform targetObject) { //The gravity direction of the planet Vector3 gravityDirection = (targetObject.position - m_transform.position).normalized; //Add the gravity to the target object targetObject.GetComponent().AddForce(-gravity * gravityDirection); }} |
这个脚本目前单纯用来产生星球重力
利用 RigidBody.AddForce 这个API
来产生对物体中心的力
以此来模拟星球重力
接下来建立 PlayerGravity.cs 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using UnityEngine;using System.Collections; public class PlayerGravity : MonoBehaviour{ public PlanetGravity planetGravity; private Transform m_transform; void Awake() { m_transform = transform; } void Update() { planetGravity.AddGravity(m_transform); }} |
在 MonoBehaviour.Update() 中对物体产生重力
赋予 Sphere 物件 PlanetGravity.cs
赋予 Capsule 物件 PlayerGravity.cs 及 Rigidbody
设置如下
开始游戏并尝试拖拉 Capsule 物件
发现已经有重力产生了
但 Capsule 物件并不会随重力的角度改变
回到 PlanetGravity.cs 脚本来增加一些修改
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 | using UnityEngine;using System.Collections; public class PlanetGravity : MonoBehaviour{ //The gravity of the planet public float gravity = 10; private Transform m_transform; void Awake() { m_transform = transform; } public void AddGravity(Transform targetObject) { //The gravity direction of the planet Vector3 gravityDirection = (targetObject.position - m_transform.position).normalized; //Add the gravity to the target object targetObject.GetComponent().AddForce(-gravity * gravityDirection); //Change the up direction of the target object to the reverse direction of gravity Vector3 targetUpDirection = targetObject.up; Quaternion targetRotation = Quaternion.FromToRotation(targetUpDirection, -gravityDirection) * targetObject.rotation; targetObject.rotation = Quaternion.Slerp(targetObject.rotation, targetRotation, Time.deltaTime * 100); }} |
星球重力已经完成了
来让玩家可以在上面做简单的移动
建立 PlayerController.cs 脚本
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 | using UnityEngine;using System.Collections; public class PlayerController : MonoBehaviour{ public float moveSpeed = 10; private Transform m_transform; private Vector3 m_moveDirection; private Rigidbody m_rigidBody; void Awake() { m_transform = transform; m_rigidBody = GetComponent(); } void Update() { m_moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized; } void FixedUpdate() { m_rigidBody.MovePosition(m_transform.position + m_transform.TransformDirection(m_moveDirection) * moveSpeed * Time.deltaTime); }} |
将 PlayerController 脚本赋予 Capsule 物件后
我们更改 Camera 的位置
让他可以跟随 Capsule 物件
最后可以摆上各种物件
来做各种模拟
