I have a problem with making a first person shooter
Ok so this is my first question I've posted. I'm trying to do a quick build of a first person shooter to get a functioning product done as I haven't really finished anything. So I'm not focussing too much on quality as that's what gets me caught up in production.
Anyway my problem:
So it's sort of two part. My first problem, which won't exactly be a problem later, but it could be: I made guns out of cubes to start coding, and so I didn't have to sit and code a holding block I just made it a child of my camera, and positioned it at the hold position. This is working fine but for some reason when I look up and down the gun warps weirdly. I'll try to attach some screenshots.
The next problem is I've written a block of code for switching guns. It works perfect for certain guns, but once I get to the first gun I've made it runs the DropGun() function on the gun I'm trying to pick up and keeps the one I already have. Also when I continuously try to pick up the gun, it warps them also. I've also found it's when I have my camera faced down where the gun I'm holding is warped. Any help would be greatly appreciated. I'm not a beginner to coding or using unity (however I wouldn't say I'm a strong coder), but this is a problem I can't figure out. This will help me learn a bunch.
here are the picking up and dropping functions Let me know if you need to see any other code:
void Pickup() { if (pControl.currentGun != null) { Firearm arm; arm = pControl.currentGun.GetComponent(); arm.DropGun(); }
Debug.Log("I'm getting picked up");
gameObject.transform.parent = cam;
body.isKinematic = true;
body.useGravity = false;
collider.enabled = false;
transform.localPosition = holdPosition;
transform.localRotation = Quaternion.identity;
isHeld = true;
pControl.currentGun = gameObject;
}
public void DropGun()
{
gameObject.transform.parent = null;
body.isKinematic = false;
body.useGravity = true;
collider.enabled = true;
body.AddForce(Player.transform.forward * throwPower);
isHeld = false;
}
Answer by OrganizedChaos · Jan 15, 2016 at 08:52 PM
Just an Update: I figured out the problem with the warping guns. I had had the camera as a child of my capsule, and I scaled it up (I'm using each unit as 1meter) which also scaled the camera up in the y axis. This caused some unwanted effects. So incase anyone else has experienced the same problem.
Still working on my weapon switch script. I'm sure I'll figure it out eventually.
2021 user here and I was suffering too. Your answer game me an idea. Don't change transforms of parent of the camera. But change camera's position and collider etc etc
Answer by ParadoxSolutions · Jan 09, 2016 at 02:37 AM
void Pickup() { if (pControl.currentGun != null) { Firearm arm; arm = pControl.currentGun.GetComponent(); arm.DropGun(); }
I think when you run Pickup() you run DropGun() at the same time so the gun you try to pick up immediately gets dropped, I can't be sure unless I know what the script is attached to. I am not sure what pControl is but you could try setting pControl.currentGun = null; then set pControl.currentGun equal to the weapon you are trying to pick up.
Make sure the two guns have different names other than both being called gameObject I say this because I don't see gameObject being set to the guns (assuming that's a gun).
I hope I am making sense I have not answered many questions, try running DropGun() first then PickupGun() to make sure you are setting the gameObjects parent to null.
Just a few things to try, Hope this helped.
Thanks for the answer!
I'll review my code with your suggestions. The code I posted is from my parent firearm script. The children of this script are attached to the respective guns. I initially created a pistol out of cubes, and then to test the weapon switching, I created a shotgun and an assault rifle. What I was going for in the code you posted was to access the script of the weapon currently being held, then run that object's drop function. The code I have now allows me to pick up the pistol, switch to the shotgun, and then to the assault rifle. The pControl is a reference to the Player script attached to the player.
Also you wouldn't happen to know why the child objects of the main camera are warping when it's rotated up and down when looking? I plan to have the weapons coded to be held without being a child of the camera, but should I need to later. This isn't the first time this has been a problem.
Thanks again!
I have never had the problem with objects warping when a child of a camera, I would really need a picture to understand this; is the cube gun stretching? Do you have any Unity Pro image affects applied? $$anonymous$$ake sure you aren't using fisheye.
If you are doing this as a hobby or just to have a base understanding of FPS mechanics, I have seen an asset on the asset store that was using cube guns with various settings for projectiles/fx ect I can't remember the name though, if you want I can try and find a link for you.
If you are making a commercial game though I would highly recommend you invest into this package, it will save you thousands of hours in development time: https://www.assetstore.unity3d.com/en/#!/content/2943
I attached two screenshots. First one is it being warped or stretched, and the second one is neutral, how it's supposed to look. I only have Unity 5 standard, so I don't have any pro features. How do I know if fisheye is on? I also just figured out how to use rendering effects, so I can now add another layer of polish to my productions. So thank you. I played around with the ones in standard assets for about 20 $$anonymous$$s.
That looks like a cool plugin. I'm just a hobbyist. I'm kinda just having fun doing this, and I'm creating all my content myself so I don't have to spend any money. The price of that plug in is a little steep, but I do have FPS control which is very similar. But it seems to not work or something.
Your answer
Follow this Question
Related Questions
Recoil system 0 Answers
Networking Fps Cameras 0 Answers
Problem of stuttering FPS Camera 1 Answer
Partial synchronization of camera with the bone of the head or other object 0 Answers
C# Camera based Recoil 0 Answers