- Home /
Problem with a charged projectile direction
I've been trying to create a topdown shooter were the weapon and the projectiles that should work like this. If you hold left mousebutton, start charging your shot to increase its speed and size. When you release the button it should fire the projectile on the cursor direction. I also want to be able to see my projectile while its charging and it should follow the player untill its shot. This is the code I made and its working fine with one exception. If I start charging my shot then i rotate my player to any direction and release the shot, my projectile will lauch on that direction.
Gun Controller
public PlayerProjectile projectile;
private PlayerProjectile newProjectile;
public bool isCharging;
public Transform firePoint;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
isCharging = true;
newProjectile = Instantiate(projectile, firePoint.position, firePoint.rotation);
newProjectile.isCharging = true;
}
if (Input.GetMouseButtonUp(0))
{
isCharging = false;
newProjectile.isCharging = false;
}
}
}
PlayerProjectile
private Transform firePoint;
private Rigidbody rb;
public float chargeSpeed = 10;
public float chargeLimit = 30;
public float newCharge;
public float newScale;
private float chargeLevel = 0f;
public bool isCharging;
void Start()
{
firePoint = GameObject.FindWithTag("firePoint").transform;
rb = gameObject.GetComponent<Rigidbody>();
rb.velocity = firePoint.transform.forward * 20;
}
void Update()
{
if (isCharging == true && newCharge <= chargeLimit)
{
transform.position = firePoint.position;
chargeLevel += Time.deltaTime * chargeSpeed;
chargeLevel = (chargeLevel > chargeLimit ? chargeLimit : chargeLevel + Time.deltaTime * chargeSpeed);
newCharge = chargeLevel;
newScale = chargeLevel * 0.1f;
transform.localScale = new Vector3(newScale, newScale, newScale);
// print(newScale);
}
if (newCharge >= chargeLimit)
{
newCharge = chargeLimit;
}
if (isCharging == false)
{
print(newCharge);
Shoot();
chargeLevel = 0f;
}
}
void Shoot()
{
// rb.velocity = transform.forward * 20;
}
Answer by xxmariofer · Mar 04, 2019 at 12:52 PM
save the rotation of the player whne you startted pressing the shoot button and use that in the shoot method
It works. Thank you so much for your help. I appreciate alot. :)
you are welcome, i have changed it to an answer so you can mark it as correct :)
Your answer
Follow this Question
Related Questions
Coding A Megaman-like Charge Shot? 1 Answer
,How do I make projectile weapons stick to enemies or targets? 2 Answers
Text popup on target hit 2 Answers
Shooting in 2 dimensional games 2 Answers
Gun Fire, sparks on Collision 1 Answer