Tuesday, June 21, 2022
HomeGame Developmentunity - Participant is ready to work together with objects however enemy...

unity – Participant is ready to work together with objects however enemy is not capable of


Beginner right here, I’ve a participant script that interacts with objects simply wonderful and I made an enemy script for a similar object and it will get fully ignored:

enter image description here

Under there are 4 scripts. The Participant.cs is participant motion, Enemy.cs is enemy motion, Pace.cs provides the participant a velocity enhance once they contact the inexperienced object named Pace and enemySpeed.cs is TRYING to present the enemy a velocity enhance when it touches the inexperienced object named enemySpeed. These final two are each connected to the inexperienced recreation object.

Participant.cs :

utilizing System.Collections.Generic;
utilizing UnityEngine;

public class Participant : MonoBehaviour
{
    public float moveSpeed = 10f;
    void Replace()
    {
        Vector3 place = remodel.place;
        if (Enter.GetKey("w")){
            place.y += moveSpeed * Time.deltaTime;
        }
        if (Enter.GetKey("s")){
            place.y -= moveSpeed * Time.deltaTime;
        }
        if (Enter.GetKey("d")){
            place.x += moveSpeed * Time.deltaTime;

        }
        if (Enter.GetKey("a")){
            place.x -= moveSpeed * Time.deltaTime;

        }
        remodel.place=place;
    }
}

Enemy.cs :

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class Enemy : MonoBehaviour
{
    public Rework participant;
    personal Rigidbody2D rb;
    personal Vector2 motion;
    public float moveSpeed = 5f;
    // Begin known as earlier than the primary body replace
    void Begin()
    {
        rb = this.GetComponent<Rigidbody2D>();
    }

    // Replace known as as soon as per body
    void Replace()
    {
        Vector3 route = participant.place - remodel.place;
        float angle = Mathf.Atan2(route.y, route.x)*Mathf.Rad2Deg;
        rb.rotation = angle;
        route.Normalize();
        motion=route;
    }
    personal void FixedUpdate(){
        moveCharacter(motion);
    }
    void moveCharacter(Vector2 route){
        rb.MovePosition((Vector2)remodel.place + (route*moveSpeed*Time.deltaTime));
    }
}

Pace.cs :

utilizing UnityEngine;

public class Pace : MonoBehaviour
{
    public float speedIncrease = 5f;
    personal void OnTriggerEnter2D(Collider2D collision){
        if (collision.CompareTag("Participant")){
            GameObject participant = collision.gameObject;
            Participant playerScript = participant.GetComponent<Participant>();
            if (playerScript){
                playerScript.moveSpeed += speedIncrease;
                Destroy(gameObject);
            }
        }
    }
}

enemySpeed.cs :

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class enemySpeed : MonoBehaviour
{
    public float speedIncrease=5f;
    personal 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 am additionally getting this error that occurred once I completed making enemySpeed.cs:

enter image description here

I’ve is set off checked on for the enemy rigidbody2d as a result of when it touches the participant with is spark off, then it’s going to kick the participant and the participant will begin gliding throughout the map like its on ice or in area.

I come from the Blender Stack Change the place we offer all sources we really feel are related to the query so let me know if I left something out or included an excessive amount of.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments