So what I am trying to achieve is have the player shoot toward the mouse positiion and I have that working but I want the player to have to charge their shot. So if the player just presses the button it only goes a certain distance but if the player holds the button down for a bit it goes farther.
I tried to make a video that kinda explains what I am going for.
This is the code that controls the shooting
public class BowController : MonoBehaviour {
public GameObject arrow;
public Transform shootLoc;
public float force;
public Camera cam;
public float chargeMin;
public float chargeAmount;
public float chargeMax;
public bool charging;
void Start () {
}
void Update () {
Ray cameraRay = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 pointToLook;
if (Physics.Raycast(cameraRay, out hit, 50))
{
pointToLook = hit.point;
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
}
else
{
pointToLook = cameraRay.origin + (cameraRay.direction * 50);
}
transform.LookAt(pointToLook);
if(Input.GetMouseButtonDown(0))
{
charging = true;
}
if (charging)
{
Charging();
}
if (Input.GetMouseButtonUp(0))
{
GameObject tempArrow;
tempArrow = Instantiate(arrow, shootLoc.position, shootLoc.rotation) as GameObject;
tempArrow.transform.LookAt(pointToLook);
Rigidbody tempBody;
tempBody = tempArrow.GetComponent<Rigidbody>();
tempBody.AddForce(transform.forward * force, ForceMode.Impulse);
force = 100;
charging = false;
}
}
void Charging()
{
force += Time.deltaTime * chargeAmount;
if (force >= 500)
{
force = 500;
}
}
}
And this is the code that is on the arrow
public Rigidbody rBody;
public float speed;
void Awake () {
flying = true;
}
void Update () {
if (flying)
{
//rBody.velocity = rBody.velocity.normalized * speed;
transform.LookAt(transform.position - GetComponent<Rigidbody>().velocity);
transform.Rotate(0, 180, 0);
}
}
void FixedUpdate()
{
/*
if (flying)
{
transform.rotation = Quaternion.LookRotation(rBody.velocity);
}
*/
}
void OnCollisionEnter(Collision collision)
{
flying = false;
}
}
I am still pretty new to coding so any help is appreciated. Thank you all very much in advance for the help!