- Home /
Issues with movement in an RTS Game.
After two years of programming in Unity with a couple of friends, I had to move away from them. Becaus of this and the fact that I had to leave our project, I decided to start my own. I decided it would be an RTS because of my love for the genre. I have, however, run into an issue. When I tested the script below on one of my basic units it worked fine. However, my problem is the fact that when I spawn more units the Game's framerate suddenly drops. I don't even have to spawn many units for this to happen. Often five is the amount that is needed before the game becomes completely unplayable. I hope some of you can help me with this, and would appreciate it if you could. My friends and I also had the same issue back when I was coding with them.
Here's the script. I have tried deactivating the Navmesh.
using UnityEngine; using System.Collections;
public class villager_behaviour_1 : MonoBehaviour {
//Villager_components
private GameObject villager;
//Villager_movement;
[SerializeField]
private bool selected;
[SerializeField]
private float speed_move;
private float standard_speed;
[SerializeField]
private float speed_rotate;
[SerializeField]
private Vector3 newposition;
private float villager_y;
private NavMeshAgent agent;
private bool leftclick_exit = true;
void Start ()
{
//Villager_components
villager = this.gameObject;
//Villager_movement
newposition = villager.transform.position;
standard_speed = speed_move;
agent = villager.GetComponent<NavMeshAgent>();
villager_y = villager.transform.position.y;
}
void FixedUpdate ()
{
agent.speed = speed_move;
agent.angularSpeed = speed_rotate;
if ((Input.GetMouseButtonDown(0)))
if (leftclick_exit == true)
selected = false;
RaycastHit hit_screen_position;
Ray ray_screen_position = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonUp(1))
{
if (selected == true)
{
if (Physics.Raycast(ray_screen_position, out hit_screen_position, 500))
{
newposition = hit_screen_position.point;
}
}
}
agent.SetDestination(newposition);
newposition.y = villager_y;
}
void OnMouseEnter ()
{
leftclick_exit = false;
}
void OnMouseDown ()
{
selected = !selected;
}
void OnMouseExit ()
{
leftclick_exit = true;
}
}
Any reason why you are doing this in FixedUpdate ins$$anonymous$$d of Update? FixedUpdate is typically for physics. I would also suggest putting the Raycast functions inside the $$anonymous$$ouseUp Events as you are doing unnecessary raycasts and not using them. All your villagers are doing a ton of raycasts