- Home /
Recoil script help
Hey guys how you doing :) I was wondering if you could help me out well basically i have created a simple recoil script which makes the camera go up and making it look like recoil. Now the only problem is that it goes up when i press or hold the F key but it wont come back down how can i solve this here is my script :
var playerCamera : GameObject;
function Update()
{
if(Input.GetKey(KeyCode.F)){
Recoil();
}
}
function Recoil()
{
playerCamera.transform.localEulerAngles -= Vector3.right;
}
Thank you in advance MCHALO THANK YOU :)
Hey dude i have found a different way but its getting there. Basically at the moment it can go up and down but the problem is when i start the game i have already picked the set the original values of the position it starts of in. And if i shoot it will go up then the cooler kicks in and then it comes back down. Now if when it is going down how do i check if it has passed it original position on the X. lets say x was 1 and when its going down how do i check if its less then 1 so -1 if it set it back to 1. Now i have tried
if(mainCameraPos.transform.localPosition <1){
then do something but that gives me a new problem
here is my script
var mainCameraObject: GameObject;
var timerStart : float;
var Cooler : float ;
var Speed: float;
var OriginalPos : Vector3;
var inputTimer : float;
function Start (){
OriginalPos = transform.localPosition;
}
function Update()
{
if( timerStart > 0){
timerStart -= Time.deltaTime;
mainCameraObject.transform.localEulerAngles += OriginalPos ;
}
if( timerStart < 0){
timerStart = 0;
}
if(Input.GetKey(KeyCode.F)){
Recoil();
timerStart = Cooler;
}
}
function Recoil()
{
mainCameraObject.transform.localEulerAngles -= Vector3.right * Speed;
}
Answer by syclamoth · Dec 19, 2011 at 01:05 AM
Well, you're doing this in a too-simple way. As it is, it only has functionality for moving the camera back- none for moving forward again. Try putting this stuff into a Coroutine-
function Recoil()
{
recoiling = true;
var currentTime : float = 0;
var originalRotation : Quaternion = transform.localRotation;
while (currentTime < recoilTime)
{
transform.localRotation = Quaternion.Slerp(originalRotation, recoilRotation, currentTime / recoilTime);
currentTime += Time.deltaTime;
yield;
}
currentTime = 0;
while (currentTime < resetTime)
{
transform.localRotation = Quaternion.Slerp(recoilRotation, originalRotation, currentTime / resetTime);
currentTime += Time.deltaTime;
yield;
}
recoiling = false;
}
Where recoilTime and resetTime are floats which designate how long each stage should take, and recoilRotation is a Quaternion which tells it how far back to lean during the recoil action.
To activate it, use-
if(!recoiling && Input.GetKey(KeyCode.F))
{
Recoil();
}
It does not work :) none of the time take effect and when i press the F key nothing happens the camera does not shake :). I have declared the variable above needed :)
Have you set 'recoilRotation' to the correct value? If it is zero, it won't do anything. I would usually set this up by creating a child object of the main transform, rotating it to the correct value, and then use
recoilRotation = recoilSetter.localRotation;
inside of Start().
hey :) i tried it but its not what i am looking for this one makes the camera fly up then swings back down i need it to only go up in the Y axis like my old code and then return back to its original position if the timer is increased :)
Obviously I'm not understanding your question properly. You said
Now the only problem is that it goes up when i press or hold the F key but it wont come back down how can i solve this here is my script :
I've solved that specific problem- it comes back down again directly after rising. Was this not what you were after? Could you please explain your requirements a little more clearly?
Try tweaking the values- it's possible that you just haven't found a setup that suits you yet.
Answer by IrshadKhan_ · Mar 28, 2017 at 09:23 AM
Check This
var recoilMod : Transform;
var weapon : GameObject;
var maxRecoil_x : float = -20;
var recoilSpeed : float = 10;
var recoil : float = 0.0;
function Update()
{
if(Input.GetMouseButton(0))
{
//every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
recoil+= 0.25;
}else
{
recoil = 0;
}
recoiling();
}
function recoiling()
{
if(recoil > 0)
{
var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
recoil -= Time.deltaTime;
}
else
{
recoil = 0;
var minRecoil = Quaternion.Euler (0, 0, 0);
// Dampen towards the target rotation
recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
}
}
Your answer
Follow this Question
Related Questions
How do I make an enemy recoil when hit? 1 Answer
How to make a camera recoil system 0 Answers
Help with Recoil for guns 3 Answers
[Solved]Top-down shooter camera recoil effect 1 Answer
Vector3.Lerp not working unless interpolant t is 1. 2 Answers