0
\$\begingroup\$

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:

enter image description here

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:

enter image description here

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Did you mean to use OnTriggerEnter2D, not OnCollisionEnter2D? \$\endgroup\$
    – DMGregory
    Jun 21 at 16:41
  • 1
    \$\begingroup\$ While you can mix naming styles, it's advisable to pick a convention & apply it consistently. Specifically, since the default for classes in C# is StartingCappedCamelCase, your enemySpeed class should probably be refactored to EnemySpeed 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\$
    – Pikalek
    Jun 21 at 17:02
  • \$\begingroup\$ @DMGregory yes i did. vs code corrected it to that by accident \$\endgroup\$
    – desperrrr
    Jun 21 at 20:58
  • \$\begingroup\$ If it works now, post that as an Answer below. If it doesn't, edit your question to show your corrected code. \$\endgroup\$
    – DMGregory
    Jun 22 at 12:12
  • \$\begingroup\$ @DMGregory sorry i havent been able to get around to testing it yet. \$\endgroup\$
    – desperrrr
    Jun 22 at 15:24

1 Answer 1

0
\$\begingroup\$

Turns out I made a beginner mistake. I mistakenly used OnCollisionEnter2D instead of OnTriggerEnter2D.

\$\endgroup\$
1
  • \$\begingroup\$ Remember to come back and mark this answer as Accepted by clicking the checkmark in the top-left if it solved your problem. \$\endgroup\$
    – DMGregory
    Jun 23 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .