Newbie here, I have a player script that interacts with objects just fine and I made an enemy script for the same object and it gets completely ignored:
Below there are 4 scripts. The Player.cs
is player movement, Enemy.cs
is enemy movement, Speed.cs
gives the player a speed boost when they touch the green object named Speed
and enemySpeed.cs
is TRYING to give the enemy a speed boost when it touches the green object named enemySpeed
. Those last two are both attached to the green game object.
Player.cs :
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 10f;
void Update()
{
Vector3 position = transform.position;
if (Input.GetKey("w")){
position.y += moveSpeed * Time.deltaTime;
}
if (Input.GetKey("s")){
position.y -= moveSpeed * Time.deltaTime;
}
if (Input.GetKey("d")){
position.x += moveSpeed * Time.deltaTime;
}
if (Input.GetKey("a")){
position.x -= moveSpeed * Time.deltaTime;
}
transform.position=position;
}
}
Enemy.cs :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform player;
private Rigidbody2D rb;
private Vector2 movement;
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement=direction;
}
private void FixedUpdate(){
moveCharacter(movement);
}
void moveCharacter(Vector2 direction){
rb.MovePosition((Vector2)transform.position + (direction*moveSpeed*Time.deltaTime));
}
}
Speed.cs :
using UnityEngine;
public class Speed : MonoBehaviour
{
public float speedIncrease = 5f;
private void OnTriggerEnter2D(Collider2D collision){
if (collision.CompareTag("Player")){
GameObject player = collision.gameObject;
Player playerScript = player.GetComponent<Player>();
if (playerScript){
playerScript.moveSpeed += speedIncrease;
Destroy(gameObject);
}
}
}
}
enemySpeed.cs :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemySpeed : MonoBehaviour
{
public float speedIncrease=5f;
private void OnCollisionEnter2D(Collider2D collision){
if (collision.CompareTag("Enemy")){
GameObject enemy=collision.gameObject;
Enemy enemyScript=collision.gameObject.GetComponent<Enemy>();
if(enemyScript){
enemyScript.moveSpeed+=speedIncrease;
Destroy(gameObject);
}
}
}
}
I'm also getting this error that happened when I finished making enemySpeed.cs
:
I have is trigger checked on for the enemy rigidbody2d because when it touches the player with is trigger off, then it will kick the player and the player will start gliding across the map like its on ice or in space.
I come from the Blender Stack Exchange where we provide all resources we feel are relevant to the question so let me know if I left anything out or included too much.
OnTriggerEnter2D
, notOnCollisionEnter2D
? \$\endgroup\$StartingCappedCamelCase
, yourenemySpeed
class should probably be refactored toEnemySpeed
instead. This won't solve your current problem, but it makes it easier to avoid annoying syntax errors. Similarly, using a consistent indentation & bracing style makes it easier to read & subsequently to debug. \$\endgroup\$