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 Unity3DJava · Mar 18, 2013 at 02:25 AM · javascriptenemy

Hello everyone, I want the enemy to not look at the enemy while shooting here is my code (code works):

var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false;

var bulletFab : Rigidbody; var gunTip : Transform; var power : float = 1000; var shotDelay : float = 1;

function Update () {

 currentDist = Vector3.Distance(transform.position, player.position);
 
 if(currentDist < safeDist){
 transform.LookAt(player);
 if(!shooting) shootStuff();
 
 }
 

}

function shootStuff(){

 shooting = true;
 

 

 
 var fwd = transform.TransformDirection(Vector3.forward);
 var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
 bulletShot.AddForce(fwd * power);
 
 yield WaitForSeconds(shotDelay);
 shooting = false;
 
 

}

Comment
Add comment · Show 1
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 bodec · Mar 18, 2013 at 03:12 AM 0
Share

whats the question?

1 Reply

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

Answer by robertbu · Mar 18, 2013 at 05:16 AM

I think what you are asking for is the code below, but the behavior may be strange. That is when the shooting stops after the 'shotDelay' the game object will immediately 'jerk' to the new position if the player has moved. If this is a spawn point, it won't matter, but if it is a visible object...

 if(currentDist < safeDist){
     if(!shooting) {
         transform.LookAt(player);
         shootStuff();
     }
 }
Comment
Add comment · Show 5 · 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 Unity3DJava · Mar 18, 2013 at 05:25 PM 0
Share

Ok thanks everyone for the code, but what I am asking for is for the enemy to not look at the player while shooting, to just shoot basically, with out any sense of direction.

avatar image robertbu · Mar 18, 2013 at 05:32 PM 0
Share

This code stops the LookAt() while shooting, so your player can move and the shots will still be at the position when the firing started. If don't want him to turn at all, then remove the LookAt(). If you want him to start shooting in random directions, then that is a whole other question and needs a lot of clarification about the limits of the randomness. Can he shoot anywhere or still in the general direction of the target for example? How accurate do you want him to be? Do you want the randomness to track the player or only where he was when the shooting started?

avatar image Unity3DJava · Mar 18, 2013 at 06:10 PM 0
Share

Ok heres my modified code it works but the shotDelay does not can you help. Thanks in advance. (changing the execution works but then the shotDelay stops after one execution) var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false;

var bulletFab : Rigidbody; var gunTip : Transform; var power : float = 1000; var shotDelay : float = 1;

function Update() {

shootStuff();

} function shootStuff(){

 shooting = true;
 

 

 
 var fwd = transform.TransformDirection(Vector3.forward);
 var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
 bulletShot.AddForce(fwd * power);
 
 yield WaitForSeconds(shotDelay);
 shooting = false;
 
 

}

avatar image robertbu · Mar 18, 2013 at 06:56 PM 0
Share

Inside Update() make shooting conditional:

 if (!shooting)
     shootStuff();
avatar image Unity3DJava · Mar 18, 2013 at 07:45 PM 0
Share

You sir are a genius ;) Thanks A lot!!!!!.

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

11 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

Related Questions

How to make the enemy move back to its waypoint after it's target killed? 2 Answers

Clones of enemies will not shoot 0 Answers

get closest enemy 1 Answer

I've got a coding problem please help 1 Answer

AI getting stuck on corners 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