i am newbie and beginner in unity so, i have a 3 script here :
shooting/attacking script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
private Animator anim;
public float cooldownTime = 2f;
private float nextFireTime = 0f;
public static int noOfClicks = 0;
float lastClickedTime = 0;
float maxComboDelay = 1;
private void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit1"))
{
anim.SetBool("hit1", false);
noOfClicks = 0;
}
if (Time.time - lastClickedTime > maxComboDelay)
{
noOfClicks = 0;
}
//cooldown time
if (Time.time > nextFireTime)
{
// Check for mouse input
if (Input.GetMouseButtonDown(0))
{
OnClick();
}
}
}
void OnClick()
{
lastClickedTime = Time.time;
noOfClicks ;
if (noOfClicks == 1)
{
anim.SetBool("hit1", true);
}
noOfClicks = Mathf.Clamp(noOfClicks, 0, 3);
if (noOfClicks >= 2 && anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.7f && anim.GetCurrentAnimatorStateInfo(0).IsName("hit1"))
{
anim.SetBool("hit1", false);
}
}
}
and about the bullet :
using UnityEngine;
using TMPro;
public class ProjectileGunTutorial : MonoBehaviour
{
//bullet
public GameObject bullet;
//bullet force
public float shootForce, upwardForce;
//Gun stats
public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
//Recoil
public Rigidbody playerRb;
public float recoilForce;
//bools
bool shooting, readyToShoot, reloading;
//Reference
public Camera fpsCam;
public Transform attackPoint;
//Graphics
public GameObject muzzleFlash;
public TextMeshProUGUI ammunitionDisplay;
//bug fixing :D
public bool allowInvoke = true;
private void Awake()
{
//make sure magazine is full
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
//Set ammo display, if it exists :D
if (ammunitionDisplay != null)
ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap " / " magazineSize / bulletsPerTap);
}
private void MyInput()
{
//Check if allowed to hold down button and take corresponding input
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
//Reloading
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Reload automatically when trying to shoot without ammo
if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();
//Shooting
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
//Set bullets shot to 0
bulletsShot = 0;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
//Find the exact hit position using a raycast
Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view
RaycastHit hit;
//check if ray hits something
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75); //Just a point far away from the player
//Calculate direction from attackPoint to targetPoint
Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
//Calculate spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate new direction with spread
Vector3 directionWithSpread = directionWithoutSpread new Vector3(x, y, 0); //Just add spread to last direction
//Instantiate bullet/projectile
GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity); //store instantiated bullet in currentBullet
//Rotate bullet to shoot direction
currentBullet.transform.forward = directionWithSpread.normalized;
//Add forces to bullet
currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
//Instantiate muzzle flash, if you have one
if (muzzleFlash != null)
Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot ;
//Invoke resetShot function (if not already invoked), with your timeBetweenShooting
if (allowInvoke)
{
Invoke("ResetShot", timeBetweenShooting);
allowInvoke = false;
//Add recoil to player (should only be called once)
playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
}
//if more than one bulletsPerTap make sure to repeat shoot function
if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
//Allow shooting and invoking again
readyToShoot = true;
allowInvoke = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime); //Invoke ReloadFinished function with your reloadTime as delay
}
private void ReloadFinished()
{
//Fill magazine
bulletsLeft = magazineSize;
reloading = false;
}
}
and the last is player movement script :
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float rotationSpeed;
public float jumpHeight;
[SerializeField]
private float gravityMultiplier;
[SerializeField]
private Transform cameraTransform;
private Animator animator;
private CharacterController characterController;
private float ySpeed;
private float originalStepOffset;
private bool isJumping;
private bool isGrounded;
void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
originalStepOffset = characterController.stepOffset;
}
void Update()
{
if(animator.GetCurrentAnimatorStateInfo(0).IsName("hit1"))
{
return;
}
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
float magnitude = Mathf.Clamp01(movementDirection.magnitude) * speed;
movementDirection = Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * movementDirection;
movementDirection.Normalize();
float gravity = Physics.gravity.y * gravityMultiplier;
ySpeed = gravity * Time.deltaTime;
if (characterController.isGrounded)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
animator.SetBool("IsGrounded", true);
isGrounded = true;
animator.SetBool("IsJumping", false);
isJumping = false;
animator.SetBool("IsFalling", false);
if (Input.GetButtonDown("Jump"))
{
ySpeed = Mathf.Sqrt(jumpHeight * -3 * gravity);
animator.SetBool("IsJumping", true);
isJumping = true; }
}
else
{
characterController.stepOffset = 0;
animator.SetBool("IsGrounded", false);
isGrounded = false;
if ((isJumping && ySpeed < 0) || ySpeed < -2)
{
animator.SetBool("IsFalling", true);
}
}
Vector3 velocity = movementDirection * magnitude;
velocity = AdjustVelocityToSlope(velocity);
velocity.y = ySpeed;
characterController.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{
animator.SetBool("IsMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
else
{
animator.SetBool("IsMoving", false);
}
}
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
{
var ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hitInfo, 0.2f))
{var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var adjustedVelocity = slopeRotation * velocity;
if (adjustedVelocity.y < 0)
{
return adjustedVelocity;
}
}
return velocity;
}
}
My character has been able to jump, run, shoot and idle, but when I click the button to shoot while jumping, the bullets come out during a jump animation. it's very strange and unwanted, so is there a way to delay the bullet and shoot when it lands?
CodePudding user response:
There is an implementation that might help: Time.timeScale = .1f; // just play at 10 times slower than normal speed;
float timeScaleDelta = .05f;
// Use this for initialization
void Start () {
...
}
// Update is called once per frame
void Update () {
...
float spX = Input.GetAxis("Horizontal");
float spY = Input.GetAxis("Vertical");
// spX, spY Handling up and down
if (Input.GetKeyDown(KeyCode.W)) // When W is pressed, the bullet time is accelerated
{
Time.timeScale = timeScaleDelta;
}
else if (Input.GetKeyDown(KeyCode.S)) // When pressing S, slow down the bullet time to play
{
if (Time.timeScale < 0) Time.timeScale = 0;
else Time.timeScale -= timeScaleDelta;
}
}
If you control the protagonist to jump and fly. Press the S key several times, you can see that the action slows down, and you can clearly see the slow motion.
CodePudding user response:
Instead of delaying the shoot I suggest just to block it while jumping. You have isJumping
field in PlayerMovement
script. You can make a public property for it and check it before shooting, having a SerializedField reference to moving script. So, if you detected a click, you first check if we are jumping now. If so, just return and do nothing.