简体   繁体   中英

Unity C# 2D Instantiate and manipulate a projectile as a child of player

I want to instantiate a projectile and be able to rotate it as a child of a parent player object. My player has a 2DRigidbody and box collider 2D, my projectiles also have a 2DRigidbody and box collider 2D, so when I shoot them from the player they bounce the player everywhere, but when I try to instantiate them elsewhere the player either jumps to that location or they appear away from the player, so I'd like to instantiate them as a child of the player. My code for the Player

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

// Calls Variables
public Rigidbody2D lazer;
public float speed = 1.0f;
public float hitpoints = 100;
public float lazerlevel = 1;
Transform ParentPlayer;


// Initalizes variables
void Start () {


}

// Updates once per frame
void Update () {
    var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
    transform.position += move * speed * Time.deltaTime;
    if (Input.GetButtonUp("Fire1")) {
        //Rigidbody2D instance = Instantiate(lazer, transform.position = new Vector3(0,-5,0), transform.rotation) as Rigidbody2D;
        //Vector3 fwd = transform.TransformDirection(Vector3.down);
        //instance.AddForce(fwd * 20 * lazerlevel);
        GameObject objnewObject = (GameObject)Instantiate(lazer, new Vector3(0, 0, 0), transform.rotation);
        objnewObject.transform.parent = ParentPlayer;
    }

}
}

And my code for the lazer

using UnityEngine;
using System.Collections;

public class PlayerProjectileDoom : MonoBehaviour {

void ontriggerenter() {
        Destroy(gameObject);
}

void Update () {
    Destroy(gameObject, 2F);
}
}

You never assigned ParentPlayer in Player script. And you want to assign instantiated object as the child of Player . So the possible way to achieve this is to instantiate your object first, set it as child and then set the transformations to identity because after setting it as child its transformations will become relative to parent.

For example,

void Update () {
    var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
    transform.position += move * speed * Time.deltaTime;
    if (Input.GetButtonUp("Fire1")) {
        GameObject objnewObject = Instantiate(lazer) as GameObject;
        objnewObject.transform.parent = transform;
        objnewObject.transform.localRotation = Quaternion.identity;
        objnewObject.transform.localPosition = Vector3.zero;
    }
}

Second thing is in your PlayerProjectileDoom script. You did implement OnTriggerEnter in wrong way. You should be careful when you are implementing these messages.

Third thing is you are calling Destroy in Update method, however it will work if you put it in Start . I'd recommend use Update at least as possible and absolutely not for these kind of events.

It should be like,

using UnityEngine;
using System.Collections;

public class PlayerProjectileDoom : MonoBehaviour {

    void Start(){
        Destroy(gameObject, 2F);
    }

    // For 3D colliders
    void OnTriggerEnter(Collider coll) {
            Destroy(gameObject);
    }

    // For 2D colliders
    void OnTriggerEnter2D(Collider2D coll) {
            Destroy(gameObject);
    }

    void Update () {

    }

}

So you are using 2D colliders so you should go with 2D collider message and remove the 3D one.

An extra note: OnTriggerEnter or OnTriggerEnter2D will execute only iff you set colliders as Trigger . You can see this Trigger check in Inspector by selecting that gameobject having any collider. But if it is not set to Trigger then you should use OnCollisionEnter2D(Collision2D coll)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM