How to assign Transform to prefab ?

来源:互联网 发布:手机淘宝首页psd模板 编辑:程序博客网 时间:2024/06/01 19:05

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.

ekran alıntısı.jpg (124.8 kB)
ekran alıntısı.jpg (121.4 kB)
评论

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.

0 0
原创粉丝点击