- Home /
Camera Recoil,Please help me.
Hi,i want camera recoil (separate script) which you add to camera and when you press shoot button (left mouse button) camera shake off and gets back in position,please guys help me.
Answer by Griffo · Sep 18, 2012 at 02:30 PM
This will do it.
#pragma strict
var recoilSpeed : float = 0.01; // Speed to move camera
function Start () {
}
function Update () {
if (Input.GetKeyDown ("left ctrl")){
recoilBack();
}
if (Input.GetKeyUp ("left ctrl")){
recoilForward();
}
}
// Move current weapon to zoomed in position smoothly over time
function MoveToPosition(newPosition : Vector3, time : float){
var elapsedTime : float = 0;
var startingPos = transform.position;
while (elapsedTime < time){
transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}
function recoilBack(){
// Start coroutine to move the camera up smoothly over time
var zoomOutOffset = Vector3(0, 0, 0.5);
var zoomOutWorldPosition = transform.TransformDirection( zoomOutOffset );
// Move the camera smoothly
StartCoroutine(MoveToPosition(transform.position + zoomOutWorldPosition, recoilSpeed));
}
function recoilForward(){
// Start coroutine to move the camera down smoothly over time
var zoomInOffset = Vector3(0, 0, -0.5);
var zoomInWorldPosition = transform.TransformDirection( zoomInOffset );
// Move the camera smoothly
StartCoroutine(MoveToPosition(transform.position + zoomInWorldPosition, recoilSpeed));
}
Wow, this is very generous of you Griffo. Of course it is great, and future users will gain from absorbing what this script does. I have of course (and if people who recognize my handle will know) supplied scripts to the 'beginner', but with this person I did not feel that they were invested in expanding their knowledge, this being an example : http://answers.unity3d.com/questions/281777/player-destroys-when-he-touches-particle-effect.html
Another indicator is the OPs karma rating. But I stand with my original comment, well done Griffo, I am upvoting this for your generosity (Fattie gets one too) =]
I hope other readers find this useful, and Claymore you should be very thankful.
Hi alucardj,
I totally agree with your comment, maybe I was a bit to quick to answer and never really looked at the users profile, as I usually do, I remember being new here and getting help so like to help if I can as I'm still very new to scripting and I'm sure you have helped me in the past.
I know the community like to see people trying before giving help and I stand by that, I find it had work writing scrips (but enjoyable) and don't like to give them away for no reason and I'm sure others feel the same.
On the other hand its nice to be helped out and we all benefit in the end.
Cheers.
EDIT .. I didn't read the whole page and have just seen you post below, sorry I will be more careful in future.
Fattie,
I know but I thought he could work that bit out for himself .. and It shouldn't take much to workout how to change it to recoil ..
True, it is for the greater good, for me the username stood out therefore the question gained 'personality'. I started Unity in January, and I would have struggled alot without having this 'site for a resource.
$$anonymous$$y final advice to Claymore :
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
also : http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html
ps i thought the same regarding the recoil factor, if the OP was sincere then maybe it would be worth expanding on, any future readers can post a comment to find out.
Answer by Rahkola · Sep 18, 2012 at 10:43 AM
You could tweak your camera position and rotation with iTween to achieve cool recoil effect.
This is not the place to ask for scripts. Unity Answers and its community is here to help you solve any Unity-related problems you have, not give away free code. If you don't know how to write code, I suggest learning Unity's Java Script (its really easy), but if you don't have time, or whatever, then you can try asking your question on the Unity Forums. I hope you can get an answer, although both these answers have answered the question already.
Also, use google. Do tutorials, and learn how to use Unity.
Just a point-of-fact, there is no camera recoil script that comes with Unity.
Your answer
Follow this Question
Related Questions
Need help with bullet/gun scripting 3 Answers
I need help with my script 3 Answers
Recoil with the FPS controller 1 Answer
How to run an animation clip on button down-hold? 1 Answer
gun bob and jerk animation 2 Answers