• 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 Tekksin · Nov 04, 2013 at 09:57 PM · 2ddirectionshoot

If player faces right, shoot right. viceversa?

I made a script that tells unity when my player faces right or doesn't.

var isRight : boolean = true;

function update(){

if(Input.GetKey("a")){ //Player moves left AT.rowNumber = 1; isRight = false; } else if(Input.GetKey("d")){ AT.rowNumber = 1; isRight = true;

}

Unfortunately my projectile script (the speed and direction the projectile goes) is in another script. How do I reference the player script so that I can say in the projectile script

"if(isRight == true){

 transform.Translate(Vector3(-speed * Time.deltaTime, 0, 0));
 Destroy(gameObject, 1);

}

and

if(isRight = "false"{ transform.Translate(Vector3(speed * Time.deltaTime, 0, 0)); Destroy(gameObject, 1); }

something like that? How do I go about this? total noob here so any suggestions and help is greatly appreciated!

Comment
Add comment
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

1 Reply

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

Answer by Cains · Nov 04, 2013 at 10:35 PM

You need to clean up the formatting as most of your code isn't in code format, making it hard to read.

If I understood correctly all you want is to be able to reference the isRight variable from your player script in your projectile script. Include the below in your projectile script, and substitute "Player" for your player gameobject's name and PlayerScript for the name of the script on the player gameobject that isRight is in.

 var player : PlayerScript;
 
 function Start() {
     player = GameObject.Find("Player").GetComponent(PlayerScript);
 }

Then when you need to use it you can do it like this

 if(player.isRight == true){
     transform.Translate(Vector3(-speed * Time.deltaTime, 0, 0));
     Destroy(gameObject, 1);
 }



Another tip is that for booleans you don't have to write out the true/false comparisons.

"if(player.isRight)" instead of "if(player.isRight == true)"

"if(!player.isRight)" instead of "if(player.isRight == false)"

Comment
Add comment · Show 9 · 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 Tekksin · Nov 04, 2013 at 11:07 PM 0
Share

you're right, my formatting was ridiculously bad in this post. in my other questions it's often spotless and easily readable, but I was in the middle of trying things and didn't want to spend too much time asking a question when I usually find an answer on my own with enough time.

But your suggestion is masterful and exactly what I was looking for.

There is only one issue, though. The script currently reads:

 var speed : int = 5;
 var player : Player;
 
 
 function Start () {
      player = GameObject.Find("Player").GetComponent(Player);
 }
 
 function Update () {
     if(player.isRight){
             transform.Translate(Vector3(-speed * Time.deltaTime, 0, 0));
             Destroy(gameObject, 2);
         }
     if(!player.isRight){
             transform.Translate(Vector3(speed * Time.deltaTime, 0, 0));
             Destroy(gameObject, 2);
         }
 
 }

The projectile fires right when he is facing right. The projectile fires left when he is face left. But if he is facing right, the fired projectile goes right, until he faces left, in which case the projectile then starts to travel left.

How how I set it so that the projectile stays traveling in one direction? Ins$$anonymous$$d of going back and forth during the course of its life before being destroyed based on whether or not the isRight boolean is true or false? I hope that was understandable. Thanks for the help!!

avatar image Cains · Nov 04, 2013 at 11:54 PM 0
Share

It's understandable, I can't count how many times I've started writing up a question only to think of the answer right in the middle.

I haven't done much with projectiles, but I'm fairly sure this'll work for what you want. Basically the way you had it every projectile's direction was dependent on which way the player is currently facing. You want to store which direction the player was facing when the projectile is created, and use that direction only.

 var speed : int = 5;
 var player : Player;
 var travelRight : boolean;
  
  
 function Start () {
     player = GameObject.Find("Player").GetComponent(Player);
     travelRight = player.isRight;
 }
  
 function Update () {
     if(travelRight) {
         transform.Translate(Vector3(-speed * Time.deltaTime, 0, 0));
         Destroy(gameObject, 2);
     }
     else if(!travelRight) {
         transform.Translate(Vector3(speed * Time.deltaTime, 0, 0));
         Destroy(gameObject, 2);
     }
 }

You might want to look into the Lerp function which can easily be used to create fluid movement for things like projectiles (though if they're extremely fast like a bullet there's not much point).

Any other questions feel free to ask.

avatar image Tekksin · Nov 05, 2013 at 02:09 AM 0
Share

you are a god among men!! thank you! :D

avatar image Tekksin · Nov 05, 2013 at 06:54 PM 0
Share

hey, as far as the

  player = GameObject.Find("Player").GetComponent(Player);

goes, how do I reference the prefab as well? the script breaks when the player dies. So how do I say "find player, and find the prefab if it dies, too." ?

avatar image Cains · Nov 05, 2013 at 11:42 PM 0
Share

I need a little more info:

  1. How does the script break?

  2. What exactly happens when your player "dies", is the Player GameObject destroyed?

  3. Overall, what is your game supposed to do at this point?

I don't think you'll want to reference the prefab for the fact that it's a prefab, and not an actual GameObject in the scene. Prefabs are just templates for creating GameObjects. In my $$anonymous$$d this script shouldn't be running at all if the player is dead because they shouldn't be able to shoot projectiles when they're dead, but I don't really know what your game is all about.

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

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Shoot second cell in 2d 0 Answers

2D Shooting & Direction 1 Answer

Bullets direction is not good (network) 0 Answers

How can I raycast the direction my 2D character is facing? 1 Answer

Why is my GameObject rotation snapping to a certain angle when he reaches its destination? 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