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 MWW · Dec 18, 2012 at 10:48 AM · aimoveshoot

how to combine my move script and my shoot script into 1 script

HI , Can someone please help , I Have 2 Javascripts, I am trying to combine the 1st Script Makes the Enemy follow and Look at the Player. The 2nd Script have the Enemy shoot at the Player and also follow the player when the player is in range. But the scripts doesn't work well together I want to combine them into one script.

Script # 1 ( The Follow Script) ------------------

var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;

function Start () {

}

function Update () { transform.LookAt(Player);

if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){

transform.position += transform.forward*MoveSpeed*Time.deltaTime;

if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something }

} }


Script #2 The shooting script_

var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; 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); 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 Catlard · Dec 18, 2012 at 10:56 AM 0
Share

If you select the code in your question and click the button with 1's and 0's on it, it will indent it, so I can read it. Do this and I will try to help you.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by MWW · Dec 18, 2012 at 11:22 AM

  `enter code here` 

 var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; 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); bulletShot.AddForce(fwd * power);
 
 yield WaitForSeconds(shotDelay); shooting = false;
 
 } 
 
 
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
avatar image
0

Answer by MWW · Dec 18, 2012 at 11:21 AM

 var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;
 
 function Start () {
 
 }
 
 function Update () { transform.LookAt(Player);
 
 if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){
 
 transform.position += transform.forward*MoveSpeed*Time.deltaTime;
 
 if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something } 
 
 } } __________________________________________________ _____________________________________________
 
 
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
avatar image
0
Wiki

Answer by MWW · Dec 18, 2012 at 11:21 AM

 var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;
 function Start () {
 }
 function Update () { transform.LookAt(Player);
 if(Vector3.Distance(transform.position,Player.posi tion) >= MinDist){
 transform.position += transform.forward*MoveSpeed*Time.deltaTime;
 if(Vector3.Distance(transform.position,Player.posi tion) <= MaxDist) { //Here Call any function U want Like Shoot at here or something } 
 } } __________________________________________________ 
 
 
 
 
 
 
 var player : Transform; var safeDist : float = 15; var currentDist : float; var shooting : boolean = false; var bulletFab : Rigidbody; 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); bulletShot.AddForce(fwd * power);
 
 yield WaitForSeconds(shotDelay); shooting = false;
 
 } 



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

10 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

Related Questions

My asteroid dont move after shoot - why? 1 Answer

Shoot a bomb to mouse position smooth 1 Answer

enemy shoot AI precision 1 Answer

Auto shooting script. 1 Answer

AI problems ( enemy doesn't shoot ) 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