Unity3D scene not working properly and lagging after reloading - unity3d

I have looked all over internet, but I cannot find a solution to my problem. I am trying to create a game and it has 3 scenes: the Game Start, Main and Game over.
The problem is that when trying to load the main level from other scenes it does not do what it has to do (e.g. jump) and the scene itself is lagging. When I try to load only the Main Scene it works ok, but after the character dies and the scene is reloaded, it starts to lag again and I cannot jump or do anything it is supposed to do.
Any ideas on what the problem might be?
using UnityEngine;
using System;
public class Player : MonoBehaviour
{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
private bool shouldJump = true;
// Update is called once per frame
private float jumpThreshold = 0.5f;
private float previousJumpTime;
void FixedUpdate ()
{
// Jump
float mc = MicControl.loudness;
//Debug.Log (mc);
if (mc>1.3f && shouldJump)
{
shouldJump = false;
previousJumpTime = Time.time;
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
if (!shouldJump)
{
if(Time.time-previousJumpTime>jumpThreshold)
{
shouldJump = true;
}
}
// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel ("main");
}
}

You said your level is called Main, but in the code you are loading "main", i'm not sure if that's the problem, but you seem to be loading the level correctly, so check if is main or Main the exact name of the level.
Also, when compiling, make sure you have all levels checked

With provided data its impossible to say what causes low performance, but I recommend you to use Profiler tool (can be found in personal version of Unity 5) and figure out what scripts and functions are problematic.
Also, try to avoid calling GetComponent<Rigidbody2D>() in Update/FixedUpdate/LateUpdate and cache this component instead.

I second what Utamaru said.
Rigidbody2D myRigidbody2d;
void Start ()
{
myRigidbody2d = GetComponent<Rigidbody2D>(); //Do this once
}
Inside your fixed update, you can do this:
void FixedUpdate ()
{
// Jump
float mc = MicControl.loudness;
//Debug.Log (mc);
if (mc>1.3f && shouldJump)
{
shouldJump = false;
previousJumpTime = Time.time;
myRigidbody2d.velocity = Vector2.zero;
myRigidbody2d.AddForce(jumpForce);
}
if (!shouldJump)
{
if(Time.time-previousJumpTime>jumpThreshold)
{
shouldJump = true;
}
}
// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
I can't see the rest of your code so I am not really sure that is the problem but give this a try.

Related

Difficulties flipping a collider in Unity when attacking

I am working on a small game for a school project, in which my player needs to attack enemies in a level. My plan is to have a collider that is enabled in an attached script, and then disabled when the attack is done. My current problem is that that the collider does not flip the way it is supposed to, it seems to flip directly on the overall x axis instead of flipping in the x axis related to the player. It is a child of the player so I am clueless as to why it is doing this. Any solutions or other approaches would be greatly appreciated. I will attach the current script that controls the collider below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerticalSword : MonoBehaviour
{
//Variables use to active the colliders
Collider2D AttackColliderVertical;
//Variables for the location of the collider
Vector2 attackOffset;
private void Start()
{
AttackColliderVertical = GetComponent<Collider2D>();
attackOffset = transform.position;
AttackColliderVertical.enabled = false;
}
public void FixedUpdate()
{
attackOffset = transform.position;
}
public void AttackUp()
{
AttackColliderVertical.enabled = true;
if (attackOffset.y > 0)
{
transform.position = attackOffset;
}
else if (attackOffset.y < 0)
{
transform.position = new Vector2(attackOffset.x, (attackOffset.y * -1)); //I think the problem is somewhere in this if and else if statement
}
print("Attack up successful"); //Error checking (This works when run)
}
public void AttackDown()
{
AttackColliderVertical.enabled = true;
if (attackOffset.y > 0)
{
transform.position = new Vector2(attackOffset.x, (attackOffset.y * -1));
}
else if (attackOffset.y < 0)
{
transform.position = attackOffset; //I think the problem is somewhere in this if and else if statement
}
print("Attack down successful"); //Error checking (This works when run)
}
public void StopAttack()
{
AttackColliderVertical.enabled = false;
}
}
Use transform.localPosition, not transform.position (that's its world space position). You need to change it everywhere in this script; the Start() function and the two attack functions

Resetting Character Position in Unity is Not Working

I'm working on a sports game where if a player skates into the goalie crease, I want all the player positions to reset to the center of the ice and do a three second countdown before play resumes.
I have tried to hardcode the starting position for the main player in a variable called PlayerStart and I call Player.transform.position = PlayerStart. When I did this, the player didn't move so I tried to switch the object I was setting as the player. This did what I wanted, but the mouse functionality changes for some reason and when the countdown ends, the player just goes right back to the position they were in before the crease violation was called.
Other things I've tried:
transform.SetPositionAndRotation
PlayerStart = Player.transform.position (instead of hard coding the numbers in)
Here is my code:
public class Crease : MonoBehaviour
{
private float Delay = 0;
private bool CreaseViolated = false;
private GameObject Player;
public Vector3 PlayerStart;
// Start is called before the first frame update
void Start()
{
Ring = GameObject.Find("Ring");
Player = GameObject.Find("FPSController");
PlayerStart = new Vector3(29.75f, 6.03999996f, 4.42000008f);
}
// Update is called once per frame
void Update()
{
Delay -= Time.deltaTime;
if (CreaseViolated)
{
CreaseViolation();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name != "Ring"
&& other.gameObject.name != "TeamGoalie"
&& other.gameObject.name != "OpponentGoalie")
{
CreaseViolated = true;
Delay = 3;
}
}
void CreaseViolation()
{
if (Delay > 0)
{
PlayerTip.GetComponent<PickupRing>().HasRing = false;
Opponent.GetComponent<AI>().HasRing = false;
Ring.transform.parent = null;
}
else
{
text.text = " ";
if (CreaseViolated)
{
Debug.Log("Player position before: " + Player.transform.position);
Player.transform.position = PlayerStart;
Debug.Log("Player position after: " + Player.transform.position);
//Player.transform.SetPositionAndRotation(PlayerStart + new Vector3(0f, 0.800000012f, 0f), new Quaternion(0f, -0.707106829f, 0f, 0.707106829f) + new Quaternion(0f, 0f, 0f, 1f));
GameObject.Find("Countdown").GetComponent<CountdownText>().timeRemaining = 4;
CreaseViolated = false;
}
}
}
}
Here is a short YouTube video showing my code and the demo: https://www.youtube.com/watch?v=mZt_4AppBh8
this problem is all solved now thanks to an awesome person at my university helping me out! The solution was disabling the CharacterController before repositioning the player and then enabling it again after.
So this:
Player.transform.position = PlayerStart;
in the CreaseViolation function becomes
cc.enabled = false;
PlayerController.transform.position = PlayerControllerStart;
cc.enabled = true;
with cc being declared earlier as
private CharacterController cc;
and in the start function I assigned it with the value
cc = Player.GetComponent<CharacterController>();
with PlayerController being set to the FPSController.
I renamed Player to PlayerController for more clarity.
Hopefully this helps anyone having the same problem I was having!
The Above answer works perfectly but might bring some issues when doing networked changes in player transform positions.
The reason the transform change doesn't work without disabling the Character Controller is because the character controller overrides the transforms.
To Disable this and enable an Auto Synchronization between transforms GoTo:
Edit > Project Settings > Physics > (Enable Auto Sync Transforms)

Destroy a game object after it leaves the view of the camera in Unity

I'm currently making a game in Unity where I'm trying to get destroy the clones of a prefab only after they leave the view of the camera only after they already entered the view of the camera in the first place. However, for some reason, my code is instantly destroying the clones as soon as their instantiated. Does anyone know how I could solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractControl : MonoBehaviour
{
Rigidbody2D rb;
GameObject target;
float moveSpeed;
Vector3 directionToTarget;
// Use this for initialization
void Start()
{
target = GameObject.Find("White Ball");
rb = GetComponent<Rigidbody2D>();
moveSpeed = 3f;
}
// Update is called once per frame
void Update()
{
MoveInteract();
OnBecameVisible();
}
/*void OnTriggerEnter2D(Collider2D col)
{
switch (col.gameObject.tag)
{
case "ColouredBall Highress":
BallSpawnerControl.spawnAllowed = false;
Destroy(gameObject);
target = null;
break;
case "Star":
Collision collision = new Collision();
break;
}
} */
void MoveInteract()
{
if (target != null)
{
if(ScoreScript.scoreValue > 3)
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
directionToTarget.y * moveSpeed);
}
else
{
directionToTarget = new Vector3(0, -1, 0);
rb.velocity = new Vector2(0, directionToTarget.y * moveSpeed);
}
}
else
rb.velocity = Vector3.zero;
}
void OnBecameInvisible()
{
if (gameObject.tag == "ColouredBall Highress")
{
Destroy(gameObject);
}
if (gameObject.tag == "Star")
{
Destroy(gameObject);
}
}
void OnBecameVisible()
{
if (gameObject.tag == "ColouredBall Highress" || gameObject.tag == "Star")
{
OnBecameInvisible();
}
}
}
I tried to solve the problem by first requiring the objects to become visible in order for them to be able to get destroyed when out of view of the camera. In short I'm looking for the OnExit collider version for on OnBecameInvisible. I guess I could make the whole screen a collider and use on Exit collider on it. Does someone possibly also know how I could make a collider that covers the camera view?
It is because you call OnBecameInvisible() from OnBecameVisible. So when they are visible they get destroyed.
Also your code is doing so many redundant things you also call OnBecameVisiblefrom Updateetc.
You can simply use this instead:
Renderer m_Renderer;
void Start()
{
m_Renderer = GetComponent<Renderer>();
}
void Update()
{
//It means object is NOT visible in the scene if it is false is visible
if (!m_Renderer.isVisible)
{
Destroy(gameObject);
}
}
Note that: Destroying/Instantiating objects are not the best practices at this circumstance. Because it causes garbage collector to work a lot and it is expensive and can slow down your game. You can use object pooling instead. It basically puts object which are not in the field of view into an object pool and you keep their references and can use them later. Therefore, it is less costly than your method.
You are calling OnBecameVisible every frame, so basically the first frame the object destroys itself. Deleting it from Update should do the trick, Unity already calls it for you.

Field of View for enemy only works with 1 enemy

I have some problems with a script for a "Field Of View" of the enemy. After watching a tutorial to create a field of view for the player I thought I can switch it to the enemy's so that they detect the player and do some other stuff. I created a boolean variable playerInRange to detect if the enemies can detect the player and set this variable to true or false.
It works fine with just one enemy. When I add another one, the new enemy will not detect the player. So maybe it is related to the coroutine, but I am not sure.
Here is a little bit of my code:
void Start() {
StartCoroutine("FindTargetsWithDelay", .2 f);
}
IEnumerator FindTargetsWithDelay(float delay) {
while (true) {
yield
return new WaitForSeconds(delay);
FindVisibleTargets();
}
}
public void FindVisibleTargets() {
visibleTargets.Clear();
Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
for (int i = 0; i < targetsInViewRadius.Length; i++) {
Transform target = targetsInViewRadius[i].transform;
Vector3 dirToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2) {
float dstToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask)) {
// Not so nice solution!
// The movement should be in a separate script!
visibleTargets.Add(target);
nav.SetDestination(player.position);
anim.SetBool("IsRunning", true);
if (dstToTarget < attackRange) {
playerInRange = true;
Debug.Log(playerInRange);
}
}
} else {
anim.SetBool("IsRunning", false);
playerInRange = false;
Debug.Log(playerInRange);
}
}
}
Thank you guys for your little hint. It was really a little hierarchy problem :( Sorry for that newbie/DAU question.
Cheers
Nico

How to display "stop" message on screen when car reaches at a traffic signal?

I have used box colliders and GUI function... but the problem with box collider is that your car stops after hitting the collider and and I also want message which is displayed on the sceen to be fade away after 10 seconds.
Here's my code:
var msg = false;
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "trafficLight")
{
Debug.Log("collided");
msg=true;
}
}
function OnGUI ()
{
if (msg== true)
{
GUI.Box (Rect (100,0,500,50), "You need to stop if the traffic signal is red");
}
}
but the problem with box collider is that your car stops after
hitting the collider
You should clarify this. Eventually post another question with the specific problem and possibly an SSCCE.
I also want message which is displayed on the sceen to be fade away
after 10 seconds.
Then put something like this inside the Update method of your MonoBehavior:
float timeElapsed;
float timeLimit = 10f;
void Update()
{
if (msg)
{
timeElapsed += Time.deltaTime;
if (timeElapsed >= timeLimit)
{
msg = false;
timeElapsed = 0f;
}
}
}
Alternative, for a more elegant approach, you can use coroutines:
IEnumerator FadeAfterTime(float timeLimit)
{
yield return new WaitForSeconds(timeLimit);
msg = false;
}
void OnCollisionEnter(Collision collision)
{
if(theCollision.gameObject.name == "trafficLight")
{
msg=true;
StartCoroutine(FadeAfterTime(10f));
}
}
From what I understand, you want a stop message to appear on the screen when the player is near a stop sign, so that the player has to stop the car himself.
In order to do this, for starters you'll need to make your box a trigger instead of a collider. There's a small tickbox on the collider of each object which says Trigger. You'll want this to be ticked.
Then put a script similar to this in the trigger box near your traffic light:
var msg = false;
function Start()
{
}
function OnTriggerEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "car") //where "car" you put the name of the car object
{
msg = true;
StartCoroutine(FadeAfterTime(10f));
}
}
IEnumerator FadeAfterTime(float timeLimit)
{
yield return new WaitForSeconds(timeLimit);
msg = false;
}
function OnGUI ()
{
if (msg== true)
{
GUI.Box (Rect (100,0,500,50), "You need to stop if the traffic signal is red");
}
}
function Update()
{
}
In essence the traffic light trigger box will detect when the car enters the designated area, and will display the GUI, with the fade out script provided by Heisenbug in the previous answer.
I can't test this myself at the moment, but it should work for you. If you have any questions, lemme know.
You should use RayCasting function for this purpose.
I have provided a real example here.
using UnityEngine;
using System.Collections;
public class carMovement : MonoBehaviour {
bool isStop = false;
public float speed = 30f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!isStop) {
transform.position += (Vector3.forward * Time.deltaTime * speed);
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawRay (transform.position, fwd, Color.green);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
isStop = true;
transform.position = transform.position;
}
}
}
}