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 alcohollica · Jul 24, 2014 at 03:30 PM · javascript

Stopping a function when another function running.

How I do it? I make platformer shooter game.My movement code here,

 var sp = 25.0f;
 var jsp = 10.0f;
 var health = 5;
 var dmg = 1;
 var X : float;
 var isPaused;
 static var bl : boolean;
 var ss : AudioClip;
 var fire : Transform;
 var barrelEnd : Transform;
 
 function Start(){
     X = transform.localScale.x;
     GameObject.Find("Health").guiText.text = "Your health is: " + health;
 }
 function Update () {
     if (Input.GetKeyDown(KeyCode.P)) {
           isPaused = !isPaused;
      }
     Time.timeScale = isPaused ? 0 : 1;
     
     if(Input.GetKey(KeyCode.D)){
         transform.position.x += sp * Time.deltaTime;
         transform.localScale.x = X;
         bl = true;
         
     }
     
     if(Input.GetKey(KeyCode.A)){
         transform.position.x -= sp * Time.deltaTime;
         transform.localScale.x = -X;
         bl = false;
         
     }
     
     if(Input.GetKey(KeyCode.W)){
         transform.position.y += jsp * Time.deltaTime;
     }
     
     
     if(health == 0){
         Application.LoadLevel(0);
     }
     
     if(Input.GetButtonDown("Fire1")){
         AudioSource.PlayClipAtPoint(ss, transform.position);
             
         Instantiate(fire, barrelEnd.position, barrelEnd.rotation);
         
     }
     
 
     
     
     
 }
 
 function OnCollisionEnter(col : Collision){
     
     if(col.gameObject.name == "EnemyRight"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
     
     if(col.gameObject.name == "EnemyLeft"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
     
     if(col.gameObject.name == "EnemyRight(Clone)"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
     
     if(col.gameObject.name == "EnemyLeft(Clone)"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
     
 }

And my fire code here,

 var sp = 5.0f;
 
 
 function Update () {
         
     if (Movement.bl == true){
         Right();
         
     }
     
     if (Moevement.bl == false){
         Left();
         
     }
 }
 
 function Right(){
     transform.position.x += sp * Time.deltaTime;
 }
 
 function Left(){
     transform.position.x -= sp * Time.deltaTime;
 }


My problem is when I push Fire1 it's work very well, fire object go right direction but when I shoot and flip left or right wherever it is when I flip reverse direction, my fired bullet turn same direction.I hope you understand what I tell.

Illustrated lecture here,

alt text

dasd.png (22.3 kB)
dasd.png (22.3 kB)
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 DajBuzi · Jul 24, 2014 at 06:50 PM 0
Share

if this Fire script that you've posted is the same as in game than you have to get reference to the movement script like:

 var mov : $$anonymous$$ovement = GetComponent("$$anonymous$$ovement");

and then in update:

 if (mov.bl) Right();
 else Left();

avatar image alcohollica · Jul 25, 2014 at 03:58 PM 0
Share

It did not work

2 Replies

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

Answer by alcohollica · Jul 31, 2014 at 01:10 PM

Hey everyone, I just fixed it my problem, if anyone has that problem, I fixed like that,

First of all I change Fire Prefab move speed for my order. So here's,

 var sp = 25.0f;
 var jsp = 10.0f;
 var health = 5;
 var dmg = 1;
 var X : float;
 var isPaused;
 static var bl : boolean;
 var ss : AudioClip;
 var fire : Transform;
 var barrelEnd : Transform;
 var canShoot : boolean = True;
 function Start(){
     X = transform.localScale.x;
     GameObject.Find("Health").guiText.text = "Your health is: " + health;
 }
 function Update () {
     if (Input.GetKeyDown(KeyCode.P)) {
           isPaused = !isPaused;
      }
     Time.timeScale = isPaused ? 0 : 1;
  
     if(Input.GetKey(KeyCode.D)){
         transform.position.x += sp * Time.deltaTime;
         transform.localScale.x = X;
         bl = true;
  
     }
  
     if(Input.GetKey(KeyCode.A)){
         transform.position.x -= sp * Time.deltaTime;
         transform.localScale.x = -X;
         bl = false;
  
     }
  
     if(Input.GetKey(KeyCode.W)){
         transform.position.y += jsp * Time.deltaTime;
     }
  
  
     if(health == 0){
         Application.LoadLevel(0);
     }
  
     if(canShoot){
         var cl;
         if (Input.GetButtonDown("Fire1") && bulletPrefab && bl==true){
                     
             cl = Instantiate(bulletPrefab, pistol.position, pistol.rotation);        
             AudioSource.PlayClipAtPoint(ss, transform.position);    
             cl.GetComponent(fireScript).moveSpeed = 2.0;
         }
         
         if (Input.GetButtonDown("Fire1") && bulletPrefab && bl==false){
                     
             cl = Instantiate(bulletPrefab, pistol.position, pistol.rotation);        
             AudioSource.PlayClipAtPoint(ss, transform.position);    
             cl.GetComponent(fireScript).moveSpeed = -2.0;
         }
     }    
  
  
  
  
  
 }
  
 function OnCollisionEnter(col : Collision){
  
     if(col.gameObject.name == "EnemyRight"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
  
     if(col.gameObject.name == "EnemyLeft"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
  
     if(col.gameObject.name == "EnemyRight(Clone)"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
  
     if(col.gameObject.name == "EnemyLeft(Clone)"){        
         health -= dmg;
         Debug.Log("Your health is: " + health);
         GameObject.Find("Health").guiText.text = "Your health is: " +health;
     }
  
 }

And my firePrefab code here,

 var moveSpeed : float     = 1.0;
 
 
 function Update(){
     transform.Translate(moveSpeed,0,0);
 }
 
 
 
 
 
 function OnTriggerEnter(col : Collider){
     if(col.gameObject.name == "EnemyRight"){
         
         
         
         
         Destroy(col.gameObject);
         Destroy(gameObject);
     }
     
     if(col.gameObject.name == "EnemyLeft"){
         
         
         
         Destroy(col.gameObject);
         Destroy(gameObject);
     }
     
     if(col.gameObject.name == "EnemyRight(Clone)"){
         
         
         
         Destroy(col.gameObject);
         Destroy(gameObject);
     
     }
     
     if(col.gameObject.name == "EnemyLeft(Clone)"){
         
         
         
         Destroy(col.gameObject);
         Destroy(gameObject);
     }
     
     
 }

I want to thank everyone who help me or who try to help me :)

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
1

Answer by JusticeAShearing · Jul 25, 2014 at 04:24 PM

I have recently been working with instantiatation, and thus have found information on forward thrust on those instantiated objects, because most instantiators are after precisely the same thing as you.

From what I gather, you could certainly use an instantiated bullet. Use this script, unless you want the bullet to keep going forever. If so, ask me in a comment. I have heavily annotated this code so that you may understand and use it again.

Instructions:

You Will Need:

  • A template bullet. Put a gameObject of your bullet in the game world anywhere. Give this template bullet everything that you want the instantiated bullets to have.

  • The instantiation script, applied to your firearm

This file is in C#.

 //C# Instantiator Script. Apply it to your character.
 
 using UnityEngine;
 using System.Collections;
 
 public class UsingInstantiate : Monobehaviour
 {
     //The rigidbody is your template bullet
     public Rigidbody bulletPrefab;
     //The transform is your pistol
     public Transform pistol;
     
     void Update ()
     {
         //If the player uses the primary fire key
         if(Input.GetButtonDown("Fire1"))
         {
             Rigidbody bulletInstance;
             //The bullet is instantiated
             bulletInstance = Instantiate(bulletPrefab, pistol.position, pistol.rotation) as rigidbody;
             //Now we add a lot of force to it
             bulletInstance.AddForce(pistol.forward * 5000);
         }
     }
 }

That comprises both movement of the bullet and the instantiation of the bullet into one script.

Tell me if you have any worries. If it works, please accept the answer properly. Lots of people don't.

Comment
Add comment · Show 4 · 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 alcohollica · Jul 25, 2014 at 06:40 PM 0
Share
 var bulletPrefab : Rigidbody;
 var pistol : Transform;
 
 if (Input.GetButtonDown("Fire1")){
         var bulletInstance : Rigidbody;
         puan++;
         bulletInstance = Instantiate(bulletPrefab, pistol.position, pistol.rotation);        
         AudioSource.PlayClipAtPoint(ss, transform.position);
         bulletInstance.AddForce(50000 * Time.deltaTime, 0,0);            
      
     }
         

I convert to JS but if I turn back it does not change everytime bullet goes this (-->) direction

avatar image JusticeAShearing · Jul 26, 2014 at 04:33 PM 0
Share
  1. What have you turned back from?

  2. When you speak of the bullet moving in a certain direction, please speak of it from the character's point of view (relative to the character).

avatar image alcohollica · Jul 30, 2014 at 04:06 PM 0
Share

Here is my problem video I hope it is helpful to understand. http://www.youtube.com/watch?v=ZDBa9JPuGwA

avatar image JusticeAShearing · Sep 03, 2014 at 04:12 PM 0
Share

The video appears to be unavailable, so I am sorry, but I could not view it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Talking Code 2 Answers

In Game Animation 2 Answers

Post login script works but Unity doesn't knows it 2 Answers

A node in a childnode? 1 Answer

Very little, but very heavy script problem. 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