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 Dreave · Nov 20, 2011 at 06:14 PM · playeraienemy

Stay Back!

How would I modify this script so that if the enemy gets about 1 metre close to the player it stops?

 var target : Transform; 

var moveSpeed = 3;

var rotationSpeed = 3;

var myTransform : Transform;

function Awake()

{

 myTransform = transform; 

}

function Start()

{

  target = GameObject.FindWithTag("Player").transform; 



}

function Update () {

 var lookDir = target.position - myTransform.position;

 lookDir.y = 0;

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

 Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);

 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by FLASHDENMARK · Nov 20, 2011 at 07:20 PM

Please when posting code highlight it and press the "101010" button and it will format it nicely like my code is:

 var target : Transform; //Player goes here, the enemy will "chase" and measure distance using this object.
 var moveSpeed = 3;
 var rotationSpeed = 3;
 var myTransform : Transform; //Cache our transform to avoid unnecessary calculation 
 
 function Awake(){
     myTransform = transform; //Store our transform
 }
 
 function Start(){   
     target = GameObject.FindWithTag("Player").transform; //Acquire our target
 }
 
 function Update () {
 //Measuring distance
 var dist = Vector3.Distance(target.position, myTransform.position);
     //Doing some fancy stuff.
     var lookDir = target.position - myTransform.position;
 
     lookDir.y = 0;
 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 
     Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
     //Checking if the player is more than 1 meter away if so -> don't move at all.
     if(dist > 1){
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }

You use [Vector3.Distance][1] to check if the enemy is more than 1 meter away.

Be aware that Vector3.Distance is quiet expensive when called every frame on several objects. [1]: http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html

Comment
Add comment · Show 9 · 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 Dreave · Nov 20, 2011 at 08:01 PM 0
Share

Nothing seems to change I have added this code to my script and in the inspector put my player to other, have I done all this right?

avatar image LegionIsTaken · Nov 20, 2011 at 09:48 PM 0
Share

What do you mean by "put my player to other"?

avatar image Dreave · Nov 21, 2011 at 07:47 AM 0
Share

In the inspector theres a variable called other, I have put the player there.

avatar image FLASHDENMARK · Nov 21, 2011 at 08:58 PM 0
Share

?

There are only 4 variables exposed:

var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var myTransform : Transform;

You must have written a variable of "other" yourself.

avatar image Dreave · Nov 22, 2011 at 04:09 PM 0
Share

This is my script is it all correct?

var other : Transform;

if (other) {

 var dist = Vector3.Distance(other.position, transform.position);

 print ("Distance to other: " + dist);

}

var target : Transform;

var moveSpeed = 3;

var rotationSpeed = 3;

var myTransform : Transform;

function Awake()

{

 myTransform = transform; 

}

function Start() {

  target = GameObject.FindWithTag("Player").transform; 



}

function Update () {

 var lookDir = target.position - myTransform.position;

 lookDir.y = 0;

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

 Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);

 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}

Show more comments
avatar image
0

Answer by billykater · Nov 20, 2011 at 07:31 PM

http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html

Comment
Add comment · Show 1 · 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 LegionIsTaken · Nov 20, 2011 at 07:58 PM 0
Share

The answer is right, but why bother posting when some one already posted the exact same?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Follow Script Help 2 Answers

Shooting Damage Help 1 Answer

Player damage stops working over time 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy AI things to consider 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