-2
我已經更改了一些代碼以適合我的需要。儘管變量「dest」應該是一個局部變量(我認爲),爲什麼出現一個錯誤出現在爲dest分配一個值給dest的符號「dest'錯誤,以及我將如何解決它? (這裏的鏈接:http://noobtuts.com/unity/2d-pacman-game)意外符號CS1525變量名稱
using UnityEngine;
using System.Collections;
public class SepticSamMove : MonoBehaviour
{
public float speed = 0.4f;
Vector2 dest = Vector2.zero;
// Use this for initialization
void Start()
{
dest = transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
//move closer to Destination
Vector2 p =Vector2.MoveTowards(transform.position,dest,speed);
GetComponent<Rigidbody2D>().MovePosition (p);
//check input for movement
if ((Vector2)transform.position == dest)
{
//up
if(Input.GetKey(KeyCode.W) && valid(Vector2.up)
dest = (Vector2)transform.position+Vector2.up;
if(Input.GetKey(KeyCode.S) && vaild(-Vector2.up)
dest = (Vector2)transform.position - Vector2.up;
if(Input.GetKey(KeyCode.D) && valid(Vector2.right)
dest = (Vector2)transform.position+Vector2.right;
if(Input.GetKey(KeyCode.A) && valid(-Vector2.right)
dest =(Vector2)transform.position-Vector2.right;
}
// Animation Parameters
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
bool vaild (Vector2 dir)
{
//cast Line from 'Next to Septic Sam' to SepticSam
Vector2 pos= transform.position;
RaycastHit2D hit = Physics2D.Linecast (pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}
是你的括號和括號平衡? –