- Home /
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,
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();
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 :)
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.
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
What have you turned back from?
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).
Here is my problem video I hope it is helpful to understand. http://www.youtube.com/watch?v=ZDBa9JPuGwA
The video appears to be unavailable, so I am sorry, but I could not view it.
Your answer
Follow this Question
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