i have added a animator to the enemy prefab and i want to make the animation change when i hit it
in the collision ( its **** out ) please can someone help me:(
i tried to make a new var eanim : Animator.... and call it in the start ect... but wont let me drag the ememy animator into the slot
how can i fix this please.
outline what i want to do i want to coll with the enemy and have the enemy that i hit change to death animation.
var Player : GameObject;
var Gravity:float = 2;
var speed:float = 2;
var enemytrans : Transform;
var enemy: GameObject;
public var jumped = false;
var anim : Animator;
function Start () {
while (true) {
yield WaitForSeconds (Random.Range(3, 0));
enemy = Instantiate(enemytrans).gameObject;
}
anim = GetComponent(Animator);
}
function Update () {
Player.transform.position.x = -4.325;
if (jumped == false){
anim.SetFloat("hf",0.0);
}
if (Input.GetButtonDown("Fire1") && jumped==false){
fire();
jumped = true;
}
if(jumped==true){
anim.SetFloat("hf",1);
}
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("ground")){
anim.SetFloat("hf",0.0);
jumped=false;
}
*********if(coll.gameObject.CompareTag("enemy") && jumped==true){ **
fire();
jumped=true;
anim.SetTrigger("isdead"); <<<<<<<<<<<<<----- this is what i need help with ------
}
if(coll.gameObject.CompareTag("enemy") && jumped==false){
Destroy(Player);
}
}
function fire(){
Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
Player.rigidbody2D.velocity = Vector2(0,10);
}
If you do anim.SetTrigger(..) in unity, you also need to setup that inside the Animator window for the corresponding gameobject.
So select your gameobject and go to window -> animator, add a trigger parameter (in your case with the name of "isdead") - and setup the transitions from the different states. So for example I create an empty state and set that as default, and then drag between that and my animation state in order to get the transitions.
Inside the transitions you just set default -> anim state to use the "isdead" parameter under the conditions. And on anim state -> default you set exit time as the condition.
Related
I'm working on a sports game where if a player skates into the goalie crease, I want all the player positions to reset to the center of the ice and do a three second countdown before play resumes.
I have tried to hardcode the starting position for the main player in a variable called PlayerStart and I call Player.transform.position = PlayerStart. When I did this, the player didn't move so I tried to switch the object I was setting as the player. This did what I wanted, but the mouse functionality changes for some reason and when the countdown ends, the player just goes right back to the position they were in before the crease violation was called.
Other things I've tried:
transform.SetPositionAndRotation
PlayerStart = Player.transform.position (instead of hard coding the numbers in)
Here is my code:
public class Crease : MonoBehaviour
{
private float Delay = 0;
private bool CreaseViolated = false;
private GameObject Player;
public Vector3 PlayerStart;
// Start is called before the first frame update
void Start()
{
Ring = GameObject.Find("Ring");
Player = GameObject.Find("FPSController");
PlayerStart = new Vector3(29.75f, 6.03999996f, 4.42000008f);
}
// Update is called once per frame
void Update()
{
Delay -= Time.deltaTime;
if (CreaseViolated)
{
CreaseViolation();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name != "Ring"
&& other.gameObject.name != "TeamGoalie"
&& other.gameObject.name != "OpponentGoalie")
{
CreaseViolated = true;
Delay = 3;
}
}
void CreaseViolation()
{
if (Delay > 0)
{
PlayerTip.GetComponent<PickupRing>().HasRing = false;
Opponent.GetComponent<AI>().HasRing = false;
Ring.transform.parent = null;
}
else
{
text.text = " ";
if (CreaseViolated)
{
Debug.Log("Player position before: " + Player.transform.position);
Player.transform.position = PlayerStart;
Debug.Log("Player position after: " + Player.transform.position);
//Player.transform.SetPositionAndRotation(PlayerStart + new Vector3(0f, 0.800000012f, 0f), new Quaternion(0f, -0.707106829f, 0f, 0.707106829f) + new Quaternion(0f, 0f, 0f, 1f));
GameObject.Find("Countdown").GetComponent<CountdownText>().timeRemaining = 4;
CreaseViolated = false;
}
}
}
}
Here is a short YouTube video showing my code and the demo: https://www.youtube.com/watch?v=mZt_4AppBh8
this problem is all solved now thanks to an awesome person at my university helping me out! The solution was disabling the CharacterController before repositioning the player and then enabling it again after.
So this:
Player.transform.position = PlayerStart;
in the CreaseViolation function becomes
cc.enabled = false;
PlayerController.transform.position = PlayerControllerStart;
cc.enabled = true;
with cc being declared earlier as
private CharacterController cc;
and in the start function I assigned it with the value
cc = Player.GetComponent<CharacterController>();
with PlayerController being set to the FPSController.
I renamed Player to PlayerController for more clarity.
Hopefully this helps anyone having the same problem I was having!
The Above answer works perfectly but might bring some issues when doing networked changes in player transform positions.
The reason the transform change doesn't work without disabling the Character Controller is because the character controller overrides the transforms.
To Disable this and enable an Auto Synchronization between transforms GoTo:
Edit > Project Settings > Physics > (Enable Auto Sync Transforms)
I'm having a bit of an odd issue in a 2d side-scroller game, where unity is only creating my projectile clone sometimes when pressing fire, it removes 1 grenade from the inventory correctly regardless of if the grenade is cloned or not.
here is my code
void ThrowGranade()
{
if (grenandeInventory > 0)
{
GameObject grenade = Instantiate(projectile, projectileSpawnPoint.transform.position, Quaternion.identity) as GameObject;
grenandeInventory =- 1;
//am.ThrowGrenade();
}
else if (grenandeInventory <= 0)
{
grenandeInventory = 0;
}
}
and the fire button script held in the update function
if (Input.GetButtonDown("Fire1"))
{
if (grenandeInventory>0)
{
grenandeInventory -= 1;
ThrowGranade();
}
}
i add the force in the start function of the projectile itself
void Start () {
#region REFERENCES
anim = GetComponent<Animator>();
am = FindObjectOfType<AudioManager>();
rb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<PlayerScript>();
capsule = GetComponent<CapsuleCollider2D>();
#endregion
rb.AddForce(Vector2.right * player.projectileForce, ForceMode2D.Force);
}
You shouldn't be doing the check in both your throw grenade method and when you click the button before calling the method. The way you set it up right now, if you have 1 grenade, you subtract from the grenade count before calling your throwGrenade so that your throwGrenade sees you have 0 grenades. Just do this.
if (Input.GetButtonDown("Fire1"))
{
ThrowGranade();
}
Your ThrowGranade() Method already handles the logic.
How would I delete an enemy on the enemyprefab but not the enemanim animator?
#pragma strict
var enemy : GameObject;
var speed : float = 1.0;
var enemanim : Animator;
function Start() {
this.transform.position.x = 8.325;
this.transform.position.y = -1.3;
enemanim = this.GetComponent(Animator);
enemanim.SetFloat("isdead", 0);
}
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.CompareTag("distroy")) {
Destroy(this.gameObject);
}
}
function enemstruck() {
enemanim.SetFloat("isdead", 1);
}
function FixedUpdate() {
this.transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
this.rigidbody2D.velocity = Vector2(-5, 0);
}
This is my enemy prefab code i am instantiating it in my main
These 2 var:
var enemytrans : Transform;
var enemy : GameObject;
function Start () {
while (true) {
yield WaitForSeconds (Random.Range(3, 0));
enemy = Instantiate(enemytrans).gameObject;
}
I think you cannot delete GameObject and save one of its components. Every component needs to be attached to some GameObject and you cannot move them to other GameObjects either.
What you can do is to remove other components from your GameObject and you will get the same result.
You can remove a component with following script:
Destroy(gameObject.GetComponent(yourComponent));
It is possible to save the Component and Destroy the GameObject. Although I can only think of very few special cases when this would be done with logic (not out of madness).
For example, you might have different enemies, when one dies, its special power transfers over to another enemy via the Component.
So this is how I would do it, using the example above (code is C# because UnityScript lacks power):
public void die()
{
Component c = GetComponent<TheType>();
// Send component.
BroadcastMessage("setComponent", c);
// Destroy.
Destroy(gameObject);
}
Then whatever object is listening to setComponent will handle it:
public void setComponent(Component c)
{
gameObject.AddComponent<c>();
}
I have never done this before, but it came out so awesome that now I am going to add it to one of my games
i have tried everything to get the destroy to work Destroy(enemytrans) Destroy(enemy) Destroy(enemy.gameObject..... ect all abbreviations wont work.
can someone please help ???
i can coll with all objects and they do destroy but if 2 enemies are on screen i hit the one closest to me the other one will die because it was last spawned how can i destroy the one i jump on ?? mind efukd. :(
var Player : GameObject;
var Gravity:float = 2;
var speed:float = 2;
var enemytrans : Transform;
var enemy: GameObject;
public var jumped = false;
var anim : Animator;
function Start () {
while (true) {
yield WaitForSeconds (Random.Range(3, 0));
enemy = Instantiate(enemytrans).gameObject;
}
anim = GetComponent(Animator);
// Instantiate the prefab and put the gameObject into "theCube"
}
function Update () {
Player.transform.position.x = -4.325;
//the gravity function
if (jumped == false){
anim.SetFloat("hf",0.0);
}
if (Input.GetButtonDown("Fire1") && jumped==false){
fire();
jumped = true;
}
if(jumped==true){
anim.SetFloat("hf",1);
}
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("ground")){
anim.SetFloat("hf",0.0);
jumped=false;
}
if(coll.gameObject.CompareTag("enemy") && jumped==true){
fire();
jumped=true;
Destroy(enemy,1);***********************************************this line************
}
if(coll.gameObject.CompareTag("enemy") && jumped==false){
Destroy(Player);
}
}
function fire(){
Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
Player.rigidbody2D.velocity = Vector2(0,10);
}
Problem
You are destroying the last spawned GameObject, which you keep a reference to in the enemy variable.
Solution
You should destroy what you hit. Unity3D already gives you who you collided against so just use that information.
function OnCollisionEnter2D(coll: Collision2D)
{
if(coll.gameObject.CompareTag("enemy") && jumped==true)
Destroy(coll.gameObject);
}
var respawn : Transform;
var dead : boolean = false;
function OnTriggerEnter(theCollision : Collider)
{
if(theCollision.gameObject.name=="Spotlight")
{
Destroy(gameObject);
Debug.Log("Dead");
dead = true;
}
}
function Respawn()
{
if(dead == true)
{
Instantiate(respawn, Vector3(0,0,0), Quaternion.identity);
}
}
I'm having trouble getting my enemies to re-spawn, at the moment I have just the one enemy following me around. I'm trying to re-spawn his after he collides with my spotlight.
I have a prefab Enemy that I want to use to create more instances of Enemy once it gets destroyed.
I'm pretty sure my code above is destroying the prefab itself and thus cannot create any more instances but I'm not sure how to just destroy an instance of a prefab. The code above is attached to my enemy.
I am pretty sure you are destroying the gameObject with the script.
if(theCollision.gameObject.name=="Spotlight")
{
Destroy(gameObject);
Debug.Log("Dead");
dead = true;
}
You should have there Destroy(theCollision.gameObject) instead.
And destroy at the end of the code block, not beginning.
Also Respawn function should be on Update or something for it to work, also you can call Respawn on the trigger function before destroying and it should spawn a new one every time.
Do not forget to reference the prefab from you Project window not Hierarchy.
As I said in my comment, you need to keep track of the time since the enemy died, and the best place to do that is in your respawner object. Here is an example implementation of it:
Respawner.js
#pragma strict
var enemyToSpawn : Enemy;
var respawnTime : int = 10; //In seconds.
private var dead : boolean = true;
private var deadTime : int = 0; //Time the enemy died, keep at 0
private var nextRespawn : int = 0;
private function Respawn() {
var enemy : GameObject = GameObject.Instantiate(enemyToSpawn.gameObject, transform.position, transform.rotation);
enemy.GetComponent(Enemy).respawner = this;
}
function Update() {
if (Time.time >= nextRespawn && dead) { //If the time is due and the enemy is dead
Respawn(); //create a new enemy
dead = false; //and tell our script it is alive
}
}
function EnemyDied(theEnemy : Enemy) {
dead = true;
deadTime = Time.time;
nextRespawn = Time.time+respawnTime;
GameObject.Destroy(theEnemy.gameObject);
}
Enemy.js
#pragma strict
public var respawner : Respawner; // The respawner associated with this enemy
function OnTriggerEnter(collision : Collider) {
if (collision.gameObject.name.Equals("Spotlight")) {
respawner.SendMessage("EnemyDied", this); //Tell the respawner to kill us
}
}
Of course your enemy class will have anything else necessary to be an actual enemy, and your respawner could have an effect. This is just an example and shouldn't be blindly copy-pasted.