- Home /
How to make a tps gun [C#]
So, I have the basic look around and shoot function, but it only shoots straight. I want it to be able to shoot up and down too, but I don't know how to do that. Could someone help me out?
Shoot Script:
using UnityEngine; using System.Collections;
public class Shoot : MonoBehaviour { [Range (0, 50)] public int Change_Despawn_Bullet; [Range(0, 50)] public int Bullet_Forward_Force; [Range(0, 50)] public int WaitTimeRelaod; [Range(0, 100)] public int ClipSize_Number = 12; [Range(0, 100)] public int AmmoSize_Number = 36;
public Transform playerTransform;
public float offset = 90;
private GameObject tempGameObject;
public GameObject Bullet;
public GameObject Bullet_Spawner;
private Damage_Heal ammo_clip;
private void Start()
{
ammo_clip = GetComponent<Damage_Heal>();
tempGameObject = new GameObject();
ammo_clip.UpdateAmmo_ClipBar();
ammo_clip.AmmoSize = AmmoSize_Number;
ammo_clip.ClipSize = ClipSize_Number;
if (playerTransform == null)
{
playerTransform = GameObject.FindWithTag("Player").transform;
}
}
public void Update()
{
ammo_clip.UpdateAmmo_ClipBar();
tempGameObject.transform.rotation = transform.rotation;
if (Input.GetKeyDown("r"))
{
StartCoroutine("Reload_Animation");
}
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ((Physics.Raycast(ray, out hit)) &&
(Bullet_Spawner != null) &&
(Bullet != null) &&
(ammo_clip != null) &&
(ammo_clip.ClipSize > 0) &&
(Input.GetButtonDown("Fire1")))
{
Gun_Controll(hit);
}
}
IEnumerator Reload_Animation()
{
while ((ammo_clip != null) && ammo_clip.ClipSize >= 0 && ammo_clip.ClipSize < 12 && ammo_clip.AmmoSize > 0)
{
yield return new WaitForSeconds(WaitTimeRelaod);
ammo_clip.AmmoSize -= 1;
ammo_clip.ClipSize += 1;
ammo_clip.UpdateAmmo_ClipBar();
}
}
public void Gun_Controll(RaycastHit hit)
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Spawner.transform.position, Bullet_Spawner.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(tempGameObject.transform.forward * Bullet_Forward_Force * 100);
Destroy(Temporary_Bullet_Handler, Change_Despawn_Bullet);
ammo_clip.ClipSize -= 1;
ammo_clip.UpdateAmmo_ClipBar();
}
}
Hey, Its my 1st post, so dont be rude, please :) If u are creating ftp game, u can create camera as child of player and then 3D model of your gun should be child of camera. Then when you are looking around with screen, gun is moving too. You dont need any coding to do it :)
That is helpful if I was doing a fps, but I am doing tps game.... wait that might work still. Give me a sec.
And cool that this is your first comment. ^^
Answer by UrielKane · Dec 26, 2016 at 06:39 AM
No one is responding becouse you give not enough information on what actually you want to achive. For example if your tps is a top down shooter with a static rotation camera like diablo games or the old unity shooter demo. Or if it's something completely diferent more like a RE4 shooter or what ever. So you need to explain exactly what is your goal and what is exactly your problem.
I can tell for what you have say, two things.
1) if you have a camera that is not rotating up and dawn and you are using the camera for orientation, that exactly your problem.
2) Or maybe you have a nice full rotatable camera, but you are dont taking properly the direction for the bullet.
Since your code is not commented it's almost impossible for me to understand exactly whats going on. But any whay, i can tell for the fact that you are using only one raycast, that you are not taking very well the direction for the bullet.
I'm still did not try to do a tps and maybe i'm totaly wrong, so take me at your risk in that sense. Any way, some month ago i dev a projectile "system" for my personal use, since i found nothing about shooting projectiles properly with directions. Taking in account the camera dir, the spawn pos and the hit position. At the end i finish the system making various and tedious raycast and linecast checks to get it working properly on what i want.
In fact i dont use this only for physic projectiles, i use it for give direction to the particle tracers aswell. So if you want maximum accuracy bettwen camera crosshair or mouse crosshair and bullet spawn position. You will need to do at least two raycast or linecast checks, one for camera or mouse and other for the bullet. Unless you dont need or want to have tracers effects and all that fancy stuff.
Hope to usefull, and not a pain in the ass for anyone.
Follow this Question
Related Questions
not losing ammo when shooting 1 Answer
Air Strike shoot aiming at cross hairs in middle of the screen 2 Answers
Shooting with Limited Ammo 2 Answers
Turret shooting at me 1 Answer