3D game
I need to improve the code for firing in Unity in C# so that the weapon shoots not only along one trajectory, since the bullet flies along z and does not change the trajectory when turning.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotToClick : MonoBehaviour
{
public GameObject Bullet;
public float Power;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject b = Instantiate(Bullet, transform.position, transform.rotation);
b.GetComponent<Rigidbody>().AddForce(Vector3.forward * Power, ForceMode.Impulse);
}
}
}
I am a beginner and I try to learn by creating projects, I hope for your help
CodePudding user response:
maybe like this. use the direction from the transform
GameObject b = Instantiate(Bullet, transform.position, transform.rotation);
b.GetComponent<Rigidbody>().AddForce(transform.TransformDirection (Vector3.forward) * Power, ForceMode.Impulse);