• 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 oinkoinkflapflap · Dec 25, 2010 at 09:40 PM · positiondistanceturretaiming

making an object stop responding with an object at a certain distance away

i have a turret aiming at my character, how do i get it to stop when my character is at a certain distance?

var LookAtTarget:Transform;

var damp = 6.0;

var laserPrefab:Transform;

var savedTime=0;

function Update () {
if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

 var seconds : int = Time.time;
 var oddeven = (seconds % 2);

 if(oddeven)
 {       
     Shoot(seconds);
 }

}

} function Shoot(seconds) { if(seconds!=savedTime) { var laser = Instantiate(laserPrefab ,transform.Find("bang").transform.position, Quaternion.identity); laser.gameObject.tag = "Untagged";
laser.rigidbody.AddForce(transform.forward * 10000);

    savedTime=seconds;
}

}

thanks :D

Comment
Add comment · Show 2
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 FLASHDENMARK · Dec 26, 2010 at 12:14 PM 0
Share

Good old TornadoTwins! :)

avatar image oinkoinkflapflap · Dec 29, 2010 at 03:33 PM 0
Share

yeah, it's cus of them i found unity

1 Reply

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

Answer by The_r0nin · Dec 26, 2010 at 01:05 AM

Add in the Update function:

if (Vector3.Distance(lookAtTarget.position,transform.position) <= 10){

 // your code here.

}

Two points of advice. First, naming conventions in Unity/Javascript usually begin variables with a lowercase letter and functions with an uppercase one (i.e. lookAtTarget, as opposed to LookAtTarget()...). It will help you keep track of what you are doing (and you'll be surprised at how easy it is to lose track).

Second, you might want to test against the square of the distance you want rather than the distance. Every square root sucks up more computing power, and the end result is the same. Your code would look like:

if ((lookAtTarget.position-transform.position).sqrMagnitude <= 100){ // 100 is 10 squared
Comment
Add comment · Show 6 · 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 oinkoinkflapflap · Dec 29, 2010 at 03:30 PM 0
Share

yeah i do know how easy it is 2 lose track, haha, god scripting is confusing!!!! thanks 4 the help and pointers, :D

avatar image oinkoinkflapflap · Dec 29, 2010 at 03:32 PM 0
Share

oh you say // your code here, is that the code i posted as a question??

avatar image The_r0nin · Dec 30, 2010 at 02:54 AM 0
Share

Yes. Check the distance, then have it rotate to your character (put everything in your Update function in this).

avatar image The_r0nin · Dec 30, 2010 at 02:55 AM 0
Share

Also, if this works for you, click the checkmark next to the answer so everyone will know that it is solved.

avatar image oinkoinkflapflap · Jan 30, 2011 at 08:33 PM 0
Share

the only problem is that it says error "expecting "(" found "Update" :/

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Turrets |Aiming don't work 1 Answer

How to find the position a specific distance away in a specific direction? 2 Answers

Left/right, up/dwn of me detection 2 Answers

Automated turret targets around origin, regardless of location 1 Answer

Finding a Vector3 from Quaternion, current Vector3 and Distance 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