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
0
Question by Velketor · Dec 31, 2012 at 03:42 PM · transformprefabrotatefunction

rot not working for 2nd prefab

This script is attached to a game object prefab. When I spawn the prefab, it rotates toward my player. When I spawn a 2nd prefab, the rotation no longer functions for the 2nd prefab ONLY. The first prefab still continues rotating correctly. What am I doing wrong? Why would it work for the 1st spawned game object but not the 2nd, 3rd, etc?

 var target = GameObject.Find("Aggro").transform; //the enemy's target
 var moveSpeed = 10; //move speed
 var rotationSpeed = 5000; //speed of turning
 var myTransform = this.transform; //current transform data of this enemy
 
 InvokeRepeating("Start", .1, 1);
 InvokeRepeating("Update", .1, .1);
 
 
 function Start()
 {
      target = GameObject.FindWithTag("aggro").transform; //target the player
      myTransform = this.transform;
 
 }
 
 function Update () {
     myTransform = this.transform;
     //rotate to look at the player
     this.myTransform.rotation = Quaternion.Slerp(this.myTransform.rotation,
     Quaternion.LookRotation(target.position - this.myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     this.myTransform.position += this.myTransform.forward * moveSpeed * Time.deltaTime;
 }

Any help is greatly appreciated. Thank you.

Comment
Add comment · Show 6
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 asafsitner · Dec 31, 2012 at 03:45 PM 0
Share

Where and how do you define what target is?

avatar image Velketor · Dec 31, 2012 at 03:52 PM 0
Share

var myTransform = this.transform;

avatar image asafsitner · Dec 31, 2012 at 03:55 PM 0
Share

What I meant was, where do you set the transform you want to look at?

You have myTransform set, but I don't see where you say who target is.

avatar image Velketor · Dec 31, 2012 at 03:59 PM 0
Share

oh i'm sorry. allow me to put the whole script. i only included the portion I was referencing because I thought it would make it easier to diagnose. i'm wrong though =p I just updated it.

avatar image GlennHeckman_old · Dec 31, 2012 at 04:30 PM 0
Share

$$anonymous$$, View some responses to your question in the other thread you started: http://answers.unity3d.com/questions/371797/restart-this-script.html

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 31, 2012 at 04:46 PM

I don't know why this script is working for the first enemy only - actually, it should not work for any one, maybe not even compile! You can't use many game object functions or properties in code outside functions, causing errors in the 1st and 4th lines, and calling Start and Update via InvokeRepeating definitely seems wrong - the engine calls them at the appropriate times (Time.deltaTime has no meaning if you call Update yourself). You should also declare moveSpeed and rotationSpeed as floats (or add a decimal point to the initialization values), because the way they're declared make them integers, possibly making some expressions return 0.
You should remove the InvokeRepeating lines and modify the variable declarations like below:

 var target: Transform; // just declare the variable type
 var moveSpeed: float = 10; // it's better to declare this as a float
 var rotationSpeed: float = 5000; // idem (the enemy rotates 5000 degrees per second!?!)
 var myTransform: Transform; // again, just declare the variable type

Remove also the first line of Update: you've already assigned myTransform at Start. Try these changes and let us know if the problem still persists.

Comment
Add comment · Show 2 · Share
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 Velketor · Dec 31, 2012 at 05:43 PM 0
Share

thanks for the help. it is still not working. unfortunately the line "var myTransform: Transform;" is throwing an error "cannot convert 'UnityEngine.Transform' to 'System.Type'"

this is what i get for trying to edit scripts myself. everyone see's my terrible errors and now the problem seems worse than before. i would have been better not modifying the script at all it seems. i appreciate your help. i'll just have to take this functionality out entirely since it just doesn't work =( i was so close too!

avatar image aldonaletto · Jan 01, 2013 at 12:53 AM 0
Share

Weird thing! This line is just a variable declaration - are you sure the error is in this line?

avatar image
0

Answer by deltamish · Jan 01, 2013 at 01:54 AM

Hi,like he said dont decalre object properties outside the function and when are instantiating the objects because you have specefied the target in Start function. so the object only works when you instantiate it at the start but if you instantiate it during an update it will rotate towards the player

Just change from

 function Start()
 {
      target = GameObject.FindWithTag("aggro").transform; //target the player
      myTransform = this.transform;
 
 }
 
 function Update () {
     myTransform = this.transform;
     //rotate to look at the player
     this.myTransform.rotation = Quaternion.Slerp(this.myTransform.rotation,
     Quaternion.LookRotation(target.position - this.myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     this.myTransform.position += this.myTransform.forward * moveSpeed * Time.deltaTime;
 }

To

 function Start(){
 mytransform = transform;
 }
 
 function Update(){
 target = GameObject.FindWithTag("aggro").transform;
 //your other code
 
 }

now it will work as the object checks for the player every frame

by the way Happy New Year To One And All. May this year be sucessful to you

Comment
Add comment · Share
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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to delete a Variable in a script in game 1 Answer

Assign a 'Transform' target to prefab by finding a 'GameObject' 1 Answer

"transform" in a static function 1 Answer

Moving objects in game using .transform does not stop 3 Answers

rotare arrow object to mouse position 0 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