- Home /
How can I add a rotation to my bullets when they are shot?
I haven't used rotation in a while so I can't remember how to do this. I am trying to make a random trajectory for my bullets and I want to add a random rotation but I don't know how to add the rotation. I commented in the script where the rotation should be added.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject bulletPrefab;
public Camera playerCamera;
private Coroutine hasCourutineRunYet = null;
public int ammo = 300;
public Text Ammmo;
private float xPos;
private float zPos;
private float yPos;
private float rot;
// Update is called once per frame
void Update()
{
Ammmo.text = "Ammo: " + ammo.ToString();
if(Input.GetMouseButtonDown(0))
{
if (hasCourutineRunYet == null)
hasCourutineRunYet = StartCoroutine(BulletShot());
}
}
IEnumerator BulletShot()
{
xPos = Random.Range(-0.3f, 0.3f);
yPos = Random.Range(-0.3f, 0.3f);
zPos = Random.Range(-0.3f, 0.3f);
rot = Random.Range(-10.0f, 10.0f);
GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward + new Vector3(xPos, yPos, zPos) ; // add rotation here
bulletObject.transform.forward = playerCamera.transform.forward;
yield return new WaitForSeconds(1.0f);
hasCourutineRunYet = null;
ammo--;
}
}
Comment
Answer by SoulBlaze06 · Jun 10, 2021 at 04:31 PM
This might help you...i have added #modified for the things i changed
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject bulletPrefab;
[Range(0f, 1000f)] [SerializeField] private float Bullet_rotation_speed = 10f; // #Modified
// It is your choice if you want to use Range. It just adds a slider for you to control in inspector
public Camera playerCamera;
private Coroutine hasCourutineRunYet = null;
public int ammo = 300;
public Text Ammmo;
private float xPos;
private float zPos;
private float yPos;
private float rot;
// Update is called once per frame
void Update()
{
Ammmo.text = "Ammo: " + ammo.ToString();
if(Input.GetMouseButtonDown(0))
{
if (hasCourutineRunYet == null)
hasCourutineRunYet = StartCoroutine(BulletShot());
}
}
IEnumerator BulletShot()
{
xPos = Random.Range(-0.3f, 0.3f);
yPos = Random.Range(-0.3f, 0.3f);
zPos = Random.Range(-0.3f, 0.3f);
rot = Random.Range(-10.0f, 10.0f);
GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward + new Vector3(xPos, yPos, zPos) ; // add rotation here
// #Modified ... This can be used to rotate your prefab... Just put the x,y,z values in the vector3
bulletPrefab.transform.Rotate(new Vector3(5f * Bullet_rotation_speed * Time.deltaTime, 0f, 0f));
bulletObject.transform.forward = playerCamera.transform.forward;
yield return new WaitForSeconds(1.0f);
hasCourutineRunYet = null;
ammo--;
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How do i get the Raycast rotation? 1 Answer
Distribute terrain in zones 3 Answers
Collision problem 4 Answers
How to change the player's forward direction and make turning more smooth 0 Answers