How do i create a return function for my shooting mechanic?
Iam building a shooting mechanics that consists on: shoot a bouncing ball that travels only thru the x axis (2D Game) but I also want it to return back to the player on using the same same input.
this is my shooting script
{ public Transform firePoint; public GameObject ballPrefab; // Update is called once per frame void Update() { if (Input.GetButtonDown("Fire2")) { Shoot(); }public class orbWeapon : MonoBehaviour
} void Shoot () { // shooting logic Instantiate(ballPrefab, firePoint.position, firePoint.rotation); }
} and this is my ball behaviour script
{ public Rigidbody2D rb; public float speed = 20f; Vector3 lastVelocity; // Start is called before the first frame update void Start() { rb.velocity = transform.right speed; } // Update is called once per frame void FixedUpdate() { lastVelocity = rb.velocity;public class BallScript : MonoBehaviour
}
void onTriggerEnter2D (Collider2D hitInfo) { Debug.Log(hitInfo.name); } private void OnCollisionEnter2D(Collision2D coll) {
var speed = lastVelocity.magnitude; var direction = Vector3.Reflect(lastVelocity.normalized, coll.contacts[0].normal); rb.velocity = direction Mathf.Max(speed);
} }
Comment
Your answer
Follow this Question
Related Questions
Shooting Gun Script 4 Answers
Reload only when not firing 1 Answer
Third person controller moving backwards when shooting 0 Answers
Finding target position for particle system based shooting 0 Answers
Car not shooting like bullet properly 2 Answers