Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Criminal Enterprises
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

*DO NOT* SHARE MEDIA OR LINKS TO LEAKED COPYRIGHTED MATERIAL. Discussion is allowed.

how to Implement rotatearound in unity?


Lbf666
 Share

Recommended Posts

Lbf666

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

LeeC22
Posted (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 by LeeC22
Link to comment
Share on other sites

Lbf666
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.