using UnityEngine;
using UnityEngine.AI;
public class enemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
void Update()
{
enemy.SetDestination(Player.position);
}
}
This is my code. I followed the tutorial perfectly and I don't know why the nav mesh runs in the exact opposite direction. I have tried to research but all the questions online ask how to make the nav mesh run away. Can you please help?
Related
I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}
I don't know how to fix it. It's a 2D game. When you collide with the box, it should load/teleport you to the next scene, but it doesn't.
I tried everything on the internet and it didn't work.
using UnityEngine;
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour
{
public void LoadNextLevel()
{
void OnTriggerEnter2D(Collider2D collider)
{
SceneManager.LoadScene(1);
}
}
}
I expect it to teleport me to my next level.
It looks like you nested the OnTriggerEnter2D() function inside of LoadNextLevel()? That's what the Local Function warning is referring to.
Those should be two separate functions, not one within the other. OnTriggerEnter2D() is a function of MonoBehavior; the MonoBehavior (VictoryZone) is what gets notified of the collision.
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour {
public void LoadNextLevel() {
SceneManager.LoadScene(1);
}
void OnTriggerEnter2D(Collider2D collider) {
LoadNextLevel();
}
}
Note: you might also need to check the GameObject associated with collider to make sure that it's a player, and not an enemy or something (if, hypothetically, you had enemies or projectiles or other objects with colliders moving into the victory zone).
I'm working on 2D Unity game and I would like to know how we can display an image on my inventory when the player collide with an object. For exemple, if the player collide with a drumstick, the drumstick image appear on the inventory menu.
Best Regards.
Yacine TAZDAIT.
Method #1
There are many different ways to display an image. For example, you can have an image with an image component, and you switch the component on and off whenever you want the image to appear/disappear. You can do this using code similar to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Image drumstick;
public void Start()
{
toggleDrumstick(); // This will toggle the drumstick. For example, if the drumstick is not being shown at the time, the drumstick will show on the screen. The opposite is true.
}
public void toggleDrumstick() {
drumstick.enabled = !drumstick.enabled;
}
}
Method #2
The code above is a great solution, but there is a more modular way to do it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Drumstick : MonoBehaviour
{
public static bool enabled = this.image.enabled;
}
I recommend the method above. The reason for this is because every script can now access the drumstick's status. For example, your player script can do this.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void doSomething () {
Drumstick.enabled = true; // make the image appear.
}
}
For any of these methods to work, make sure your drumstick uses the image component.
EDIT:
To answer your question further, here is a way to implement method #2 in your code. In your player script, you can use OnCollisionEnter and the method above to make the drumstick appear.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "drumstick") Drumstick.enabled = false;
}
}
For this to work, make sure that the drumstick has the tag "drumstick".
I am using an animated sprite to collide and delete an enemy sprite. The enemy sprite disappears when it hits my floor (sprite) also. (All have rigidbody2D). How do I get the enemy sprite to ignore the floor and everything else except the animated sprite?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
void OnCollisionEnter2D()
{
if (gameObject.tag.Equals("Enemy"))
{
Destroy(gameObject);
}
}
}
Go to Edit > Project Settings > Physics (or Physics2D) and edit the Layer Collision Matrix:
I have a scene with two cubes, and canvas. One cube falls onto another. Canvas appearing must be triggered by collision. It's does not working. Where's the problem?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void Start()
{
}
void OnGUI()
{
GUICanvas.gameObject.SetActive(true);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "FallingCube")
{
OnGUI();
}
}
void Update()
{
}
}
Put the code below inside your EverythingInside script. Make sure Collider and Rigidbody are attached to both cubes. Also make sure to drag your Canvas from the Editor to the GUICanvas slot.
public Canvas GUICanvas;
void Start()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("FallingCube"))
{
GUICanvas.gameObject.SetActive(true);
}
}
void Update()
{
}
Unity Physics tutorials.
Unity UI Tutorials.
Unity Scripting Tutorials.
Your problem is that OnGUI() as a function is called once every frame in Unity, regardless of whether or not you call it manually. Also, OnGUI is reserved for doing things like rendering simple buttons and text directly to the screen, it doesn't perform any actual logic.
All you have to do to make this work is just replace where you've called OnGui() with GUICanvas.gameObject.SetActive(true)
and get rid of the other functions.
using UnityEngine;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void OnCollisionEnter(Collision col)
{
if (col.tag == "FallingCube")
{
GUICanvas.gameObject.SetActive(true);
}
}
}
That right there is literally all you need to make it work. Bear in mind that if you change it later on to be a trigger instead of two colliding cubes you will need to replave OnCollisionEnter with OnTriggerEnter.