I am trying to teleport the character when they touch a cube, but for some reason, it prints the message that it the collision is being detected, but the character's position is not changing. It works for another scenario that is almost exactly the same, but even if I put this in another script, it does not teleport the character. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class othercollision : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name == "teleportcube")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("Collision detected");
}
}
}
Here is the script that has something similar working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class detectcollision : MonoBehaviour
{
public GameObject tplocation1;
public GameObject player;
public GameObject entranceloc;
void OnControllerColliderHit(ControllerColliderHit hit)
{
//This if statement does Debug.Log the message and it also changes the player's position.
if (hit.gameObject.name == "trigger")
{
Debug.Log("triggered");
gameObject.transform.position = tplocation1.transform.position;
Thread.Sleep(7);
Application.Quit();
}
//This if statement does not change the player's position, but Debug.Logs the teleporting message.
if (hit.gameObject.name == "entrance")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("teleporting...");
}
}
}
Try to make the Vector3 beforehand as a variable or a temporary variable and then assign it to the position. If you did then maybe the values that you have entered are out of boundary somewhy, but that's unlikely, try changing them.
Related
I have a Script Coin-Counter connected with a Text. Every time the Player and the Coin are colliding the coinScore decreases by 1. If I want to display the coinScore in Update() this doesn't work. Why?
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScore : MonoBehaviour
{
[SerializeField] private Text coinScorer;
private int coinScore;
private int oldCoinScore;
void Update()
{
coinScorer.text = coinScore.ToString(); // This doesn't work.
oldCoinScore = coinScore;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
coinScore += 1;
Destroy(gameObject);
//coinScorer.text = coinScore.ToString(); //This works.
}
}
Your collision method is destroying this game object.
There is no update to run after that.
The way my code works is it checks if touching the ground, then if its true, it waits for space to get put in as an input, then if both statements are true, it uses the rigid body method to propel itself into the air.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public string objectName;
public Rigidbody rb;
public int h;
// Start is called before the first frame update
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("On ground statement is true.");
if(Input.GetKey("space"))
{
rb.AddForce(0, h, 0);
}
}
}
}
The problem is, when i run it, it knows its on ground, but it won't respond to key inputs.
I know it can't be a problem with my computer, as my other controls work.
It's a bad idea to be testing for player input inside a collision event - if the player is pressing space, it will only ever register during the exact same frame that the object collided with something. A better place for that would be the Update() method, which happens every frame.
You should also create a variable that stores whether the player is currently on the ground or not, based on your collision events. Your input code will check this variable when deciding if the player is allowed to jump.
Try this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public string objectName;
public Rigidbody rb;
public int h;
public bool onGround = false; // new
// get your player input in here instead
void Update()
{
if(onGround && Input.GetKey("space"))
{
rb.AddForce(0, h, 0);
}
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("On ground statement is true.");
onGround = true; // new
}
}
// new
void OnCollisionExit(Collision col)
{
if(col.gameObject.name == objectName){
Debug.Log("No longer on the ground.");
onGround = false;
}
}
}
I just want to move a bullet (but it doesn't work) and test on cube (but the code too did not move the cube).
The commented codes also did not move the game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movetest : MonoBehaviour
{
//public GameObject cube;
// Use this for initialization
public GameObject testmovecube;
public Rigidbody rb;
void Start () {
//testmovecube = this.GetComponent<GameObject>();
//rb = testmovecube.GetComponent<Rigidbody>();
move();
//
}
// Update is called once per frame
void Update () {
}
private void move()
{
testmovecube.transform.position =
Vector3.MoveTowards(transform.position, new Vector3(226, 1, 226) , 2);
//transform.Translate(Vector3.forward);
//rb.AddForce(Vector3.forward);
}
}
Please any help is appreciated.
I am making a really short C# Unity game for a college class I am in and I have created a script for a trap that deactivates my player on contact that also includes a replay button. It all works except when I replay, the player remains inactive.
How would I modify my script to make it so the player reactivates on replay?
Also, this class I am in is a beginner class, I'm not super good at this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Trap : MonoBehaviour
{
public GameObject playerExplosion;
public GameObject gameOverUI;
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
other.gameObject.SetActive(false);
gameObject.SetActive(false);
gameOverUI.SetActive(true);
PlayerController.gameOver = false;
}
}
Edit: Here is the replay script too. It works on a health bar system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReplayGame : MonoBehaviour
{
public Transform player;
public Image uiBar;
public GameObject GameOverUI;
public static Vector3 startPosition;
private float fillAmount;
void Start()
{
startPosition = player.position;
fillAmount = uiBar.fillAmount;
GameOverUI.SetActive(false);
}
public void Click ()
{
PlayerController.gameOver = false;
player.position = startPosition;
uiBar.fillAmount = fillAmount;
GameOverUI.SetActive(false);
}
}
you will have to re Activate your player, or you have to Instantiate a new player.
public void Replay ()
{
//ReActivate
myPlayer.gameObject.SetActive(true);
//or Instnatiate a new
Instantiate(myPlayer);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public Transform target;
// Update is called once per frame
void Update ()
{
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Test")
{
this.transform.position = target.position;
}
}
}
I have a ThirdPersonController and i want it to collide with a cube or cylinder.
The script is attached to the ThirdPersonController.
I tried to add either to the cylinder or the cube Rigidbody turned on/off the Use Gravity and the Is Kinematic but nothing. It's not getting to the event.
ThirdPersonController uses CharacterController and OnControllerColliderHit is used for that not OnTriggerEnter.
Note that you must move it with the Move function not directly by its transform in order for OnControllerColliderHit to be called.
void OnControllerColliderHit(ControllerColliderHit hit)
{
}
Every thing is correct but the thing wrong here is you are changing the position of the object which is colliding with a second object , but the thing id the thing is the collider is already there.....
As it's alternative try to print any statement when collision happens like this
private void OnTriggerEnter(Collider other)
{
if (other.tag==your_tag)
{
print("message");
}
}