Lbf666 Posted July 8 Share Posted July 8 this func is in unity script It can make an object move around another object in a uniform circle but I don't know Its principle Can you help me realize it thanks!!!! Link to comment Share on other sites More sharing options...
LeeC22 Posted July 8 Share Posted July 8 (edited) 57 minutes ago, Lbf666 said: this func is in unity script It can make an object move around another object in a uniform circle but I don't know Its principle Can you help me realize it thanks!!!! In its most simple form, it is a case of using Sine and Cosine to generate a circle with a specified Radius and to then use the object you are rotating around as the origin. I could try and explain all the maths but I would probably make it sound too complicated, so here's a small script to demonstrate it instead. Create a file called RotateAround.cs, copy this code into it and then place it in your scripts folder. This is created with ScriptHookVDotNet v2, so if you are using v3 you might have to change the filename to ensure it runs correctly... not too sure on that though. using System; using System.Drawing; using GTA; using GTA.Math; namespace RotateAround { public class cRotateAround : Script { private float RotationAngle = 0f; private float RotationSpeed = 20f; private float RotationRadius = 3f; const float DegreesToRadians = 0.01745f; public cRotateAround() { Tick += onTick; Interval = 0; } private void onTick(object sender, EventArgs e) { // Exits from the loop if the game is loading if (Game.IsLoading) return; // Get the last frame time as deltaTime // deltaTime is a fraction of a second, 1 second = 1000 milliseconds so if the frame took 16 milliseconds to // update, deltaTime = 16 / 1000 = 0.016 float deltaTime = Game.LastFrameTime; // Increment the angle by the speed * deltaTime, this allows the speed to be constant no matter what FPS // the game is running at RotationAngle += (RotationSpeed * deltaTime); // Make sure the angle is always in the range of 0f to 360f RotationAngle = (RotationAngle + 360f) % 360f; // Convert the angle into Radians float angleAsRadian = RotationAngle * DegreesToRadians; // Get the new offsets using Sine and Cosine and multiply those values by the Radius double offsetX = Math.Cos(angleAsRadian) * RotationRadius; double offsetY = Math.Sin(angleAsRadian) * RotationRadius; // Get the player's position as the origin Vector3 origin = Game.Player.Character.Position; // Create the object's position relative to the origin Vector3 rotatedObjectPosition = origin + new Vector3((float)offsetX, (float)offsetY, 0); // Draw a marker to show the rotated position World.DrawMarker(MarkerType.DebugSphere, rotatedObjectPosition, Vector3.Zero, Vector3.Zero, new Vector3(.25f, .25f, .25f), Color.Magenta); } } } This just rotates a sphere around the player on the X and Y axis. If you want to rotate in all 3 dimensions, then it gets more complicated than this. Edited July 8 by LeeC22 Link to comment Share on other sites More sharing options...
Lbf666 Posted July 8 Author Share Posted July 8 25 minutes ago, LeeC22 said: 在最简单的形式中,它是使用正弦和余弦生成具有指定半径的圆,然后使用您正在旋转的对象作为原点的情况。 我可以尝试解释所有的数学,但我可能会让它听起来太复杂,所以这里有一个小脚本来演示它。创建一个名为 RotateAround 的文件.cs,将此代码复制到其中,然后将其放在脚本文件夹中。这是使用ScriptHookVDotNet v2创建的,因此,如果您使用的是v3,则可能必须更改文件名以确保其正常运行...虽然不太确定。 using System; using System.Drawing; using GTA; using GTA.Math; namespace RotateAround { public class cRotateAround : Script { private float RotationAngle = 0f; private float RotationSpeed = 20f; private float RotationRadius = 3f; const float DegreesToRadians = 0.01745f; public cRotateAround() { Tick += onTick; Interval = 0; } private void onTick(object sender, EventArgs e) { // Exits from the loop if the game is loading if (Game.IsLoading) return; // Get the last frame time as deltaTime // deltaTime is a fraction of a second, 1 second = 1000 milliseconds so if the frame took 16 milliseconds to // update, deltaTime = 16 / 1000 = 0.016 float deltaTime = Game.LastFrameTime; // Increment the angle by the speed * deltaTime, this allows the speed to be constant no matter what FPS // the game is running at RotationAngle += (RotationSpeed * deltaTime); // Make sure the angle is always in the range of 0f to 360f RotationAngle = (RotationAngle + 360f) % 360f; // Convert the angle into Radians float angleAsRadian = RotationAngle * DegreesToRadians; // Get the new offsets using Sine and Cosine and multiply those values by the Radius double offsetX = Math.Cos(angleAsRadian) * RotationRadius; double offsetY = Math.Sin(angleAsRadian) * RotationRadius; // Get the player's position as the origin Vector3 origin = Game.Player.Character.Position; // Create the object's position relative to the origin Vector3 rotatedObjectPosition = origin + new Vector3((float)offsetX, (float)offsetY, 0); // Draw a marker to show the rotated position World.DrawMarker(MarkerType.DebugSphere, rotatedObjectPosition, Vector3.Zero, Vector3.Zero, new Vector3(.25f, .25f, .25f), Color.Magenta); } } } 这只是在X和Y轴上围绕玩家旋转一个球体。如果你想在所有3个维度上旋转,那么它变得比这更复杂。 ohhhhhhhh!! brother I Love you , let me try this , I've been looking for it for a long time very very very thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now