本文介绍了名为`distanceball'的Alocal变量不能在此范围内声明,因为它会给`distanceball'赋予不同的含义,'distanceball'已在'child'范围内用于表示其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用UnityEngine;
使用System;
使用System.Collections;
公共类Player_Script:MonoBehaviour {
//玩家名称
公共字符串名称;
public TypePlayer type = TypePlayer.DEFENDER;
公共浮动速度= 1.0f;
公共浮动强= 1.0f;
public float Control = 1.0f;
private const float STAMINA_DIVIDER = 64.0f;
private const float STAMINA_MIN = 0.5f;
private const float STAMINA_MAX = 1.0f;
public enum TypePlayer {
DEFENDER,
MIDDLER,
ATTACKER
};
public Vector3 actualVelocityPlayer;
private Vector3 oldVelocityPlayer;
public Sphere sphere;
私人GameObject []玩家;
private GameObject [] oponents;
public Vector3 initialPosition;
private float inputSteer;
private const float initialDisplacement = 20.0f;
public Transform goalPosition;
public Transform headTransform;
[HideInInspector]
public bool temporallyUnselectable = true;
[HideInInspector]
public float timeToBeSelectable = 1.0f;
public float maxDistanceFromPosition = 20.0f;
public enum Player_State {
RESTING,
GO_ORIGIN,
控制,
通过,
拍卖,
MOVE_AUTOMATIC,
ONE_STEP_BACK,
STOLE_BALL,
OPONENT_ATTACK,
PICK_BALL,
CHANGE_DIRECTION,
SAQUE_BANDA,
SAQUE_CORNER
};
public Player_State state;
公共材料material_bound;
private float timeToRemove = 3.0f;
private float timeToPass = 1.0f;
// squeleton层次结构中玩家的手牌
public Transform hand_bone;
public InGameState_Script inGame;
public Texture barTexture;
public Texture barStaminaTexture;
private int barPosition = 0;
私人Quaternion initialRotation;
public float stamina = 64.0f;
void Awake(){
animation.Stop();
state = Player_State.MOVE_AUTOMATIC;
}
void Start(){
//随机改变玩家大小以获得不同的外观
/ / transform.localScale = new Vector3(UnityEngine.Random.Range(0.9F,1.1F),UnityEngine.Random.Range(0.9F,1.1F),UnityEngine.Random.Range(0.9F,1.1F));
players = GameObject.FindGameObjectsWithTag(PlayerTeam1);
oponents = GameObject.FindGameObjectsWithTag(OponentTeam);
if(gameObject.tag ==PlayerTeam1)
initialPosition = new Vector3(transform.position.x,transform.position.y,transform.position.z + initialDisplacement);
if(gameObject.tag ==OponentTeam)
initialPosition = new Vector3(transform.position.x,transform.position.y,transform.position.z-initialDisplacement);
//设置动画速度以适应完美动作
animation [retroceder_bucle]。speed = 1.5f;
animation [arranque]。speed = 1.0f;
animation [arranque_balon]。speed = 1.0f;
animation [corriendo]。speed = 1.2f;
animation [correr_balon]。speed = 1.0f;
animation [pase]。speed = 1.8f;
animation [reposo]。speed = 1.0f;
animation [cambio_sentido]。speed = 1.3f;
动画[entrada]。速度= 1.2f;
// para el movimiento de la cabeza de los jugadores
animation.Play(reposo);
initialRotation = transform.rotation * headTransform.rotation;
}
//控制实际玩家
void Case_Controlling(){
if(sphere.inputPlayer == gameObject){
if(sphere.fVertical!= 0.0f || sphere.fHorizontal!= 0.0f){
oldVelocityPlayer = actualVelocityPlayer;
Vector3 right = inGame.transform.right;
Vector3 forward = inGame.transform.forward;
right * = sphere.fHorizontal;
forward * = sphere.fVertical;
Vector3 target = transform.position + right + forward;
target.y = transform.position.y;
float speedForAnimation = 5.0f;
//如果是Ball的所有者....
if(sphere.owner == gameObject){
if(animation.IsPlaying(reposo) )){
animation.Play(arranque_balon);
speedForAnimation = 1.0f;
}
if(animation.IsPlaying(arranque_balon)== false)
animation.Play(correr_balon);
}
else {
if(animation.IsPlaying(reposo)){
animation.Play(arranque);
speedForAnimation = 1.0f;
}
if(animation.IsPlaying(arranque)== false)
animation.Play(corriendo);
}
transform.LookAt(target);
float staminaTemp = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
actualVelocityPlayer = transform.forward * speedForAnimation * Time.deltaTime * staminaTemp * Speed;
transform.position + = actualVelocityPlayer;
//如果获得玩家更改动画的根本不同方向...
float dotp = Vector3.Dot(oldVelocityPlayer.normalized,actualVelocityPlayer.normalized);
if(dotp< 0.0f&& sphere.owner == gameObject){
animation.Play(cambio_sentido);
state = Player_State.CHANGE_DIRECTION;
transform.forward = -transform.forward;
sphere.owner = null;
gameObject.GetComponent< CapsuleCollider>()。enabled = false;
sphere.gameObject.GetComponent< Rigidbody>()。AddForce(-transform.forward.x * 1500.0f,-transform.forward.y * 1500.0f,-transform.forward.z * 1500.0f);
}
}其他{
animation.Play(reposo);
}
//传递
if(sphere.bPassButton&& sphere.owner == gameObject){
animation.Play( PASE);
timeToBeSelectable = 2.0f;
state = Player_State.PASSING;
sphere.pressiPhonePassButton = false;
}
//拍摄
if(sphere.bShootButtonFinished&& sphere.owner == gameObject){
animation.Play(tiro);
timeToBeSelectable = 2.0f;
state = Player_State.SHOOTING;
sphere.pressiPhoneShootButton = false;
sphere.bShootButtonFinished = false;
}
} else {
state = Player_State.MOVE_AUTOMATIC;
}
}
bool NoOneInFront(GameObject [] team_players){
foreach(GameObject go in team_players){
Vector3 relativePos = transform.InverseTransformPoint(go.transform.position);
if(relativePos.z> 0.0f)
返回true;
}
返回false;
}
// NPC控制
void Case_Oponent_Attack(){
actualVelocityPlayer = transform.forward * 5.0f * Time.deltaTime;
animation.Play(correr_balon);
Vector3 RelativeWaypointPosition = transform.InverseTransformPoint(goalPosition.position);
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
transform.Rotate(0,inputSteer * 10.0f,0);
float staminaTemp = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
transform.position + = transform.forward * 4.0f * Time.deltaTime * staminaTemp * Speed;
timeToPass - = Time.deltaTime;
if(timeToPass< 0.0f&& NoOneInFront(oponents)){
timeToPass = UnityEngine.Random.Range(1.0f,5.0f);
state = Player_State.PASSING;
animation.Play(pase);
timeToBeSelectable = 1.0f;
temporallyUnselectable = true;
}
浮动距离=(goalPosition.position - transform.position).magnitude;
Vector3 relative = transform.InverseTransformPoint(goalPosition.position);
if(距离< 20.0f&& relative.z> 0){
state = Player_State.SHOOTING;
animation.Play(tiro);
timeToBeSelectable = 1.0f;
temporallyUnselectable = true;
}
}
void LateUpdate(){
Vector3 relativePos = transform.InverseTransformPoint( sphere.gameObject.transform.position);
if(relativePos.z> 0.0f){
Quaternion lookRotation = Quaternion.LookRotation(sphere.transform.position + new Vector3(0,1.0) f,0) - headTransform.position);
headTransform.rotation = lookRotation * initialRotation;
headTransform.eulerAngles = new Vector3(headTransform.eulerAngles.x,headTransform.eulerAngles.y,-90.0f);
}
}
void Update(){
stamina + = 2.0f * Time.deltaTime;
stamina = Mathf.Clamp(stamina,1,64);
switch(state){
case Player_State.SAQUE_BANDA:
break;
case Player_State.SAQUE_CORNER:
break;
case Player_State.CHANGE_DIRECTION:
if(!animation.IsPlaying(cambio_sentido)){
gameObject.GetComponent< CapsuleCollider>()。enabled = true ;
transform.forward = -transform.forward;
animation.Play(reposo);
state = Player_State.CONTROLLING;
}
休息;
case Player_State.CONTROLLING:
if(gameObject.tag ==PlayerTeam1)
Case_Controlling();
休息;
case Player_State.OPONENT_ATTACK:
Case_Oponent_Attack();
休息;
case Player_State.PICK_BALL:
transform.position + = transform.forward * Time.deltaTime * 5.0f;
if(animation.IsPlaying(entrada)== false){
if(gameObject.tag ==OponentTeam)
state = Player_State。 OPONENT_ATTACK;
else
state = Player_State.MOVE_AUTOMATIC;
}
休息;
case Player_State.SHOOTING:
if(animation.IsPlaying(tiro)== false)
state = Player_State.MOVE_AUTOMATIC;
if(animation [tiro]。normalizedTime> 0.2f&& sphere.owner == this.gameObject){
state = Player_State.MOVE_AUTOMATIC;
sphere.owner = null;
if(gameObject.tag ==PlayerTeam1){
sphere.gameObject.GetComponent< Rigidbody>()。velocity = new Vector3(transform.forward.x * 30.0f,5.0f,transform。 forward.z * 30.0f);
barPosition = 0;
}
else {
float valueRndY = UnityEngine.Random.Range(4.0f,10.0f);
sphere.gameObject.GetComponent< Rigidbody>()。velocity = new Vector3(transform.forward.x * 30.0f,valueRndY,transform.forward.z * 30.0f);
}
}
休息;
case Player_State.PASSING:
if(animation.IsPlaying(pase)== false)
state = Player_State.MOVE_AUTOMATIC;
if(animation [pase]。normalizedTime> 0.3f&& sphere.owner == this.gameObject){
sphere.owner = null;
GameObject bestCandidatePlayer = null;
float bestCandidateCoord = 1000.0f;
if(gameObject.tag ==PlayerTeam1){
foreach(GameObject go in players){
if( go!= gameObject){
Vector3 relativePos = transform.InverseTransformPoint(new Vector3(go.transform.position.x,go.transform.position.y,go.transform.position.z));
浮动幅度= relativePos.magnitude;
float direction = Mathf.Abs(relativePos.x);
if(relativePos.z> 0.0f&& direction< 5.0f&& magnitude< 15.0f&&(direction< bestCandidateCoord)){
bestCandidateCoord = direction;
bestCandidatePlayer = go;
}
}
}
}其他{
foreach(GameObject go in oponents){
if(go!= gameObject){
Vector3 relativePos = transform.InverseTransformPoint(new Vector3(go.transform.position.x,go.transform.position.y,go.transform。 position.z));
浮动幅度= relativePos.magnitude;
float direction = Mathf.Abs(relativePos.x);
if(relativePos.z> 0.0f&& direction< 15.0f&&(magnitude + direction< bestCandidateCoord)){
bestCandidateCoord = magnitude + direction;
bestCandidatePlayer = go;
}
}
}
}
if(bestCandidateCoord!= 1000.0f){
sphere.inputPlayer = bestCandidatePlayer;
Vector3 directionBall =(bestCandidatePlayer.transform.position - transform.position).normalized;
float distanceBall =(bestCandidatePlayer.transform.position - transform.position).magnitude * 1.4f;
distanceBall = Mathf.Clamp(distanceBall,15.0f,40.0f);
sphere.gameObject.GetComponent< Rigidbody>()。velocity = new Vector3(directionBall.x * distanceBall,distanceBall / 5.0f,directionBall.z * distanceBall);
} else {
//如果没有找到候选人只是向前扔球......
sphere.gameObject.GetComponent< Rigidbody>()。velocity = transform。进* 20.0f;
}
}
break;
case Player_State.GO_ORIGIN:
animation.Play(corriendo);
//现在我们只是找到汽车变换中航点的相对位置,
//我们可以确定航点的左右两边。
Vector3 RelativeWaypointPosition = transform.InverseTransformPoint(new Vector3(
initialPosition.x,
initialPosition.y,
initialPosition.z));
//通过将水平位置除以幅度,我们得到转弯角度的十进制百分比,我们可以用来驱动车轮
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
if(inputSteer == 0&& RelativeWaypointPosition.z< 0)
inputSteer = 10.0f;
transform.Rotate(0,inputSteer * 10.0f,0);
float staminaTemp = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
transform.position + = transform.forward * 3.0f * Time.deltaTime * staminaTemp * Speed; transform.position + = transform.forward * 3.0f * Time.deltaTime;
if(RelativeWaypointPosition.magnitude< 1.0f){
state = Player_State.MOVE_AUTOMATIC;
}
休息;
case Player_State.MOVE_AUTOMATIC:
timeToRemove + = Time.deltaTime;
浮点距离=(transform.position - initialPosition).magnitude;
//知道球和球员的距离
float distanceBall =(transform.position - sphere.transform.position).magnitude;
//如果我们超出了我们玩家的界限,我们会回到初始位置
if(距离> maxDistanceFromPosition){
Vector3 ball = sphere。 transform.position;
Vector3 direction =(ball - transform.position).normalized;
Vector3 RelativeWaypointP = transform.InverseTransformPoint(new Vector3(
initialPosition.x,
initialPosition.y,
initialPosition.z));
inputSteer = RelativeWaypointP.x / RelativeWaypointP.magnitude;
if(inputSteer == 0&& RelativeWaypointP.z< 0)
inputSteer = 10.0f;
transform.Rotate(0,inputSteer * 20.0f,0);
animation.Play(corriendo);
float staminaTemp2 = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
transform.position + = transform.forward * 5.5f * Time.deltaTime * staminaTemp2 * Speed;
} //如果不是我们去Ball ...
else {
Vector3 ball = sphere.transform.position;
Vector3 direction =(ball - transform.position).normalized;
Vector3 posFinal = initialPosition +(direction * maxDistanceFromPosition);
Vector3 RelativeWaypointP = new Vector3(posFinal.x,posFinal.y,posFinal.z);
//转到球位....
if(distanceBall> 5.0f){
RelativeWaypointP = transform.InverseTransformPoint(new Vector3(
posFinal。 x,
posFinal.y,
posFinal.z));
}否则if(distanceBall< 5.0f&& distanceBall> 2.0f){
//如果我们小于5米的球我们停止
RelativeWaypointP = transform.InverseTransformPoint(new Vector3(
transform.position.x,
transform.position.y,
transform.position.z));
//如果我们离得太近,我们回去看特殊动画
}否则如果(distanceBall< 2.0f){
animation.Play(retroceder_bucle );
state = Player_State.ONE_STEP_BACK;
休息;
}
inputSteer = RelativeWaypointP.x / RelativeWaypointP.magnitude;
if(inputSteer == 0&& RelativeWaypointP.z< 0)
inputSteer = 10.0f;
if(inputSteer> 0.0f)
transform.Rotate(0,inputSteer * 20.0f,0);
//这只是检查玩家的位置是否足够接近。
if(RelativeWaypointP.magnitude< 1.5f){
transform.LookAt(new Vector3(sphere.GetComponent< Transform>()。position.x,transform.position.y,sphere .GetComponent< Transform>()。position.z));
animation.Play(reposo);
timeToRemove = 0.0f;
} else {
if(timeToRemove> 1.0f){
animation.Play(corriendo);
staminaTemp = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
transform.position + = transform.forward * 5.5f * Time.deltaTime * staminaTemp * Speed;
}
}
}
break;
case Player_State.RESTING:
transform.LookAt(new Vector3(sphere.GetComponent< Transform>()。position.x,transform .position.y,sphere.GetComponent< Transform>()。position.z));
animation.Play(reposo);
休息;
case Player_State.ONE_STEP_BACK:
if(animation.IsPlaying(retroceder_bucle)== false)
state = Player_State.MOVE_AUTOMATIC;
transform.position - = transform.forward * Time.deltaTime * 4.0f;
休息;
case Player_State.STOLE_BALL:
Vector3 relPos = transform.InverseTransformPoint(sphere.transform.position);
inputSteer = relPos.x / relPos.magnitude;
transform.Rotate(0,inputSteer * 20.0f,0);
animation.Play(corriendo);
float staminaTemp3 = Mathf.Clamp((stamina / STAMINA_DIVIDER),STAMINA_MIN,STAMINA_MAX);
transform.position + = transform.forward * 4.5f * Time.deltaTime * staminaTemp3 * Speed;
休息;
};
//在传球或射门球员进入不可选状态后的某些时间
timeToBeSelectable - = Time.deltaTime;
if(timeToBeSelectable< 0.0f)
temporallyUnselectable = false;
else
temporallyUnselectable = true;
}
void OnCollisionStay(碰撞coll){
if(coll.collider。 transform.gameObject.tag ==Ball&&!gameObject.GetComponent< Player_Script>()。temporallyUnselectable){
inGame.lastTouched = gameObject;
//我们是不是把球甩到了对手?
if(sphere.owner&&(sphere.owner.tag!= gameObject.tag)){
if((gameObject.tag = =PlayerTeam1&& sphere.bPassButton)|| gameObject.tag ==OponentTeam){
// comparacion entre fuerza de ambos jugadores ...
float myStrong =(this.Strong + UnityEngine.Random.Range(0.0f,1.0f));
float oponentStrong =(sphere.owner.GetComponent< Player_Script>()。Strong + UnityEngine.Random.Range(0.0f,1.0f))/ * + 10.0f * /;
if(myStrong< oponentStrong){
Debug.Log(regate ganado por jugador de POSESION+ myStrong +vs+ oponentStrong);
this.gameObject.animation.Play(retroceder_bucle);
this.gameObject.GetComponent< Player_Script>()。state = Player_State.ONE_STEP_BACK;
this.gameObject.GetComponent< Player_Script>()。timeToBeSelectable = 0.5f;
this.gameObject.GetComponent< Player_Script>()。temporallyUnselectable = true;
返回;
} else {
Debug.Log(regate ganado por jugador de ENTRADA+ myStrong +vs+ oponentStrong);
} sphere.owner.GetComponent< Player_Script>()。state = Player_State.ONE_STEP_BACK;
sphere.owner.GetComponent< Player_Script>()。timeToBeSelectable = 2.0f;
sphere.owner.GetComponent< Player_Script>()。temporallyUnselectable = true;
sphere.owner = gameObject;
sphere.owner.GetComponent< Player_Script>()。state = Player_State.PICK_BALL;
sphere.owner.animation.Play(entrada);
返回;
}其他{
返回;
}
}
Vector3 relativePos = transform.InverseTransformPoint(sphere.gameObject.transform.position);
//如果碰撞位于底部
,则只将球粘给球员if(relativePos.y< 0.35f){
coll.rigidbody.rotation = Quaternion.identity;
GameObject ball = coll.collider.transform.gameObject;
ball.GetComponent< Sphere>()。owner = gameObject;
if(gameObject.tag ==OponentTeam){
state = Player_Script.Player_State.OPONENT_ATTACK;
}
}
}
}
void OnGUI(){
if(sphere.timeShootButtonPressed> 0.0f&& sphere.inputPlayer == this.gameObject){
Vector3 posBar = Camera.main.WorldToScreenPoint(headTransform.position + new Vector3(0,0.8f,0));
GUI.DrawTexture(new Rect(posBar.x-30,(Screen.height-posBar.y),barPosition,10),barTexture);
barPosition =(int)(sphere.timeShootButtonPressed * 128.0f);
if(barPosition> = 63)
barPosition = 63;
}
if(sphere.owner == this.gameObject){
Vector3 posBar = Camera。 main.WorldToScreenPoint(headTransform.position + new Vector3(0,1.0f,0));
GUI.DrawTexture(new Rect(posBar.x-30,(Screen.height-posBar.y),(int)stamina,10),barStaminaTexture);
stamina - = 1.5f * Time.deltaTime;
}
}
}
我尝试了什么:
尝试更改变量名称篮球,距离球
解决方案
using UnityEngine; using System; using System.Collections; public class Player_Script : MonoBehaviour { // player name public string Name; public TypePlayer type = TypePlayer.DEFENDER; public float Speed = 1.0f; public float Strong = 1.0f; public float Control = 1.0f; private const float STAMINA_DIVIDER = 64.0f; private const float STAMINA_MIN = 0.5f; private const float STAMINA_MAX = 1.0f; public enum TypePlayer { DEFENDER, MIDDLER, ATTACKER }; public Vector3 actualVelocityPlayer; private Vector3 oldVelocityPlayer; public Sphere sphere; private GameObject[] players; private GameObject[] oponents; public Vector3 initialPosition; private float inputSteer; private const float initialDisplacement = 20.0f; public Transform goalPosition; public Transform headTransform; [HideInInspector] public bool temporallyUnselectable = true; [HideInInspector] public float timeToBeSelectable = 1.0f; public float maxDistanceFromPosition = 20.0f; public enum Player_State { RESTING, GO_ORIGIN, CONTROLLING, PASSING, SHOOTING, MOVE_AUTOMATIC, ONE_STEP_BACK, STOLE_BALL, OPONENT_ATTACK, PICK_BALL, CHANGE_DIRECTION, SAQUE_BANDA, SAQUE_CORNER }; public Player_State state; public Material material_bound; private float timeToRemove = 3.0f; private float timeToPass = 1.0f; // hand of player in squeleton hierarchy public Transform hand_bone; public InGameState_Script inGame; public Texture barTexture; public Texture barStaminaTexture; private int barPosition=0; private Quaternion initialRotation; public float stamina = 64.0f; void Awake (){ animation.Stop(); state = Player_State.MOVE_AUTOMATIC; } void Start (){ // change size of player randomly to get different look // transform.localScale = new Vector3( UnityEngine.Random.Range(0.9F, 1.1F), UnityEngine.Random.Range(0.9F, 1.1F), UnityEngine.Random.Range(0.9F, 1.1F) ); players = GameObject.FindGameObjectsWithTag("PlayerTeam1"); oponents = GameObject.FindGameObjectsWithTag("OponentTeam"); if ( gameObject.tag == "PlayerTeam1" ) initialPosition = new Vector3( transform.position.x, transform.position.y, transform.position.z+initialDisplacement ); if ( gameObject.tag == "OponentTeam" ) initialPosition = new Vector3( transform.position.x, transform.position.y, transform.position.z-initialDisplacement ); // set animations speed to fit perfect movements animation["retroceder_bucle"].speed = 1.5f; animation["arranque"].speed = 1.0f; animation["arranque_balon"].speed = 1.0f; animation["corriendo"].speed = 1.2f; animation["correr_balon"].speed = 1.0f; animation["pase"].speed = 1.8f; animation["reposo"].speed = 1.0f; animation["cambio_sentido"].speed = 1.3f; animation["entrada"].speed = 1.2f; // para el movimiento de la cabeza de los jugadores animation.Play("reposo"); initialRotation = transform.rotation * headTransform.rotation; } // control of actual player void Case_Controlling() { if ( sphere.inputPlayer == gameObject ) { if ( sphere.fVertical != 0.0f || sphere.fHorizontal != 0.0f ) { oldVelocityPlayer = actualVelocityPlayer; Vector3 right = inGame.transform.right; Vector3 forward = inGame.transform.forward; right *= sphere.fHorizontal; forward *= sphere.fVertical; Vector3 target = transform.position + right + forward; target.y = transform.position.y; float speedForAnimation = 5.0f; // if is owner of Ball.... if ( sphere.owner == gameObject ) { if ( animation.IsPlaying("reposo") ) { animation.Play("arranque_balon"); speedForAnimation = 1.0f; } if ( animation.IsPlaying("arranque_balon") == false ) animation.Play("correr_balon"); } else { if ( animation.IsPlaying("reposo") ) { animation.Play("arranque"); speedForAnimation = 1.0f; } if ( animation.IsPlaying("arranque") == false ) animation.Play("corriendo"); } transform.LookAt( target ); float staminaTemp = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN ,STAMINA_MAX ); actualVelocityPlayer = transform.forward*speedForAnimation*Time.deltaTime*staminaTemp*Speed; transform.position += actualVelocityPlayer; // if get a radical diferent direction of player change animation... float dotp = Vector3.Dot( oldVelocityPlayer.normalized, actualVelocityPlayer.normalized ); if ( dotp < 0.0f && sphere.owner == gameObject ) { animation.Play("cambio_sentido"); state = Player_State.CHANGE_DIRECTION; transform.forward = -transform.forward; sphere.owner = null; gameObject.GetComponent<CapsuleCollider>().enabled = false; sphere.gameObject.GetComponent<Rigidbody>().AddForce( -transform.forward.x*1500.0f, -transform.forward.y*1500.0f, -transform.forward.z*1500.0f ); } } else { animation.Play("reposo"); } // pass if ( sphere.bPassButton && sphere.owner == gameObject ) { animation.Play("pase"); timeToBeSelectable = 2.0f; state = Player_State.PASSING; sphere.pressiPhonePassButton = false; } // shoot if ( sphere.bShootButtonFinished && sphere.owner == gameObject ) { animation.Play("tiro"); timeToBeSelectable = 2.0f; state = Player_State.SHOOTING; sphere.pressiPhoneShootButton = false; sphere.bShootButtonFinished = false; } } else { state = Player_State.MOVE_AUTOMATIC; } } bool NoOneInFront( GameObject[] team_players ) { foreach( GameObject go in team_players ) { Vector3 relativePos = transform.InverseTransformPoint( go.transform.position ); if ( relativePos.z > 0.0f ) return true; } return false; } // NPC control void Case_Oponent_Attack() { actualVelocityPlayer = transform.forward*5.0f*Time.deltaTime; animation.Play("correr_balon"); Vector3 RelativeWaypointPosition = transform.InverseTransformPoint(goalPosition.position); inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude; transform.Rotate(0, inputSteer*10.0f , 0); float staminaTemp = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN ,STAMINA_MAX ); transform.position += transform.forward*4.0f*Time.deltaTime*staminaTemp*Speed; timeToPass -= Time.deltaTime; if ( timeToPass < 0.0f && NoOneInFront( oponents ) ) { timeToPass = UnityEngine.Random.Range( 1.0f, 5.0f); state = Player_State.PASSING; animation.Play("pase"); timeToBeSelectable = 1.0f; temporallyUnselectable = true; } float distance = (goalPosition.position - transform.position).magnitude; Vector3 relative = transform.InverseTransformPoint(goalPosition.position); if ( distance < 20.0f && relative.z > 0 ) { state = Player_State.SHOOTING; animation.Play("tiro"); timeToBeSelectable = 1.0f; temporallyUnselectable = true; } } void LateUpdate() { Vector3 relativePos = transform.InverseTransformPoint( sphere.gameObject.transform.position ); if ( relativePos.z > 0.0f ) { Quaternion lookRotation = Quaternion.LookRotation (sphere.transform.position + new Vector3(0, 1.0f,0) - headTransform.position); headTransform.rotation = lookRotation * initialRotation ; headTransform.eulerAngles = new Vector3( headTransform.eulerAngles.x, headTransform.eulerAngles.y, -90.0f); } } void Update() { stamina += 2.0f * Time.deltaTime; stamina = Mathf.Clamp(stamina, 1, 64); switch ( state ) { case Player_State.SAQUE_BANDA: break; case Player_State.SAQUE_CORNER: break; case Player_State.CHANGE_DIRECTION: if ( !animation.IsPlaying("cambio_sentido")) { gameObject.GetComponent<CapsuleCollider>().enabled = true; transform.forward = -transform.forward; animation.Play("reposo"); state = Player_State.CONTROLLING; } break; case Player_State.CONTROLLING: if ( gameObject.tag == "PlayerTeam1" ) Case_Controlling(); break; case Player_State.OPONENT_ATTACK: Case_Oponent_Attack(); break; case Player_State.PICK_BALL: transform.position += transform.forward * Time.deltaTime * 5.0f; if (animation.IsPlaying("entrada") == false) { if ( gameObject.tag == "OponentTeam" ) state = Player_State.OPONENT_ATTACK; else state = Player_State.MOVE_AUTOMATIC; } break; case Player_State.SHOOTING: if (animation.IsPlaying("tiro") == false) state = Player_State.MOVE_AUTOMATIC; if (animation["tiro"].normalizedTime > 0.2f && sphere.owner == this.gameObject) { state = Player_State.MOVE_AUTOMATIC; sphere.owner = null; if ( gameObject.tag == "PlayerTeam1" ) { sphere.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(transform.forward.x*30.0f, 5.0f, transform.forward.z*30.0f ); barPosition = 0; } else { float valueRndY = UnityEngine.Random.Range( 4.0f, 10.0f ); sphere.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(transform.forward.x*30.0f, valueRndY, transform.forward.z*30.0f ); } } break; case Player_State.PASSING: if (animation.IsPlaying("pase") == false) state = Player_State.MOVE_AUTOMATIC; if (animation["pase"].normalizedTime > 0.3f && sphere.owner == this.gameObject) { sphere.owner = null; GameObject bestCandidatePlayer = null; float bestCandidateCoord = 1000.0f; if ( gameObject.tag == "PlayerTeam1" ) { foreach ( GameObject go in players ) { if ( go != gameObject ) { Vector3 relativePos = transform.InverseTransformPoint( new Vector3( go.transform.position.x, go.transform.position.y, go.transform.position.z ) ); float magnitude = relativePos.magnitude; float direction = Mathf.Abs(relativePos.x); if ( relativePos.z > 0.0f && direction < 5.0f && magnitude < 15.0f && (direction < bestCandidateCoord) ) { bestCandidateCoord = direction; bestCandidatePlayer = go; } } } } else { foreach ( GameObject go in oponents ) { if ( go != gameObject ) { Vector3 relativePos = transform.InverseTransformPoint( new Vector3( go.transform.position.x, go.transform.position.y, go.transform.position.z ) ); float magnitude = relativePos.magnitude; float direction = Mathf.Abs(relativePos.x); if ( relativePos.z > 0.0f && direction < 15.0f && (magnitude+direction < bestCandidateCoord) ) { bestCandidateCoord = magnitude+direction; bestCandidatePlayer = go; } } } } if ( bestCandidateCoord != 1000.0f ) { sphere.inputPlayer = bestCandidatePlayer; Vector3 directionBall = (bestCandidatePlayer.transform.position - transform.position).normalized; float distanceBall = (bestCandidatePlayer.transform.position - transform.position).magnitude*1.4f; distanceBall = Mathf.Clamp( distanceBall, 15.0f, 40.0f ); sphere.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(directionBall.x*distanceBall, distanceBall/5.0f, directionBall.z*distanceBall ); } else { // if not found a candidate just throw the ball forward.... sphere.gameObject.GetComponent<Rigidbody>().velocity = transform.forward*20.0f; } } break; case Player_State.GO_ORIGIN: animation.Play("corriendo"); // now we just find the relative position of the waypoint from the car transform, // that way we can determine how far to the left and right the waypoint is. Vector3 RelativeWaypointPosition = transform.InverseTransformPoint(new Vector3( initialPosition.x, initialPosition.y, initialPosition.z ) ); // by dividing the horizontal position by the magnitude, we get a decimal percentage of the turn angle that we can use to drive the wheels inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude; if ( inputSteer == 0 && RelativeWaypointPosition.z < 0 ) inputSteer = 10.0f; transform.Rotate(0, inputSteer*10.0f , 0); float staminaTemp = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN ,STAMINA_MAX ); transform.position += transform.forward*3.0f*Time.deltaTime*staminaTemp*Speed; transform.position += transform.forward*3.0f*Time.deltaTime; if ( RelativeWaypointPosition.magnitude < 1.0f ) { state = Player_State.MOVE_AUTOMATIC; } break; case Player_State.MOVE_AUTOMATIC: timeToRemove += Time.deltaTime; float distance = (transform.position - initialPosition).magnitude; // know the distance of ball and player float distanceBall = (transform.position - sphere.transform.position).magnitude; // if we get out of bounds of our player we come back to initial position if ( distance > maxDistanceFromPosition ) { Vector3 ball = sphere.transform.position; Vector3 direction = (ball - transform.position).normalized; Vector3 RelativeWaypointP = transform.InverseTransformPoint(new Vector3( initialPosition.x, initialPosition.y, initialPosition.z ) ); inputSteer = RelativeWaypointP.x / RelativeWaypointP.magnitude; if ( inputSteer == 0 && RelativeWaypointP.z < 0 ) inputSteer = 10.0f; transform.Rotate(0, inputSteer*20.0f , 0); animation.Play("corriendo"); float staminaTemp2 = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN ,STAMINA_MAX ); transform.position += transform.forward*5.5f*Time.deltaTime*staminaTemp2*Speed; } // if not we go to Ball... else { Vector3 ball = sphere.transform.position; Vector3 direction = (ball - transform.position).normalized; Vector3 posFinal = initialPosition + ( direction * maxDistanceFromPosition ); Vector3 RelativeWaypointP = new Vector3(posFinal.x, posFinal.y, posFinal.z); // go to Ball position.... if ( distanceBall > 5.0f ) { RelativeWaypointP = transform.InverseTransformPoint(new Vector3( posFinal.x, posFinal.y, posFinal.z ) ); } else if ( distanceBall < 5.0f && distanceBall > 2.0f ) { // if we are less than 5 meters of ball we stop RelativeWaypointP = transform.InverseTransformPoint(new Vector3( transform.position.x, transform.position.y, transform.position.z ) ); // if we are too close we go back with special animation } else if ( distanceBall < 2.0f ) { animation.Play("retroceder_bucle"); state = Player_State.ONE_STEP_BACK; break; } inputSteer = RelativeWaypointP.x / RelativeWaypointP.magnitude; if ( inputSteer == 0 && RelativeWaypointP.z < 0 ) inputSteer = 10.0f; if ( inputSteer > 0.0f ) transform.Rotate(0, inputSteer*20.0f , 0); // this just checks if the player's position is near enough. if ( RelativeWaypointP.magnitude < 1.5f ) { transform.LookAt( new Vector3( sphere.GetComponent<Transform>().position.x, transform.position.y ,sphere.GetComponent<Transform>().position.z) ); animation.Play("reposo"); timeToRemove = 0.0f; } else { if ( timeToRemove > 1.0f ) { animation.Play("corriendo"); staminaTemp = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN , STAMINA_MAX ); transform.position += transform.forward*5.5f*Time.deltaTime*staminaTemp*Speed; } } } break; case Player_State.RESTING: transform.LookAt( new Vector3( sphere.GetComponent<Transform>().position.x, transform.position.y ,sphere.GetComponent<Transform>().position.z) ); animation.Play("reposo"); break; case Player_State.ONE_STEP_BACK: if (animation.IsPlaying("retroceder_bucle") == false) state = Player_State.MOVE_AUTOMATIC; transform.position -= transform.forward*Time.deltaTime*4.0f; break; case Player_State.STOLE_BALL: Vector3 relPos = transform.InverseTransformPoint( sphere.transform.position ); inputSteer = relPos.x / relPos.magnitude; transform.Rotate(0, inputSteer*20.0f , 0); animation.Play("corriendo"); float staminaTemp3 = Mathf.Clamp ((stamina/STAMINA_DIVIDER), STAMINA_MIN ,STAMINA_MAX ); transform.position += transform.forward*4.5f*Time.deltaTime*staminaTemp3*Speed; break; }; // after pass or shoot player get in a Unselectable state some little time timeToBeSelectable -= Time.deltaTime; if ( timeToBeSelectable < 0.0f ) temporallyUnselectable = false; else temporallyUnselectable = true; } void OnCollisionStay( Collision coll ) { if ( coll.collider.transform.gameObject.tag == "Ball" && !gameObject.GetComponent<Player_Script>().temporallyUnselectable ) { inGame.lastTouched = gameObject; // are we stoling ball to oponent? if ( sphere.owner && (sphere.owner.tag != gameObject.tag) ) { if ( (gameObject.tag == "PlayerTeam1" && sphere.bPassButton) || gameObject.tag == "OponentTeam" ) { // comparacion entre fuerza de ambos jugadores... float myStrong = (this.Strong + UnityEngine.Random.Range(0.0f, 1.0f)); float oponentStrong = (sphere.owner.GetComponent<Player_Script>().Strong + UnityEngine.Random.Range(0.0f, 1.0f))/* + 10.0f*/; if ( myStrong < oponentStrong ) { Debug.Log("regate ganado por jugador de POSESION " + myStrong + " vs " + oponentStrong ); this.gameObject.animation.Play("retroceder_bucle"); this.gameObject.GetComponent<Player_Script>().state = Player_State.ONE_STEP_BACK; this.gameObject.GetComponent<Player_Script>().timeToBeSelectable = 0.5f; this.gameObject.GetComponent<Player_Script>().temporallyUnselectable = true; return; } else { Debug.Log("regate ganado por jugador de ENTRADA " + myStrong + " vs " + oponentStrong); } sphere.owner.GetComponent<Player_Script>().state = Player_State.ONE_STEP_BACK; sphere.owner.GetComponent<Player_Script>().timeToBeSelectable = 2.0f; sphere.owner.GetComponent<Player_Script>().temporallyUnselectable = true; sphere.owner = gameObject; sphere.owner.GetComponent<Player_Script>().state = Player_State.PICK_BALL; sphere.owner.animation.Play("entrada"); return; } else { return; } } Vector3 relativePos = transform.InverseTransformPoint( sphere.gameObject.transform.position ); // only "glue" the ball to player if the collision is at bottom if ( relativePos.y < 0.35f ) { coll.rigidbody.rotation = Quaternion.identity; GameObject ball = coll.collider.transform.gameObject; ball.GetComponent<Sphere>().owner = gameObject; if ( gameObject.tag == "OponentTeam" ) { state = Player_Script.Player_State.OPONENT_ATTACK; } } } } void OnGUI() { if ( sphere.timeShootButtonPressed > 0.0f && sphere.inputPlayer == this.gameObject) { Vector3 posBar = Camera.main.WorldToScreenPoint( headTransform.position + new Vector3(0,0.8f,0) ); GUI.DrawTexture( new Rect( posBar.x-30, (Screen.height-posBar.y), barPosition, 10 ), barTexture ); barPosition = (int)(sphere.timeShootButtonPressed * 128.0f); if ( barPosition >= 63 ) barPosition = 63; } if ( sphere.owner == this.gameObject ) { Vector3 posBar = Camera.main.WorldToScreenPoint( headTransform.position + new Vector3(0,1.0f,0) ); GUI.DrawTexture( new Rect( posBar.x-30, (Screen.height-posBar.y), (int)stamina, 10 ), barStaminaTexture ); stamina -= 1.5f * Time.deltaTime; } } }
What I have tried:
try to change variable name basketball , and distance ball
解决方案
这篇关于名为`distanceball'的Alocal变量不能在此范围内声明,因为它会给`distanceball'赋予不同的含义,'distanceball'已在'child'范围内用于表示其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!