- Home /
Transform an object in other
=======
Hi, I'm creating a game, and some of the mechanics I want, is a power, a staff turns into a snake, how can I do this in C #? The snake must have animations, AI.
You're going to have to be $$anonymous$$UCH more specific on your questions. Where exactly are you stuck on this problem? What have you tried? Can you be more specific on what it is you're trying to achieve?
Answer by shadowpuppet · Sep 09, 2017 at 03:02 AM
trigger it somehow and instantiate a prefab. like if the player gets close to the staff or if the player has a certain amount of points or whatever condition is required to turn the staff into a snake. The snake will be a prefab with animation and AI scripts on it. When the condition is met to make the staff change, destroy it and instantiate the snake. The code below is if the player gets close to the staff. The staff needs a trigger collider and this script.
using UnityEngine;
using System.Collections;
public class staffChange : MonoBehaviour {
public GameObject snake;//this is where you drag the snake prefab
void OnTriggerEnter(Collider other) {
if(other.tag == "player")
{
Instantiate(snake, transform.position , transform.rotation);
Destroy(gameObject);
}
}
}
for a score requirement of 50 points:
using UnityEngine;
using System.Collections;
public class staffChange : MonoBehaviour {
public GameObject snake;//this is where you drag the snake prefab
public GameObject PlayerScore;//this is where you drag the gameobject with scoring script
MonoBehaviour score;// this is the value in the scoring script of the players score
void start()
score = PlayerScore.GetComponent<scoreScript>();
}
if( scoreScript.score = 50)
{
Instantiate(snake, transform.position , transform.rotation);
Destroy(gameObject);
}
}
}
Oh wow, I created basically the same (but less comprehensive) answer before realizing someone had beat me to it while I was writing it. LOL
Answer by IsaiahKelly · Sep 09, 2017 at 03:11 AM
Just create a snake prefab with all the functions you need. Then create a script and attach it to the staff that will replace it with this new snake prefab. Something like this:
using UnityEngine;
public class StaffSwappingScript : MonoBehaviour
{
public GameObject swapObject; // drag snake prefab onto this from the editor.
// call this when you want to switch to the "swapObject" object.
public void Swap()
{
// spawn replacement object with same position and rotation.
var replacement = Instantiate(swapObject, transform.position, transform.rotation);
// might want to also make sure it has the same parent?
replacement.transform.parent = transform.parent;
// destroy the old object we're replacing.
Destroy(gameObject);
}
}
This script could be attached to the staff itself for onetime switching or the player and expanded to allow for switching back and forth.
Note: "transform" and "gameObject" are helper functions to easily access the GameObject and Transform the script is attached to. If you attach the script to something else this code will need to be modified to make it work.