Teleporting PacMan to other side of the Maze - unity3d

I'm creating a teleporting for my PacMan game to get to the other side of the maze when triggered. I have the code then but when colliding nothing happens. I need that when i trigger the Left Portal i go to the right portal. I am thinking that the Pathfinding i have might be the problem. Thanks
Portal Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
public Transform warpTarget;
void onTriggerEnter2D(Collider2D other){
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}
}

Maybe you should check for the methods's name. It must start with an uppercase char instead of an lowercase, so it should be something like this:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}

Related

Destroy gameobject in Unity

I want the enemy to be destroyed when the player touches the heart. what's wrong with my code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
public GameObject player;
public float speed;
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, player.transform.position,
speed * Time.deltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (player.gameObject.CompareTag("haret"))
{
Destroy(gameObject);
}
}
}
You haven't provided an error code or anything so I don't have much to go off of ;-;
make sure the tag in the code is the same as the code in the project and put the tag on the heart.
make sure that the player gameobject has been assigned to the enemy.
make sure that the player and the heart both have a 2d collider not set to is trigger
Check your tag spelling. You have a spelling mistake there.
Without an error it is hard to figure out what the problem is. From the looks of it you might have misspelled gameObject.CompareTag("haret") where it should be gameObject.CompareTag("heart") instead.
Also make sure that the script is assigned to the enemy, that you have assigned the player gameObject in the inspector and that you have set the collider of the heart gameObject to isTrigger in the inspector.

Collision system not working. There are no errors, it just wont register that the projectile hit the player

Here is the code in which I was talking about. I can't figure out why my collider isn't working:
using System.Collections.Generic;
using UnityEngine;
public class SphereCollider : MonoBehaviour
{
GameObject obj;
void Awake ()
{
obj = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Player")
{
obj.GetComponent<Health>().health -= 25;
}
}
}
}
Rather than using the code you have, make sure to change some code. This is what you should change: get rid of the obj variable and the awake function. Then instead of finding the object with that name, use tag. Also get rid of Update(){} and take the OnCollisionEnter(){} out of it.
A few more changes are shown in code:
void OnCollisionEnter(Collision collision){
var obj = collision.GameObject;
if (obj.Tag == “Player”){
obj.GetComponent<Health>().health -= 25;
}
}
This will work as long as: the script is called Health, the variable inside Health is called health, and it is set to a float, and the player’s tag is set to Player.
Not sure if the code pasted in the question is just copied over wrong, but OnCollisionEnter() is its own unique method and should not be nested in Update().
If the player will be colliding with this Sphere object often, it is good to cache a component rather than using GetComponent frequently. However I assume in your case this Sphere will be one of many obstacles, so calling the GetComponent in the collision is fine. The issue with your current code is the gameObject you are colliding with might not have the name exactly spelled as Player, which would make your collision check fail. Caching the object that has the tag of Player is a bit redundant when you can just check it inside of the collision check.
I will use your entire snippet of code so there is no confusion
using System.Collections.Generic;
using UnityEngine;
public class SphereCollider : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
// compare if the collider's gameObjects tag is equal to our target tag (Player)
if (col.gameObject.tag == "Player")
{
// get the component of Health on our gameObject that we collided with that has the tag of Player
obj.GetComponent<Health>().health -= 25;
}
}
}

I'm using Unity and creating a text based game where your options directly affect the health and need assistance

I'm trying to add a healthbar which is affected by the choices made and link the two but am unable to do so. Any help would be appreciated.
I will recommend you that before you go any further with your game development process, you research a bit more about general OOP programming (no offense or anything, it will just make things waaay easier, trust me).
That being said, here's an example to steer you to the right direction:
You can create a script with a global variable called health and subtract it every time the player makes a decision that would "punish" them, here's an example of how that could work:
PlayerManager.cs (put this script on your player)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour {
public float health = 100f;
private void Update() {
if (health < 0)
Die();
}
private void Die () {
//Fancy animations and text can be added on this method
Destroy(gameObject);
}
}
And on your dialogue script, once they choose a wrong answer, then you can just decrease health on the proper player manager instance, like this:
DialogueSystem.cs
public PlayerManager playerManager;
public float damage = 10f;
private void Start() {
//You can initialize other stuff here as well
//This line is assuming you have the dialogue system script attach to your player as well
playerManager = GetComponent<PlayerManager>();
}
private void Update () {
//This is obviously going to change depending on how your system is built
if (wrongChoice) {
playerManager.health -= damage;
wrongChoice = false;
}
}

2D Camera movement script throwing CS0428

I'm trying to get a camera to move whenever you press either WASD or the arrow keys, but it's throwing
Error CS0428 Cannot convert method group 'GetComponent' to non-delegate type 'Transform'. Did you intend to invoke the method?
On this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameramover : MonoBehaviour
{
public Camera controlled;
Vector3 movement;
void Start()
{
Transform transform1 = controlled.GetComponent<Transform>;
}
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
}
}
I tried invoking the method, but that threw CS0201, so I tried doing
new Transform(controlled.GetComponent<Transform>;)
and that didn't work either, so I went here to ask for help.
Figured it out with the help of the wonderful people at the Game Dev Network discord server! i just had to add a pair of parentheses on the end of the controlled.GetComponen;

Drag and Drop on a certain area like mountain unity3d

I want to drag and drop an object to a particular place only like mountain. but if the user will drop the object outside the mountain it should come back to its initial position.
I already have a script on the possibility that the user will drop the object outside the mountain.. My question is that how would I be able to only drop the object specifically on the mountain only..
I tried using the OnTriggerEnter() but I don't know how to do it..
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class mountainAreaScript : MonoBehaviour {
void OnTriggerEnter(Collider collider){
if(collider.gameObject.tag == "mount"){
//Debug.Log ("working");
}
}
void OnTriggerExit(Collider collider){
if(collider.gameObject.tag == "mount"){
//Debug.Log ("working but false");
}
}
}
Give the object your dragging a rigidbody. If needed set the is Kinematic to true to avoid collision issues with other objects.