I can't figure out why this variable is not updating
Here is a script I'm writing
using UnityEngine;
public class SkunkProjectile : Projectile
{
private bool doesProjectileMoveRight;
public override void FireProjectile(bool facingRight)
{
if(facingRight)
{
Debug.Log("Facing right");
doesProjectileMoveRight = false;
}
else
{
Debug.Log("Facing left");
doesProjectileMoveRight = true;
}
Debug.Log("fire projectile " + doesProjectileMoveRight);
}
protected override void CalculateTrajectory()
{
Debug.Log("Calculate trajectory " + doesProjectileMoveRight);
}
private void FixedUpdate()
{
CalculateTrajectory();
}
}
When I run it doesProjectileMoveRight is always false. Even when I reach the else case in Fire projectile. Can someone help me understand why this is happening?
Btw, if you want to try and talk about what I'm doing. I'm trying to make a projectile fire from an enemy. I've tried rigidBody2D.velocity and addForce but neither work because I can't get any updated information from my FireProjectile function.
One solution I found was to pass the game object that controls the firing call in order to get which direction it is facing but is there a more efficient solution to this? It seems like passing large data structures like game objects seems excessive.
Here is the updated code:
using System.Collections;
using UnityEngine;
/// <summary>
/// The projectile class is the base class for all projectiles.
/// It is responsible for when the projectile spawns and when it is destroyed.
/// </summary>
public class Projectile : $$anonymous$$onoBehaviour
{
[SerializeField]protected GameObject firingObject;
[SerializeField]protected Rigidbody2D rb;
protected int moveSpeed;
public Rigidbody2D GetRigidbody() { return rb; }
public void SetVelocity(Vector2 velocity) { rb.velocity = velocity; }
private void Start()
{
//Start the coroutine we define below named ExampleCoroutine.
StartCoroutine(ExampleCoroutine());
}
/**
This is a very simple function to destory projectiles after a set amount of time.
Better solutions will be to destroy the projectile if it collides with certian objects,
or if the projectile travels off screen.
*/
IEnumerator ExampleCoroutine()
{
//yield on a new YieldInstruction that waits for 3 seconds.
yield return new WaitForSeconds(2);
Destroy(this.gameObject);
}
public void FireProjectile(Transform spawnPosition, GameObject projectileLauncher)
{
Instantiate(this,
new Vector3(spawnPosition.position.x, spawnPosition.position.y, spawnPosition.position.z),
new Quaternion(0.0f,0.0f,0.0f,0.0f));
firingObject = projectileLauncher;
}
}
using UnityEngine;
///<summary>
///Child class of projectile
///</summary>
public class SkunkProjectile : Projectile
{
private void Update()
{
if(firingObject.GetComponent<Skunk>().GetFacingRight())
{
rb.velocity = new Vector2(-2.0f, 0.0f);
}
else
{
rb.velocity = new Vector2(2.0f, 0.0f);
}
}
}
Your answer
Follow this Question
Related Questions
2D moving Script 1 Answer
Is there a way I can make one script for all my buttons in my game? 1 Answer
2D Projectile Script Not Working 1 Answer
How do I get my character to shoot towards my mouse in a 2D top down? 0 Answers
Get the Transform from Gameobjects in arrays to make a Line by Clicking the Mouse. 0 Answers