How to assign Transform to prefab ?

How to assign Transform to prefab ?

I've been working for 2 days and i still didn't succeed :(

There's 2 spaceships i use...

alt text

and ther are using same code;

I wrote a smooth look at code using random methods. Bu I can't define my transform objects to prefab.

As you see;

alt text

I'm trying to drag drop them but it's not available.

      
  1. var targetsArray : Transform[];
  2. var damping = 6.0;
  3. var smooth = true;
  4. private var target = -1;
  5. function Start() {
  6. target = Random.Range(0, targetsArray.Length);
  7. }
  8. function LateUpdate () {
  9. if (smooth)
  10. {
  11. // Look at and dampen the rotation
  12. var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
  13. transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
  14. }
  15. }

and that's my code. So , Is there another method can i use ?

P.S. = I'm using 1 scene in my game. One level, one scene.

1条回复

 ·  添加您的回复
avatar image
2

个解答,截止robertbu 

A Prefab is like a blueprint for a game object (including scripts and children). It doesn't exist in the scene until it is Instantiated(). You cannot drag and drop scene items onto a prefab since it doesn't exist in the scene and can be Instantiated into any scene. You can drag and drop other object within the prefab...that is you can fixup internal links within the prefab, just not external ones.

As for your problem, you are going to have to figure out an alternate way to make these links. Something like:

      
  1. var targetsArray : Transform[] = new Transform[4];
  2. var damping = 6.0;
  3. var smooth = true;
  4. private var target = -1;
  5. function Start() {
  6. targetsArray[0] = GameObject.Find("1Follower").transform;
  7. targetsArray[1] = GameObject.Find("2Follower").transform;
  8. targetsArray[2] = GameObject.Find("3Follower").transform;
  9. targetsArray[3] = GameObject.Find("4Follower").transform;
  10. target = Random.Range(0, targetsArray.Length);
  11. }

An alternate method would be to use the tag manager to give all the followers the same tag. Then you could do:

      
  1. var targetsArray : GameObject[];
  2. var damping = 6.0;
  3. var smooth = true;
  4. private var target = -1;
  5. function Start() {
  6. targetsArray = GameObject.FindGameObjectsWithTag("Follower");
  7. target = Random.Range(0, targetsArray.Length);
  8. }

Note this alternate method has the entries of the 'targetsArray' as game object, not transforms.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值