- Home /
Other
How do I get by bullet clone to move forward when mouse is clicked?
(I'm still very new to Unity) I've tried making it so the bullet would move forward when the mouse is clicked. But when I test it, the bullet clones spawn and don't move. The bullets properly go to the player and appear like I want it to but it doesn't move forward like I want it to. The code I used originally came from a Brackeys video. It was for 2D top down shooter tutorial. I did every step correctly but only from the shooting part but it didn't work. The function of his game was different from mine because his character would follow the mouse and my character just faces forward. But when I tested I originally tried the "firePoint.up" part into the script, like in the tutorial, and that made the bullet go up instead of forward. So I thought instead I could change it to "firePoint.forward". But when I tested it, the bullet just appeared and did nothing. I have all the code used if it is needed to help me out but I want to know what to do to fix this. I just want it so when the mouse is clicked, the bullet moves forward and deletes when it touches an object.
Answer by deckerjoshua239 · Mar 02, 2021 at 04:16 PM
Make sure to assign everything to where it needs to be and you can delete the particle stuff if you do not know how to make a particle. I did not make this script this guy did. Brackeys:https://www.youtube.com/brackeys
using UnityEngine;
public class Guns: MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot ()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
}
Answer by $$anonymous$$ · Mar 02, 2021 at 05:33 PM
Check if your character's facing forward sprite matches in the forward direction of the transform, and not the right / left.
If not, change 'firePoint.forward' to 'firePoint.right' if facing right, or throw a minus in front of it and use '-firePoint.right' for the left.
Follow this Question
Related Questions
how to loop an object in a 2D game 1 Answer
How to move an object to a certain direction using arrow keys? 1 Answer
How do I add camera rotation while moving a player sideways? 2 Answers
How can make the player move constantly when the key A or D is held down? 1 Answer
Player walks in the direction the vr camera is looking? 2 Answers