- Home /
transform.rotate
I'm working on recoil for weapons and i decided to use transform.rotate and it works well kinda... i need it to not bounce back to its original position and rather just rotate -5 and then with the next shot rotate another -5. but it bounces back. this is most likely due to me not know the language very well. please help if you can.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class recoil : MonoBehaviour {
public float fireRate = .16f;
public float ammo = 20f;
private float nextFire;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
ammo -= 1;
transform.Rotate(-5, Time.deltaTime, 0, Space.Self);
}
if(ammo == 0)
{
ammo = 20;
}
if(ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
{
ammo = 20;
}
}
}
Answer by unit_nick · Sep 07, 2017 at 09:42 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
public float fireRate = 0.16f;
public float ammo = 20f;
// total recoil influence of a fire action
public float recoilDegrees = 5f;
// time it will take for gun to reach full recoil
public float recoilTime = 0.16f;
// recoil variables
private float prevFire;
private float prevRecoil;
private float totalRecoil; // a movie by Arnold Schwarzenegger :D
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
// Use this for initialization
void Start()
{
prevFire = prevRecoil = 0;
totalRecoil = float.MaxValue;
}
// Update is called once per frame
void Update()
{
// get the burrent time
float time = Time.time;
// fire if enough time has passed sine last shot
if (Input.GetKey(KeyCode.Mouse0) && time > prevFire + fireRate)
{
prevFire = time;
prevRecoil = totalRecoil = 0;
ammo -= 1;
}
// if still recoiling then apply recoil
if (totalRecoil < recoilDegrees) {
// get the total recoil for this time
totalRecoil = recoilDegrees * Mathf.Sin(90f * (1f / recoilTime) * Mathf.Min(time - prevFire, recoilTime) * Mathf.Deg2Rad);
// apply the total recoil less the previous recoil. make it neg to go up
transform.Rotate((totalRecoil - prevRecoil) * -1f, 0, 0, Space.Self);
// save last recoil for next loop
prevRecoil = totalRecoil;
}
if (ammo == 0)
{
ammo = 20;
}
if (ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
{
ammo = 20;
}
}
}
Answer by tormentoarmagedoom · Sep 07, 2017 at 08:10 AM
Hello @Joeman9832 ,
As Ju_M says, is 100% recomended to use animation. Is more easy than scripting, and you can see the results as you make it. Look some videos to learn if dont know how to do it, but in less than 15 minutes you will know all you need for do it!
:D
i may have been hard to understand what i want is to have the camera rotate and shake like the gun is pushing you up. i have animations on the gun. ill include an image of what im wanting.
Answer by Joeman9832 · Sep 07, 2017 at 10:27 PM
Thank you so much unit nick
unit nick i still have the problem of it bouncing back to the original spot im not sure why
I would need to see more code. Nothing else in the code provided moves the object.
Also I made the recoil decelerate by gradually slowing because that was my first idea. However thinking about it I might make it linear.
Although reading your comment with diagram above this may have been a lucky coincidence. If it is the camera you are rotating then not having it come to a sudden stop might be appropriate?
yah i would like to smooth out the motion and also have it continue its path upwards if the player continues to shoot but im not sure how i would do that
Your answer
Follow this Question
Related Questions
Clamping X rotation on camera. 1 Answer
Adding quaternions using a seperate axis 0 Answers
transform.Rotate slows down my moving cube 0 Answers
Issues with copying Y axisrotation of another object 2 Answers