- Home /
The question is answered, right answer was accepted
How do I fix my gun animation
I am very new to unity and game development, and my weapon pick up system has a problem with my gun shooting animation. Everytime i drop the gun meaning it un-parents it self from the camera, the gun would teleport the to the animation root position and float there. And when I apply "apply root motion", whenever i shoot the gun once, it would rotate itself on the x rotation.
This is my pickup script.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PickUpController : MonoBehaviour { public RayGun gunScript; public Rigidbody rb; public BoxCollider coll; public Transform player, gunContainer, fpsCam; public Animator Animation; public float pickUpRange; public float dropForwardForce, dropUpwardForce; public bool equipped; public static bool slotFull; private void Start() { //Setup if (!equipped) { gunScript.enabled = false; rb.isKinematic = false; coll.isTrigger = false; } if (equipped) { gunScript.enabled = true; rb.isKinematic = true; coll.isTrigger = true; slotFull = true; } } private void Update() { //Check if player is in range and "E" is pressed Vector3 distanceToPlayer = player.position - transform.position; if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) PickUp(); //Drop if equipped and "Q" is pressed if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop(); } private void PickUp() { equipped = true; slotFull = true; //Make weapon a child of the camera and move it to default position transform.SetParent(gunContainer); transform.localPosition = Vector3.zero; transform.localRotation = Quaternion.Euler(Vector3.zero); transform.localScale = Vector3.one; //Make Rigidbody kinematic and BoxCollider a trigger rb.isKinematic = true; coll.isTrigger = true; //Enable script gunScript.enabled = true; } private void Drop() { equipped = false; slotFull = false; //Set parent to null transform.SetParent(null); //Make Rigidbody not kinematic and BoxCollider normal rb.isKinematic = false; coll.isTrigger = false; //Gun carries momentum of player rb.velocity = player.GetComponent<Rigidbody>().velocity; //AddForce rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse); rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse); //Add random rotation float random = Random.Range(-1f, 1f); rb.AddTorque(new Vector3(random, random, random) * 10); //Disable script gunScript.enabled = false; } }
Follow this Question
Related Questions
Player Animation change when pickup Weapon 0 Answers
weapon animation question 1 Answer
destroy GameObject without removing reference in list 1 Answer
Hot to use walking animation while aiming! 0 Answers
Multiple Weapon Animation Help 0 Answers