Reputation: 33
I have been having this problem with my current game.
Well the problem is that when my player(Rocket) collides with Asteroid(Enemy) the player is meant to die and restart the level but that doesn't work even thought I have put my player with a box collider with no trigger and my Asteroid with trigger and a circle collider. I have also tried to put the asteroid with trigger and player with no trigger and both of them with triggers but my Rocket just goes through the Asteroid each time. There are no errors in the console window. And below is the code I am using for this.UPDATE: I used everyone's advice and sorted out the code looking back I should of seen that putting the "OnTriggerEnter" in the Update method wouldn't work but anyway the Rocket and the Asteroid both have a "Rigibody" component on the and when the player collides with the asteroid he disappears and doesn't restart and I have started writing a respawn point but the error that comes up is "error CS1955: Non-invocable member 'Transform.position' cannot be used like a method." This is the updated code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Movement : MonoBehaviour
{
public float speed = 6.0f;
public GameObject character;
private Animator anim;
private Vector3 Respawnpoint;
private void OnCollisionEnter2D(Collision2D collision)
{
switch (collision.gameObject.tag)
{
case "Enemy":
Destroy(gameObject);
break;
default:
//nothing
break;
}
}
void Start()
{
anim = GetComponent<Animator>();
Respawnpoint = transform.position();
}
void Update()
{
if (Input.anyKey)
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
anim.SetBool("Isright", true);
}
if (Input.GetKeyDown(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
anim.SetBool("Isleft", true);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
anim.SetBool("Isright", true);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
anim.SetBool("Isleft", true);
}
else
{
}
}
}
Upvotes: 1
Views: 615
Reputation: 47
I think that the problem in your code is that your condition gameObject.tag
is a little bit wrong. I suggest changing it to collision.gameObject.tag
. Also, I think that it is better to place your void OnTriggerEnter2D (Collider2D other)
outside of the void Update()
. Please reply if this solution works.
Update: You can write something like this:
using UnityEngine.SceneManagement;
...
private void OnCollisionEnter2D(Collision2D collision)
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
switch (collision.gameObject.tag)
{
case "Enemy":
Destroy(gameObject);
SceneManager.LoadScene(sceneIndex);
break;
default:
//nothing
break;
}
}
I suggest that you use OnCollisionEnter2D
instead of OnTriggerEnter2D
because triggers are usually meant to be invisible objects that you can go through.
Upvotes: 1
Reputation: 17858
As the others have pointed out the code is structured incorrectly.
Inside your update you have your trigger method. As a result it will never be called
Simply you need to remove the 2 lines of void update() and its following opening parenthesis. And the last closing parenthesis for the method so the trigger method is on its own in the class.
Assuming you have a Rigidbody2D and have set trigger it should work.
I’d also suggest there is room for improvement in the code but rather than nit pick let’s address the main issue
Upvotes: 1
Reputation: 51
You put the void OnTriggerEnter2D (Collider2D other) int the Update. Did you write it correctly?
Upvotes: 1