Moving gameObject Script

This code is very simple and all you have to do is apply the code to the designated gameObject. From the inspector view, you can choose which direction/rotation you want the object to move in.

Example use of Code:
This code is well suited for objects that are static or background objects. I use it to rotate background scenery
or NPC’s who are there for aesthetic.

DISCLAIMER:
You can select more than one direction/rotation at a time! Do this at your own accord (No Promise It Will Work The Way You Want)! This code is not 100% efficient/accurate so keep that in mind! This is an open-source code which means you are free to use it as you please.

You can see this code in action by clicking (HERE)

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

/*
This code is made and built by Mohammad Ishtiaq
Description:
This code is very simple and all you have to do is apply the code to the designated gameObject.
From the inspector view, you can choose which direction/rotation you want the object to move in.
Example use of Code:
This code is well suited for objects that are static or background objects. I use it to rotate background scenery
or NPC's who are there for aesthetic.
DISCLAIMER:
You can select more than one direction/rotation at a time! Do this at your own accord (No Promise It Will Work The Way You Want)!
This code is not 100% efficient/accurate so keep that in mind!
This is an open-source code which means you are free to use it as you please.
*/

public class NPCMovement : MonoBehaviour
{
    //Speed at which you want the gameobject to move
    public int speed;

    //Decied which direction you want the object to move in
    public bool goForward = true;
    public bool goBackwards = false;
    public bool goRight = false;
    public bool goLeft = false;

    //Rotate Right or Left on Y Axis
    public bool yRotateRight = false;
    public bool yRotateleft = false;
   
    //Rotate Right or Left on X Axis
    public bool xRotateup = false;
    public bool xRotatedown = false;
   
    //Rotate Right or Left on Z Axis
    public bool zRotateRight = false;
    public bool zRotateleft = false;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        //Directional Movement
        if (goForward == true)
        {
            transform.position += transform.forward * speed * Time.deltaTime;
        }
        else if (goBackwards == true)
        {
            transform.position += transform.forward * -speed * Time.deltaTime;
        }
        else if(goRight == true)
        {
            transform.position += transform.right * speed * Time.deltaTime;
        }
        else if (goLeft == true)
        {
            transform.position += transform.right * -speed * Time.deltaTime;
        }

        //Angular Movement
        if (yRotateRight == true)
        {
            transform.Rotate(Vector3.up, Time.deltaTime * speed);
        }
        else if (yRotateleft == true)
        {
            transform.Rotate(Vector3.up, Time.deltaTime * -speed);
        }
        else if (xRotateup == true)
        {
            transform.Rotate(Vector3.left, Time.deltaTime * speed);
        }
        else if (xRotatedown == true)
        {
            transform.Rotate(Vector3.left, Time.deltaTime * -speed);
        }
        else if (zRotateRight == true)
        {
            transform.Rotate(Vector3.back, Time.deltaTime * speed);
        }
        else if (zRotateleft == true)
        {
            transform.Rotate(Vector3.back, Time.deltaTime * -speed);
        }
    }
}

Hey and welcome to the Unity forums.
Sharing code others might need is a great idea. I do have some notes on improvements if you dont mind tho:

  • What if people want two different speeds for movement and rotation?
  • There is a lot of bools here. Instead you could have used a variable from -1 to 1 per axis and let it be adjustable through the inspector as a slider. You cannot walk forwards and backwards at the same time anyways, so this would more closely represent the axis-implementation used by Unity itself. It would also roughly halve the code length.
  • Up and Down?^^
  • You do not need to compare bools to true or false. ‘someBool == true’ is the same as ‘someBool’. To check for ‘someBool == False’ we can simply check not someBool, written ‘!someBool’.

Furthermore, from your title it is not entirely clear you are trying to offer some code for people to use, so it likely wont be found either. You might want to edit the title to make your intentions more clear.

1 Like

Thank you for the feedback, I will take it into consideration and see what I can do. Like I said the code isn’t 100% efficient or perfect :slight_smile: