Getting projectile to leave marks at where its hit, for example different spot of a building of a 2D game?
Hi everyone, I am a total beginner to game making and programming, I am trying to make a gameplay about shooting things onto building and leaving marks of where it landed in a 2D Zelda-liked game, for example, the player is able to shot the projectile and leave marks on door, windows, walls, different parts of the building based on the distance between the player and the building.
My very naive approach is to record the projectile bornTime, entryTime (where the project touches the building box collider), and determine a value of airBorneTime,, so that I get the travelTime (that allows the projectile 'travels' within the building box collider, let say 0.1s ) and after the travelTime, I would turn off the isTrigger to make the projectile stay in place,
BUT after I spent a couple of days to make the code running, it didnt work, instead, the projectile just bounces out of the boxCollider area.
I am wondering if anyone could point me to a better direction/approach? I am very new to programming too, this was my best shot so far help please!! thank you so much!
here is my clunky code (for some reason the projectile is called boba) public class Boba : MonoBehaviour { public float speed; public Rigidbody2D myRigidbody;
public float lifetime;
private float lifetimeSeconds;
private Animator animator;
public int statesNumber;
private float bornTime;
private float enterTime;
private float travelTime;
public float airBorneTime;
Collider2D myCollider;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
lifetimeSeconds = lifetime;
animator = GetComponent<Animator>();
animator.SetBool("isLanded", false);
bornTime = Time.time;
}
public void Setup(Vector2 velocity, Vector3 direction)
{
myRigidbody.velocity = velocity.normalized * speed;
transform.rotation = Quaternion.Euler(direction);
myRigidbody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
myCollider = GetComponent<Collider2D>();
}
private void Update()
{
lifetimeSeconds -= Time.deltaTime;
if (lifetimeSeconds <= 0)
{
Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
enterTime = Time.time;
}
private void OnTriggerStay2D(Collider2D collision)
{
StartCoroutine(bobaEnterCollider());
bobaEnterCollider();
}
private IEnumerator bobaEnterCollider()
{
travelTime = airBorneTime - (enterTime - bornTime);
yield return new WaitForSeconds(travelTime);
myCollider.isTrigger = false;
animator.SetBool("isLanded", true);
animator.SetInteger("bobaLandedRandom", Random.Range(0, statesNumber));
}
}
Your answer
Follow this Question
Related Questions
make an enemy spell move towards player 1 Answer
Firing Projectile on path,Firing Rockets on path 0 Answers
How do you make one game object follow the shape of another game object? 0 Answers
Rotate projectile sprite in the direction it is flying (like an arrow) 2D. 1 Answer
Firing a projectile change the direction of the other projectiles. 1 Answer