- Home /
Question by
SVKsuli · May 18, 2014 at 09:11 PM ·
c#javascriptconverting
problem with converting js to c#
Hi, i try join this js:
#pragma strict
var animator: Animator;
function Start () {
animator = GetComponent("Animator");
}
function Update () {
if(Input.GetKey(KeyCode.W)){
animator.SetBool("stop", false);
animator.SetBool("stop2", false);
}
else{
animator.SetBool("stop", true);
animator.SetBool("stop2", true);
}
}
to this c# script:
using System.Collections;
public class BotAiScript : MonoBehaviour {
public Transform target;
public Transform enemyTransform;
public Animator animator;
public float rotationSpeed=10f;
public float MinDist;
public float MaxDist;
public float speed;
public float maxspeed;
public float accelerate;
Vector3 upAxis = new Vector3 (0f, 0f, -1f);
void Start () {
animator = GetComponent("Animator");
}
void Update(){
//rotate to look at the player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0,-90,-90),Space.Self);//correcting the original rotation
//move towards the player
if (Vector3.Distance (enemyTransform.position, target.position) >= MinDist) {
rigidbody2D.velocity = transform.up * speed * Time.deltaTime;
animator.SetBool("stop", false);
animator.SetBool("stop2", false);
if(speed < maxspeed) {
speed += accelerate;
}
}
if(Vector3.Distance (enemyTransform.position, target.position) <= MinDist && Vector3.Distance(enemyTransform.position,target.position) >= MaxDist) {
animator.SetBool("stop", true);
animator.SetBool("stop2", true);
if(speed > 0) {
speed -= accelerate;
}
else{
speed += accelerate;
}
}
if(Vector3.Distance(enemyTransform.position,target.position) <= MaxDist) {
rigidbody2D.velocity = transform.up * speed * Time.deltaTime;
if(speed > -400) {
speed -= accelerate * 5;
}
}
}
}
but it write: (18,17): error CS0266: Cannot implicitly convert type UnityEngine.Component' to
UnityEngine.Animator'. An explicit conversion exists (are you missing a cast?)
i dont know how to translate it to c# thx.
Comment
You need to type it like so:
GetComponent<CO$$anonymous$$PONENT_YOU_WANT_TO_GET>();
Answer by KiraSensei · May 18, 2014 at 09:13 PM
Try :
void Start () {
animator = (Animator)GetComponent("Animator");
}