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);
}
}
}