i want to let a prefab which is placed in the scene in unity rotate continously at 360…
- User
i want to let a prefab which is placed in the scene in unity rotate continously at 360 degrees and it should slightly swivel.It should be for a weapon which the player can pick up when he walks through that. Code the script for the rotating and swivel part first.
- Assistant
Certainly! Below is a simple Unity C# script that rotates a prefab continuously at 360 degrees on its Y-axis and applies a slight swaying (swiveling) effect on its X-axis. You can attach this script to the prefab placed in your scene.
- User
it should rotate around its x axis and swivel equally from the center of the object. The effect should be like the Weapon pickup from quake 3 arena
- Assistant
- User
i meant on its y axis
- Assistant
Gotcha! You want the weapon to continuously rotate around its X-axis (spinning forward), and simultaneously swivel back and forth around its Y-axis (side to side tilt from center), just like Quake 3's weapon pickup effect.
Here's the updated script implementing that:
using UnityEngine; public class WeaponRotateAndSwivel : MonoBehaviour { [Header("Rotation Settings")] // Rotation speed in degrees per second around X-axis public float rotationSpeed = 180f; [Header("Swivel Settings")] // Max swivel angle in degrees around Y-axis (left-right tilt) public float swivelAngle = 15f; // Swivel oscillation speed in cycles per second public float swivelSpeed = 1.5f; void Update() { // Continuous rotation around X-axis transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime, Space.Self); // Calculate the swivel angle oscillation using sine wave over time float swivelOffset = Mathf.Sin(Time.time * swivelSpeed * Mathf.PI * 2) * swivelAngle; // Calculate swivel rotation on Y axis Quaternion swivelRotation = Quaternion.Euler(0f, swivelOffset, 0f); // Apply swivel rotation *after* base rotation // To combine properly we store current rotation around X in Quaternion: Quaternion rotationX = Quaternion.Euler((Time.time * rotationSpeed) % 360f, 0f, 0f); // Combine X rotation and Y swivel transform.localRotation = rotationX * swivelRotation; } } - User
its rotating wrong. The weapon should never face down. currently its rotating in 360 degrees but imagine like the front of the gun is facing to the sky and than to the ground, but I want to rotate it continously on the same height and the gun should face 360 degrees around the terrain and swivel slightly.
- Assistant
- User
now the front of the gun is just pointing in one direction while its slightly swinging up and down, instead it should rotate so that the front of the guns is rotating on its axis around the terrain
- Assistant