The name `explosion' does not exist in the current context
之前都是使用js编写脚本,今天想使用C#把之前的代码重新整理一下,在写的过程中发现有莫名的错误,不知道是我不知道Unity3D中的C#语法,这些代码在编程的过程中都是Unity 3D提供的API 。刚刚又遇到一个错误提示The name `explosion' does not exist in the current context 。当我把explosion变量放到Update()中就不再报错,但是 我就不能再给爆炸效果赋值了 ,请高手指教!! 能发个完整点的代码嘛 谢谢 解决了,是我把变量的类型弄错了 不知道怎么的,中午代码还正常运行,现在竟然那个什么“没有上下文的错”,Assets/scripts/FishToLeftController.cs(40,38): error CS0103: The name `originPosition' does not exist in the current context下面是源代码:using UnityEngine;
using System.Collections;
public class FishToLeftController : MonoBehaviour {
public Transform playerTransform;//小鱼的位置
private int times=0;//记录击打RedFish的次数
private Vector3 originPosition;//获得RedFish的初始值位置
private bool isFromRedFish=true;//判断来自RedFish的方向
// Use this for initialization
void Start () {
Vector3 originPosition=transform.position;
}
// Update is called once per frame
void Update () {
float angle=Mathf.Atan((playerTransform.position.y-transform.position.y)/(playerTransform.position.x-transform.position.x));
Vector3 myVector31=new Vector3(-0.8f*Time.deltaTime,-0.8f*Mathf.Abs(Mathf.Tan(angle))*Time.deltaTime,0.0f);//RedFish在第一象限
Vector3 myVector34= new Vector3(-0.8f*Time.deltaTime,0.8f*Mathf.Abs(Mathf.Tan(angle))*Time.deltaTime,0.0f);//RedFish在第四象限
//Debug.Log("angle"+angle+"myVector3 "+myVector31+"ATan "+Mathf.Atan(45f)+"Tan "+Mathf.Tan(45f)+"Mathf.Deg2Rad "+Mathf.Deg2Rad*180f+"Mathf.Atan2 "+Mathf.Atan2(1,1)+"Mathf.Rad2Deg "+Mathf.Rad2Deg*3.1415926f);
myVector31.Normalize();
if(transform.position.y>playerTransform.position.y)
{
transform.Translate(0.01f*myVector31);
//Debug.Log("ifmyVector32 "+myVector31+"playerTransform.position"+playerTransform.position);
}
myVector34.Normalize();
if(transform.position.y<playerTransform.position.y)
{
transform.Translate(0.01f*myVector34);
}
if(transform.position.x<=-3.0f)
{
transform.position=originPosition;
Debug.Log("chaogu-3.5");
}
}
void OnTriggerEnter(Collider other)
{
times++;
if(times>=2)
{
Debug.Log("times "+times);
transform.position=originPosition;//这里没有销毁RedFish 只是把它的位置初始化
times=0;
}
if(other.transform.tag=="projectile")
{
Destroy(other.gameObject);
}
}
}
很不错 好帖就是要顶 说的非常好 很好哦 LZ真是人才 很不错
页:
[1]