I’m currently working on a game tutorial from UnityCookie. CG Cookie | Learn Blender, Online Tutorials and Help - CG Cookie | Learn Blender, Online Tutorials and Feedback
Around 28:40 is where we add some new code that is confusing to me for convert from JS to C#.
In the “else if” statement, we add code to access another script. The script name is LandingPad.cs. Im not sure if i’ve intrepreted the JS to C# correctly there. Reason why i think this is the line “landingPad = hitInfo.gameObject.GetComponent(“LandingPad”);” is giving me this error “an explicit conversion exists (are you missing a cast )” I don’t understand what that means. Any help is much appreciated. Thank you.
using UnityEngine;
using System.Collections;
//using ParticlePlayground;
public class PlayerController : MonoBehaviour {
public ParticleSystem bottomThruster;
public ParticleSystem subBottomThruster;
public ParticleSystem leftThruster;
public ParticleSystem rightThruster;
public ParticleSystem topLeftThruster;
public ParticleSystem topRightThruster;
public GameObject[] shipExplosions;
// Use this for initialization
void Start ()
{
bottomThruster.enableEmission = false;
subBottomThruster.enableEmission = false;
leftThruster.enableEmission = false;
rightThruster.enableEmission = false;
topLeftThruster.enableEmission = false;
topRightThruster.enableEmission = false;
}
// Update is called once per frame
void Update ()
{
if(Input.GetAxis("Horizontal") > 0 ) //Moves ship to the right
{
//print("Got Right Key");
leftThruster.enableEmission = true;
rigidbody.AddForce(10,0,0);
}
if (Input.GetAxis("Horizontal") < 0) //Moves ship to the left
{
//print("Got Left Key");
rightThruster.enableEmission = true;
rigidbody.AddForce(-10, 0, 0);
}
if (Input.GetAxis("Horizontal") == 0) //when nothing is pressed do nothing
{
//print("No Horizontal Key");
leftThruster.enableEmission = false;
rightThruster.enableEmission = false;
rigidbody.AddForce(0, 0, 0);
}
if (Input.GetAxis("Vertical") > 0) //Move ship up
{
//print("Got Up Key");
bottomThruster.enableEmission = true;
subBottomThruster.enableEmission = true;
rigidbody.AddForce(0, 10, 0);
}
if (Input.GetAxis("Vertical") < 0)
{
//print("Got Down Key");
topLeftThruster.enableEmission = true;
topRightThruster.enableEmission = true;
rigidbody.AddForce(0, -10, 0);
}
if (Input.GetAxis("Vertical") == 0) //when nothing is pressed do nothing
{
//print("No Vertical Key");
bottomThruster.enableEmission = false;
subBottomThruster.enableEmission = false;
topLeftThruster.enableEmission = false;
topRightThruster.enableEmission = false;
rigidbody.AddForce(0, 0, 0);
}
}
void OnCollisionEnter(Collision hitInfo)
{
if(hitInfo.relativeVelocity.magnitude >= 2)
{
Explode();
}
else if(hitInfo.gameObject.tag == "LandingPad")
{
print("landing pad hit");
LandingPad landingPad = new LandingPad();
landingPad = hitInfo.gameObject.GetComponent("LandingPad");
landingPad.Activate();
}
}
void Explode()
{
int randomNumber;
randomNumber = Random.Range(0, shipExplosions.Length);
Instantiate(shipExplosions[randomNumber], transform.position, transform.rotation);
Destroy(gameObject);
}
}