Hello!
my projectile is sometimes moving slow, sometimes faster… Why?
The same problem has the firerate time, too.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ControlPlayer : MonoBehaviour
{
protected Joystick joystick;
protected JoyButton joybutton;
public GameObject bullet;
public GameObject weapon;
float bulletSpeed = Global.StandardGunBulletSpeed;
public float fireCoalDown = 0.1f;
float fireRate = Global.standardGunCoolDown;
float nextFire;
GameObject bulletClone;
// Test shooting via RayCast
public float damage = 20;
public float range = 100;
// Engine Particles
public GameObject engine1;
public GameObject engine2;
void Start()
{
joystick = FindObjectOfType<Joystick>();
joybutton = FindObjectOfType<JoyButton>();
nextFire = Time.time + fireRate;
}
// Update is called once per frame
void Update()
{
Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
var rigidbody = GetComponent<Rigidbody2D>();
rigidbody.velocity = new Vector2(joystick.Horizontal * Global.playerSpeed * Time.deltaTime,
joystick.Vertical * Global.playerSpeed * Time.deltaTime);
if (joystick.Vertical != 0 && joystick.Horizontal != 0)
{
Vector2 v = rigidbody.velocity;
float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
Quaternion endRotation = Quaternion.AngleAxis(angle, Vector3.forward);
Quaternion startRotation = transform.rotation;
transform.rotation = Quaternion.Lerp(startRotation, endRotation, 7f * Time.deltaTime);
if (!engine1.GetComponentInChildren<ParticleSystem>().isPlaying)
{
engine1.GetComponentInChildren<ParticleSystem>().Play();
}
if (!engine2.GetComponentInChildren<ParticleSystem>().isPlaying)
{
engine2.GetComponentInChildren<ParticleSystem>().Play();
}
}
if (joybutton.pressed && Time.time >= nextFire)
{
nextFire = Time.time + fireRate;
fire();
}
}
public void fire() {
RaycastHit2D hit = Physics2D.Raycast(weapon.transform.position, transform.right, 5);
if (hit.collider != null && hit.collider.name != "Background") {
if (Vector2.Distance (hit.point, weapon.transform.position) <= Global.StandardGunRange) {
bulletClone = bullet;
Vector3 position = (weapon.transform.position);
bulletClone = (GameObject)Instantiate (bulletClone, position, weapon.transform.rotation);
bulletClone.GetComponent<Rigidbody2D> ().AddForce (weapon.transform.right * bulletSpeed * Time.deltaTime, ForceMode2D.Impulse);
}
}
}
}