标签:last GameObject 代码 Vector3 transform Csharp new allbody
效果:不碰撞,一起运动。但无法旋转,速度不一。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//在预制体实例化与C sharp类的方法之间出现了矛盾。
public class body_generate : MonoBehaviour
{
public GameObject prefab;
private Vector3 oldpos;
public Rigidbody rd;
//public Transform self;
List<GameObject> allbody;
// Start is called before the first frame update
void Start()
{
allbody = new List<GameObject>();
allbody.Add(this.gameObject);
rd = this.gameObject.transform.GetComponent<Rigidbody>();
// GameObject first = new GameObject("one");
// GameObject last = GameObject.Instantiate(prefab);
//last.AddComponent(typeof(Rigidbody));
// allbody.Add(last);
}
void Update()
{
Transform self = allbody[0].transform;
if (Input.GetKeyDown(KeyCode.Q))
{
//迭代替换位置的方式:第4个会被撞飞
/*
oldpos = allbody[0].transform.position;
allbody[0].transform.position = oldpos + new Vector3(1, 0, 0);
for (int i = 1; i < allbody.Count; i++)
{
allbody[i].transform.position = oldpos;
}
if (allbody[allbody.Count - 1].transform.position[0] >= 1)
{
GameObject last = GameObject.Instantiate(prefab);
last.AddComponent(typeof(Rigidbody));
last.transform.parent = allbody[allbody.Count-1]
allbody.Add(last);
}
*/
//施加力的方式
oldpos = allbody[0].transform.position;
allbody[0].transform.position = oldpos + new Vector3(1, 0, 0);
/*
for (int i = 0; i < allbody.Count; i++)
{
oldpos = allbody[i].transform.position;
allbody[i].transform.position = oldpos + new Vector3(1, 0, 0);
}*/
if(allbody[allbody.Count - 1].transform.position[0] >= 1)
{
GameObject last = GameObject.Instantiate(prefab);
last.AddComponent(typeof(Rigidbody));
last.transform.parent = allbody[allbody.Count - 1].transform;
//last.transform.localPosition = new Vector3(1, 0, 0);//设置位置 已实现不碰撞的固定位置的实例化。
//last.transform.localRotation = Quaternion.identity;//设置角度
allbody.Add(last);
}
}
/*
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//Debug.Log(h);
rd.AddForce(new Vector3(h, 0, v));
/*for (int i = 1; i < allbody.Count; i++)
{
allbody[i].transform.localPosition = new Vector3(0, 0, 0);
allbody[i].transform.localRotation = Quaternion.identity;
}*/
// GameObject third = GameObject.CreatePrimitive(PrimitiveType.Capsule);
}
}
testforce施加到预制体上:代码如下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testforce : MonoBehaviour
{
public Rigidbody rd;
// Start is called before the first frame update
void Start()
{
//this.gameObject.AddComponent(typeof(Rigidbody));
rd = this.gameObject.transform.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
rd.AddForce(Vector3.right);
}
if (Input.GetKeyDown(KeyCode.H))
{
rd.AddForce(Vector3.left);
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//Debug.Log(h);
rd.AddForce(new Vector3(h, 0, v));
}
}
标签:last,GameObject,代码,Vector3,transform,Csharp,new,allbody 来源: https://blog.csdn.net/weixin_45547419/article/details/121097491
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。