- Home /
I figured out where I went wrong. and now works.
Having trouble with flipping my projectile.
Hello my name is Treven and am trying to flip the local scale of my projectile when shot to match my player's direction. I have a simple instantiation for my projectile but when I put in this piece of code, the projectile stretches and turns invisible while the scale is told to be the same. I have made a separate scene and scripts to try and figure this out but no luck.
Heres what I got to flip my projectile.
Flip Code
if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), myRigidbody.velocity.y);
}
What is supposed to happen is to use the object's rigid body to see if the object is moving then to get the transform's local scale (X) to the have it be positive or negative depending on direction.
But what happens is the projectile turns invisible, the scale dose not change, and the scale of the object gets stretched.
The projectile works like intended until I put in the script to try flipping the projectile.
Any help in finding a solution would be much appreciated. Full Code and Player Script Below:
Player Control Script
public class SimplePlayertesterScript : MonoBehaviour
{
[Header("Player Movement")]
[SerializeField] float playerWalkSpeed = 5f;
[SerializeField] GameObject projectileChargeBuster = null;
[SerializeField] GameObject projectilePrefab = null;
bool hasHorizontalSpeed()
{
if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
{
return true;
}
else
{
return false;
}
}
[SerializeField] public float hori;
[SerializeField] float vert;
Rigidbody2D myRigidbody;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
hori = Input.GetAxisRaw("Horizontal");
vert = Input.GetAxisRaw("Vertical");
Vector2 playerVelocity = new Vector2(playerWalkSpeed * hori, myRigidbody.velocity.y);
myRigidbody.velocity = (playerVelocity);
if (hasHorizontalSpeed())
{
if (Mathf.Abs(hori) > 0)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), transform.localScale.y);
}
}
if (Input.GetKey("z"))
{
SpawnStandardProjectile();
}
}
void SpawnStandardProjectile()
{
GameObject projectile = Instantiate(projectilePrefab, projectileChargeBuster.transform.position, projectileChargeBuster.transform.rotation) as GameObject;
if (transform.localScale.x >= 0)
{
projectile.GetComponent<SimpleProjectileTest>().projectileMoveSpeed *= 1;
}
else
{
projectile.GetComponent<SimpleProjectileTest>().projectileMoveSpeed *= -1;
}
}
}
Spawning the Projectile:
public class SimpleProjectileTest : MonoBehaviour
{
[SerializeField] public float projectileMoveSpeed = 3;
[SerializeField] float projectileLifeTime = 3;
Rigidbody2D myRigidbody;
// Start is called before the first frame update
void Start()
{
//transform.parent = FindObjectOfType<ContainerManager>().projectileContainer.transform;
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
myRigidbody.velocity = transform.right * projectileMoveSpeed;
if (Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), myRigidbody.velocity.y);
}
ProjectileLife();
}
private void ProjectileLife()
{
Destroy(gameObject, projectileLifeTime);
}
}