Unity中如何编写代码实现小球撞击墙壁的动画效果?

2026-05-25 04:391阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计740个文字,预计阅读时间需要3分钟。

Unity中如何编写代码实现小球撞击墙壁的动画效果?

小球打击方块介绍 + 自动生成障碍 + 射线透视准器,同时发射小球 + 角色控制(角色控制器CharacterController)+ 摄像机跟随 + 制作物步进器。1. 构建基本场景 + 添加弹性体:CharacterController,Rigidbody

Unity中如何编写代码实现小球撞击墙壁的动画效果?

小球打方块 介绍
  1. 自动生成围墙
  2. 射线瞄准器,并且发射小球
  3. 角色控制移动(角色控制器CharacterController)
  4. 摄像机跟随
制作步骤 1. 构建基本场景


胶囊体需要添加:CharacterController,RigidBody组件

2. 控制角色移动(CharacterController)

脚本直接挂载在胶囊体上。
下方注释代码也可以与移动胶囊体

3. 生成Walls


4. 射线瞄准器 效果

射线一端放在胶囊体上,一段指向射线碰撞的地方

获取一些必要的属性

发出射线

注意发出射线的脚本必须挂载在Camera下才行

注意:正常使用可能射线颜色不会改变,需要在材质里改一下这个Shader

Update中实时检测射线碰撞的位置

当射线有碰撞的物体并且鼠标左键按下,才会发射小球

摄像头跟随

摄像头跟随胶囊体移动而移动,并且目标始终在胶囊体上

源码一:物体移动

注意脚本名和源码类名一致

using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveTarget : MonoBehaviour { CharacterController cc; float speed =0; private void Start() { cc=this.gameObject.GetComponent<CharacterController>(); speed =10f*Time.deltaTime; } void Update() { if(Input.GetKey(KeyCode.W))cc.Move(transform.forward*speed); if(Input.GetKey(KeyCode.S))cc.Move(-1f*transform.forward*speed); if(Input.GetKey(KeyCode.A))cc.Move(-1f*transform.right*speed); if(Input.GetKey(KeyCode.D))cc.Move(transform.right*speed); // if(Input.GetKey(KeyCode.W))cc.Move(Vector3.forward*speed); // if(Input.GetKey(KeyCode.S))cc.Move(Vector3.back*speed); // if(Input.GetKey(KeyCode.A))cc.Move(Vector3.left*speed); // if(Input.GetKey(KeyCode.D))cc.Move(Vector3.right*speed); } } 源码二:核心代码

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Test : MonoBehaviour { // Start is called before the first frame update public Transform tf;//主角 public Material material;//射线颜色 RaycastHit hit;//碰撞的位置 private void Start() { //构建walls for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.AddComponent<Rigidbody>(); obj.transform.position=new Vector3(0,2f*i,1f*j); } } } void OnPostRender(){ //画线 material.SetPass(0); GL.Begin(GL.LINES); // GL.Color(Color.green); GL.Vertex(hit.point); GL.Vertex(tf.position); GL.End(); } private void Update() { //检测射线碰撞,发出小球 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray,out hit)&&Input.GetMouseButtonDown(0)){ GameObject addobj =GameObject.CreatePrimitive(PrimitiveType.Sphere); addobj.AddComponent<Rigidbody>(); addobj.transform.position=tf.position+Vector3.up*1f; addobj.GetComponent<Rigidbody>().AddForce(100f*(hit.point-tf.transform.position)); Destroy(addobj,3); } } private void LateUpdate() { //摄像机跟随 this.transform.position = tf.position-tf.forward*4f+tf.up*2f; this.transform.LookAt(tf.transform); } }

本文共计740个文字,预计阅读时间需要3分钟。

Unity中如何编写代码实现小球撞击墙壁的动画效果?

小球打击方块介绍 + 自动生成障碍 + 射线透视准器,同时发射小球 + 角色控制(角色控制器CharacterController)+ 摄像机跟随 + 制作物步进器。1. 构建基本场景 + 添加弹性体:CharacterController,Rigidbody

Unity中如何编写代码实现小球撞击墙壁的动画效果?

小球打方块 介绍
  1. 自动生成围墙
  2. 射线瞄准器,并且发射小球
  3. 角色控制移动(角色控制器CharacterController)
  4. 摄像机跟随
制作步骤 1. 构建基本场景


胶囊体需要添加:CharacterController,RigidBody组件

2. 控制角色移动(CharacterController)

脚本直接挂载在胶囊体上。
下方注释代码也可以与移动胶囊体

3. 生成Walls


4. 射线瞄准器 效果

射线一端放在胶囊体上,一段指向射线碰撞的地方

获取一些必要的属性

发出射线

注意发出射线的脚本必须挂载在Camera下才行

注意:正常使用可能射线颜色不会改变,需要在材质里改一下这个Shader

Update中实时检测射线碰撞的位置

当射线有碰撞的物体并且鼠标左键按下,才会发射小球

摄像头跟随

摄像头跟随胶囊体移动而移动,并且目标始终在胶囊体上

源码一:物体移动

注意脚本名和源码类名一致

using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveTarget : MonoBehaviour { CharacterController cc; float speed =0; private void Start() { cc=this.gameObject.GetComponent<CharacterController>(); speed =10f*Time.deltaTime; } void Update() { if(Input.GetKey(KeyCode.W))cc.Move(transform.forward*speed); if(Input.GetKey(KeyCode.S))cc.Move(-1f*transform.forward*speed); if(Input.GetKey(KeyCode.A))cc.Move(-1f*transform.right*speed); if(Input.GetKey(KeyCode.D))cc.Move(transform.right*speed); // if(Input.GetKey(KeyCode.W))cc.Move(Vector3.forward*speed); // if(Input.GetKey(KeyCode.S))cc.Move(Vector3.back*speed); // if(Input.GetKey(KeyCode.A))cc.Move(Vector3.left*speed); // if(Input.GetKey(KeyCode.D))cc.Move(Vector3.right*speed); } } 源码二:核心代码

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Test : MonoBehaviour { // Start is called before the first frame update public Transform tf;//主角 public Material material;//射线颜色 RaycastHit hit;//碰撞的位置 private void Start() { //构建walls for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.AddComponent<Rigidbody>(); obj.transform.position=new Vector3(0,2f*i,1f*j); } } } void OnPostRender(){ //画线 material.SetPass(0); GL.Begin(GL.LINES); // GL.Color(Color.green); GL.Vertex(hit.point); GL.Vertex(tf.position); GL.End(); } private void Update() { //检测射线碰撞,发出小球 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray,out hit)&&Input.GetMouseButtonDown(0)){ GameObject addobj =GameObject.CreatePrimitive(PrimitiveType.Sphere); addobj.AddComponent<Rigidbody>(); addobj.transform.position=tf.position+Vector3.up*1f; addobj.GetComponent<Rigidbody>().AddForce(100f*(hit.point-tf.transform.position)); Destroy(addobj,3); } } private void LateUpdate() { //摄像机跟随 this.transform.position = tf.position-tf.forward*4f+tf.up*2f; this.transform.LookAt(tf.transform); } }