C# 在游戏对象上运行一次脚本
我有一个脚本,它使用Unity引擎的C# 在游戏对象上运行一次脚本,c#,unity3d,C#,Unity3d,我有一个脚本,它使用Unity引擎的lerp功能处理游戏对象随时间的转换 public class MovePiece : MonoBehaviour { Vector3 startPoint; Vector3 endPoint; float speed = 1; float startTime; float journeyLength; void Start() { startPoint = gameObject.GetComp
lerp
功能处理游戏对象随时间的转换
public class MovePiece : MonoBehaviour {
Vector3 startPoint;
Vector3 endPoint;
float speed = 1;
float startTime;
float journeyLength;
void Start() {
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(20, 0, 0); //Will make this a variable eventually
startTime = Time.time;
journeyLength = Vector3.Distance(startPoint, endPoint);
}
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
}
}
公共类MovePiece:monoBehavior{
矢量3起始点;
向量3终点;
浮动速度=1;
浮动起始时间;
浮动行程长度;
void Start(){
startPoint=gameObject.GetComponent().position;
endPoint=startPoint+newvector3(20,0,0);//最终将使其成为变量
startTime=Time.Time;
行程长度=矢量3.距离(起点、终点);
}
无效更新(){
float distCovered=(Time.Time-startTime)*速度;
浮动行程=距离覆盖/行程长度;
transform.position=Vector3.Lerp(起点、终点、终点);
}
}
然而,我只希望在我点击一个游戏对象时运行一次,然后在该对象移动完毕后将其自身与该对象分离,以便下次我点击另一个对象时可以再次运行
我有一个光线投射设置连接到相机,它允许我选择游戏对象,我只是不知道如何在选择的对象上运行这个脚本
如何执行此操作?重命名开始方法
void Start() { ... }
其他组件可以公开访问的内容,即:
public void BeginMove() { ... }
然后,让您的Raycast脚本调用新函数。您需要访问已单击的游戏对象的MovePiece组件:
...
GameObject hitObject = raycastHit.collider.gameObject;
MovePiece hitObjectMovePiece = hitObject.getComponent<MovePiece>();
hitObjectMovePiece.BeginMove();
。。。
GameObject hitObject=raycastHit.collider.GameObject;
MovePiece hitObjectMovePiece=hitObject.getComponent();
hitObjectMovePiece.BeginMove();
您可能还希望在动画中引入布尔锁。下面是您的代码可能的样子
public class MovePiece : MonoBehaviour {
Vector3 startPoint;
Vector3 endPoint;
float speed = 1;
float startTime;
float journeyLength;
// Animation lock
private bool moving = false;
public void BeginMove() {
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(20, 0, 0); //Will make this a variable eventually
startTime = Time.time;
journeyLength = Vector3.Distance(startPoint, endPoint);
moving = true;
}
void Update() {
if(!moving)
return;
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
if(fracJourney >= 1.0f)
moving = false;
}
}
公共类MovePiece:monoBehavior{
矢量3起始点;
向量3终点;
浮动速度=1;
浮动起始时间;
浮动行程长度;
//动画锁
私有布尔移动=假;
public void BeginMove(){
startPoint=gameObject.GetComponent().position;
endPoint=startPoint+newvector3(20,0,0);//最终将使其成为变量
startTime=Time.Time;
行程长度=矢量3.距离(起点、终点);
移动=真;
}
无效更新(){
如果(!移动)
返回;
float distCovered=(Time.Time-startTime)*速度;
浮动行程=距离覆盖/行程长度;
transform.position=Vector3.Lerp(起点、终点、终点);
如果(分形行程>=1.0f)
移动=假;
}
}
我希望这有帮助!祝你好运。重命名开始方法
void Start() { ... }
其他组件可以公开访问的内容,即:
public void BeginMove() { ... }
然后,让您的Raycast脚本调用新函数。您需要访问已单击的游戏对象的MovePiece组件:
...
GameObject hitObject = raycastHit.collider.gameObject;
MovePiece hitObjectMovePiece = hitObject.getComponent<MovePiece>();
hitObjectMovePiece.BeginMove();
。。。
GameObject hitObject=raycastHit.collider.GameObject;
MovePiece hitObjectMovePiece=hitObject.getComponent();
hitObjectMovePiece.BeginMove();
您可能还希望在动画中引入布尔锁。下面是您的代码可能的样子
public class MovePiece : MonoBehaviour {
Vector3 startPoint;
Vector3 endPoint;
float speed = 1;
float startTime;
float journeyLength;
// Animation lock
private bool moving = false;
public void BeginMove() {
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(20, 0, 0); //Will make this a variable eventually
startTime = Time.time;
journeyLength = Vector3.Distance(startPoint, endPoint);
moving = true;
}
void Update() {
if(!moving)
return;
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, fracJourney);
if(fracJourney >= 1.0f)
moving = false;
}
}
公共类MovePiece:monoBehavior{
矢量3起始点;
向量3终点;
浮动速度=1;
浮动起始时间;
浮动行程长度;
//动画锁
私有布尔移动=假;
public void BeginMove(){
startPoint=gameObject.GetComponent().position;
endPoint=startPoint+newvector3(20,0,0);//最终将使其成为变量
startTime=Time.Time;
行程长度=矢量3.距离(起点、终点);
移动=真;
}
无效更新(){
如果(!移动)
返回;
float distCovered=(Time.Time-startTime)*速度;
浮动行程=距离覆盖/行程长度;
transform.position=Vector3.Lerp(起点、终点、终点);
如果(分形行程>=1.0f)
移动=假;
}
}
我希望这有帮助!祝你好运。这似乎应该可以,但是我在调用
BeginMove()的行中得到了一个对象引用,它没有设置为对象的实例function@James嗯,你的对象的碰撞器在子变换中吗?确保“MovePiece”组件位于具有碰撞器的同一游戏对象上,或者将“MovePiece”放置在根游戏对象上,并更改此行:“GameObject hitObject=raycastHit.Collider.GameObject;”对此:“GameObject hitObject=raycastHit.collider.transform.root.GameObject;”在Raycast函数中打印一个快速的“Debug.Log(hitObject.name+“selected”)”,这样你就可以很容易地判断到底是哪个对象被选中了。目前离开计算机时,我生病了,所以我不能尝试,我不是粗鲁无礼,我会尽快给你回复。这看起来应该行得通,但是,在调用BeginMove()
function@James嗯,你的对象的碰撞器在子变换中吗?确保“MovePiece”组件位于具有碰撞器的同一游戏对象上,或者将“MovePiece”放置在根游戏对象上,并更改此行:“GameObject hitObject=raycastHit.Collider.GameObject;”对此:“GameObject hitObject=raycastHit.collider.transform.root.GameObject;”在Raycast函数中打印一个快速的“Debug.Log(hitObject.name+“selected”)”,这样你就可以很容易地判断到底是哪个对象被选中了。目前离开计算机时我生病了,所以我不能尝试,我不是粗鲁,我会给你回复的ASAP@FarhanAnam,c#和unity都是新手,所以我不知道这是什么afraid@FarhanAnam对c#和unity来说都是全新的,所以我恐怕不知道那是什么