Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 27, 2016 at 02:09 AM by NovaDrox.
avatar image
0
Question by NovaDrox · Dec 25, 2016 at 09:36 PM · shootingshootgun scripttps

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();

 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arthas666 · Dec 25, 2016 at 10:53 PM 0
Share

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 :)

avatar image NovaDrox arthas666 · Dec 26, 2016 at 12:48 AM 0
Share

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. ^^

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Problem with firing laser in direction of camera 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges