- Home /
How to make gun sway script affect camera
So, I am trying to add my gun sway effect to my camera that draws my guns over everything else instead of adding the script to every gun, because if I do that my bullets just fly through the walls and that is a problem in the next phase of my game; targets. So, I am really frustrated with this, so please, if anyone knows what I should do, please post your answer down below.
Answer by meat5000 · Oct 20, 2013 at 12:00 PM
Here are some links for you.
http://docs.unity3d.com/Documentation/Components/Layers.html
http://answers.unity3d.com/questions/17841/always-render-a-object-on-top-of-another.html
I know all that, it's just that my gun script attached to the camera doesn't work for some weird reason. Anyways, thanks.
Post your script and perhaps say what does and doesn't work with it :)
And are you after Gun sway or View sway/bob?
var amount : float = 0.02;
var maxAmount : float = 0.03;
var Smooth : float = 3;
var SmoothRotation = 2;
var tiltAngle = 25;
private var def : Vector3;
function Start ()
{
def = transform.localPosition;
}
function Update ()
{
var factorX : float = -Input.GetAxis("$$anonymous$$ouse X") * amount;
var factorY : float = -Input.GetAxis("$$anonymous$$ouse Y") * amount;
if (factorX > maxAmount)
factorX = maxAmount;
if (factorX < -maxAmount)
factorX = -maxAmount;
if (factorY > maxAmount)
factorY = maxAmount;
if (factorY < -maxAmount)
factorY = -maxAmount;
var Final : Vector3 = new Vector3(def.x+factorX, def.y+factorY, def.z);
transform.localPosition = Vector3.Lerp(transform.localPosition, Final, Time.deltaTime * Smooth);
var tiltAroundZ = Input.GetAxis("$$anonymous$$ouse X") * tiltAngle;
var tiltAroundX = Input.GetAxis("$$anonymous$$ouse Y") * tiltAngle;
var target = Quaternion.Euler (tiltAroundX, 0, tiltAroundZ);
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,Time.deltaTime * SmoothRotation);
}
I don't think you should use def as the name of your Vector3. It's a $$anonymous$$eyword
Yeah, I've changed it but it doesn't change a thing, but this script still works if I attach it to each individual gun. Here's my camera setup:
Your answer
![](https://koobas.hobune.stream/wayback/20220613120259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need help with my gun movement script 1 Answer
Problem with camera 1 Answer
Jumpscare script help!! 1 Answer
Troubles With A Shoot Script 1 Answer
Problem doing a script. 2 Answers