Unity Rotate Object

A simple script to rotate object in Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    Vector3 movement;
    public int xi, yi, zi;

   
    void Start()
    {
        movement = new Vector3(xi, yi, zi);
    }

    
    void Update()
    {
        transform.Rotate(movement * Time.deltaTime);
    }
}


Add values to the public variables xi, yi, zi depending on the speed and direction you would like the object to rotate.

AI scripts for autopilot, rotate to face object and distance/angle calculations

Here are scripts to help with AI for
Autopilot
Rotate to face an object
Calculate distance between two objects
Calculate angle of two objects

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

// A very simplistic car driving on the x-z plane.

public class Drive : MonoBehaviour
{
    
    public float speed = 10.0f;
    float autoPilotSpeed = 5.0f;
    public float rotationSpeed = 100.0f;
    public GameObject gasCan;
    bool autoPilotMode = false;

  

    // mathematical equations for AI machine learning------------------------

    void CalculateDistance()
    {
        Vector3 tankPosition = this.transform.position;
        Vector3 gasCanPosition = gasCan.transform.position;

        float distance = Mathf.Sqrt(Mathf.Pow(tankPosition.x - gasCanPosition.x,2) + Mathf.Pow(tankPosition.y - gasCanPosition.y,2) + Mathf.Pow(tankPosition.z - gasCanPosition.z, 2));

        float unityDistance = Vector3.Distance(tankPosition, gasCanPosition);   

        Debug.Log("Distance: " + distance.ToString());
        Debug.Log("Unity Distance: " + unityDistance.ToString());
    }


    void CalculateAngle()
    {
        Vector3 tankAngle = this.transform.up;
        Vector3 gasCanAngle = gasCan.transform.position - this.transform.position;

        float dot = tankAngle.x * gasCanAngle.x + tankAngle.y * gasCanAngle.y;// this didnt work, not correct
        float angle = Mathf.Acos(dot / (tankAngle.magnitude * gasCanAngle.magnitude));

        Debug.Log("Angle: " + angle * Mathf.Rad2Deg);
        Debug.Log("Unity Angle: " + Vector3.Angle(tankAngle, gasCanAngle)); ;

        Debug.DrawRay(this.transform.position, tankAngle * 10, Color.green, 2);
        Debug.DrawRay(this.transform.position, gasCanAngle, Color.red, 2);

        int clockwise = 1;
        if(GetCrossProduct(tankAngle, gasCanAngle).z < 0)
        {
            clockwise = -1;
        }

        float unityAngle = Vector3.SignedAngle(tankAngle, gasCanAngle, this.transform.forward);

        this.transform.Rotate(0,0,unityAngle);
    }

  

    Vector3 GetCrossProduct(Vector3 v, Vector3 w)
    {
        float xMult = v.y * w.z - v.z * w.y;
        float yMult = v.z * w.x - v.x * w.z;
        float zMult = v.x * w.y - v.y * w.x;

        Vector3 crossProd = new Vector3(xMult, yMult, zMult);

        return crossProd;

    }

    //---------------------------------------------------------
    //**********useful functions for ai and movement***********
    //******************************************
    //using AI, rotate gameobject toward a target/// pass in the vector 3 of two game objects 
    void rotateToTarget(GameObject targetObject)
    {
        Vector3 whoIslooking = this.transform.up;
        Vector3 targetObjectAngle = targetObject.transform.position - this.transform.position; 
        float unityAngle = Vector3.SignedAngle(whoIslooking, targetObjectAngle, this.transform.forward);

        this.transform.Rotate(0, 0, unityAngle);
    }
    //---------------------------------------------------
    //----------------------------------------------
    //*********calculate the distance to an object***********
    //********* returns a float of the distance between this game object and a target object
    float distanceToTarget(GameObject targetObject)
    {
        return Vector3.Distance(this.transform.position, targetObject.transform.position);
    }
    //-----------------------------------------------------------------
    //----------------------------------------------------------------
    //*******************Use AI to rotate to a target and move there******
    //**** not very good yet doesnt account for obstacles etc, 
    //uses a speed float var
    void AutoPilot(float autoPilotSpeed, GameObject targetGameObject)
    {
        rotateToTarget(targetGameObject);
        this.transform.Translate(this.transform.up * autoPilotSpeed, Space.World);

    }


    void Update()
    {
        // Get the horizontal and vertical axis.
        // By default they are mapped to the arrow keys.
        // The value is in the range -1 to 1
        float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;

        // Make it move in meters per second instead of meters per frame...
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;

        // Move translation along the object's z-axis
        transform.Translate(0, translation, 0);

        // Rotate around our y-axis
        transform.Rotate(0, 0, -rotation);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            CalculateDistance();
            CalculateAngle();
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            rotateToTarget(gasCan);
            Debug.Log("Rotating to face " + gasCan.name);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("Deedubs distance: " + distanceToTarget(gasCan));
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            autoPilotMode = !autoPilotMode;
            if (autoPilotMode) AutoPilot(autoPilotSpeed, gasCan);
        }


    }
}