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 kidshenlong · Mar 27, 2012 at 12:49 AM · movementenemy

Enemy Approaching player and then stopping in front??

Hey guys need a quick bit of help. My game is a star fox clone essentally so maybe that'll give you an idea of what I'm trying to do. I was messing around with some code. I wanted the enemy to approach the player and when he gets within the "safe distance" to turn in the opposite direction and match the players speed and then later start attackign but one thing at a time!

 var player:GameObject;

var safeDistance = 20; //How close the player can get

public var speed =10;

function Update()

{

var player = GameObject.Find("Sphere(Clone)");

//transform.LookAt (player.transform); //you look at player with this

//write other codes here

var distanceToPlayer = Vector3.Distance(this.transform.position, player.transform.position);

// this checks to see if the player is too close then runs the fire function

if(distanceToPlayer <= safeDistance){

transform.Translate (Vector3(0,0,2));

print("forward");

}

else{

transform.Translate (Vector3(0,0,Random.Range(-1,-10))); //moves with speed in local z and you are looking toward him so you move toward the player

print("backwards");

}

}

function OnCollisionEnter(collision : Collision)

{

Destroy(gameObject);

}

So what am I doing wrong...?

:(

Thanks in advance guys!

Comment
Add comment · Show 8
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 ByteSheep · Mar 27, 2012 at 03:22 AM 0
Share

well what is not working? It's hard to guess - It'd be easier if we new whether you get any errors in the console etc. :)

avatar image kidshenlong · Mar 27, 2012 at 03:45 AM 0
Share

Sorry for being so vague!! It appears to just go straight to the else statement. The enemy is spawn about a good 500(not sure what measurement unity uses!!) in front if the player and doesn't bother approaching at all :S

avatar image ByteSheep · Mar 27, 2012 at 03:56 AM 0
Share

I would perhaps try debugging and narrowing it down further - for example check the distanceToPlayer variable's value using:

Debug.Log("Distance to player: "+distanceToPlayer);

It's only a guess but it's good to just check that all variables are fine and not causing any trouble :D

avatar image fafase · Mar 27, 2012 at 05:18 AM 0
Share

You could take off the var player = GameObject.Find("Sphere(Clone)"); which looks kinda weird to me, and simply drag your player object to the slot in the inspector. transform.LookAt (player); also.

avatar image ByteSheep · Mar 27, 2012 at 05:25 AM 0
Share

@fafase

var player = GameObject.Find("Sphere(Clone)");

  • looked a bit weird to me at first too, but it shouldn't cause any problems since it is searching for the object "Sphere(Clone)" and the brackets are just part of the name, I think.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kidshenlong · Mar 27, 2012 at 02:43 PM

Think I've figured it out! Thanks everyone but specifically merry-christmas I would've of discovered the issue if hadn't debugged.

Basically the object was moving too fast. 20 was too small a unit to use as it would count down and fly past me before it could even check if the enemy was close. the problem is when it flew past the player the distance would start building up again so it'd never detect being that close to me. Bumping safeDistance up to a 100 fixed the issue.

I hope that made sense.

Thanks for the input guys :)

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

6 People are following this question.

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

Related Questions

Vector3 MoveTowards character flying up when told to follow,Vector3 MoveTowards object flying up when I press play 0 Answers

2D Pathfinding Top Down 0 Answers

c# array of game objects movement help 1 Answer

Enemy AI Movement Decision Making 1 Answer

How to make an enemy pace? 1 Answer


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