Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Me · Dec 11, 2010 at 07:08 PM · instantiatetransformprefabupdateruntime

Prefabs Transforms LookAt

I have a Strange thing going on and need a solution if anyone knows?

I have a Enemy who's AI "Looks At" my Players Transform which is a variable in the Inspector (var Player : Transform).

If I drag and drop my Player from the Scene View onto the Player variable (for LookAt) everything works fine and the enemy is always going after my Player.

Heres the Problem:

If I instantiate the Enemy at runtime it wont remember or hold my players Transform, So Im thinking I need to add my players Prefab in the Inspector so I did.

If I drag my Players Prefab onto the var Player (for LookAt) the Enemy will go to where the players prefab was and not go after my player???

I need use the Players Prefab because when I instantiate the Enemy its the only way for it to Hold My Players Transform.

Ahh.. this is confusing anyone ever run into this problem? and would anyone know how I would solve this?

Im thinking the Players Prefab needs to be updated for the Enemy to know where I am know but I have no idea how to go about this or this may not be the right solution. Here's my Code Im hoping someone can help me sort this out?

var awareDistance : float = 15.0; var scaredDistance : float = 10.0; var player : Transform;

var runSpeed : float = 4.0;

enum AIStatus {Idle = 0, Scared = 1} private var status = AIStatus.Idle; //by default

var controller : CharacterController;

private var moveDirection = Vector3.zero;

//function Start loop animations function Start() { //set all animations to loop animation.wrapMode = WrapMode.Loop;
}

function Awake() { //maybe replace character controller with box collider & rigidBody for gravity controller = GetComponent(CharacterController); }

function Update() {

CheckStatus();

switch(status) { case AIStatus.Idle: Idle(); break;

 case AIStatus.Scared:
 RunAway();
 break;

}

}

function Idle() { animation.CrossFade ("idle2");
}

function RunAway() { //transform.eulerAngles.y = -player.transform.eulerAngles.y; //gets thier rotation from mine

// give enemy thier own rotation transform.LookAt(player.transform);

//to make them run the other way add 180 degrees //transform.eulerAngles.y += 180;

moveDirection = Vector3(0,0,40); //help our robot know hich way to go create a float variable to test it. moveDirection = transform.TransformDirection(moveDirection); //converst it to worldSpace moveDirection = runSpeed; //times 4.0 above controller.SimpleMove(moveDirection Time.deltaTime); // moves our enemy animation.CrossFade ("relax_walk1"); //and plays the run animation

}

function CheckStatus() { //player.position is what ever enemy I drag into the player varable above in the inspector var dist = (player.position - transform.position).magnitude; //returns the length for dist

if (dist < scaredDistance)
{
    status = AIStatus.Scared;   
}   
else if (dist > awareDistance)
{
    status = AIStatus.Idle; 
}

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Me · Dec 12, 2010 at 03:05 AM 0
Share

Wowowow I finally figured it out one little line of code drove me nuts but here it is and it has to be in the start function.

player = GameObject.FindWithTag("myrobot").transform;

avatar image DaveA · Jan 13, 2011 at 11:04 PM 0
Share

Yup. So either 'answer' it yourself or delete the question to clean up the list. Thanks

avatar image Posly · Jan 31, 2012 at 09:16 PM 0
Share

Thanks I was having the same problem and this solved it TY!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Can't remove instantiated prefab 0 Answers

Instantiate a prefab at runtime 2 Answers

Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers

Destroying & Then Spawning Quickly 1 Answer

Instantiating Custom Prefabs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges