- Home /
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!
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)"
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!!
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.
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." ?
I need a little more info:
How does the script break?
What exactly happens when your player "dies", is the Player GameObject destroyed?
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.