Instanciar
+2
MiguelLuiz
Henrique0102
6 participantes
Página 1 de 1
Instanciar
glr alguem me ajuda ta dando um erro quando eu entro na minha cena de jogo
NullReferenceException: Object reference not set to an instance of an object
Volante.Update () (at Assets/Script/Volante.cs:37)
acho que tenho que instanciar ele como faço isso??
isso só esta acontecendo depois que eu fiz o sistema de escolha de players do Marcos
Sistema de seleção de player
NullReferenceException: Object reference not set to an instance of an object
Volante.Update () (at Assets/Script/Volante.cs:37)
acho que tenho que instanciar ele como faço isso??
isso só esta acontecendo depois que eu fiz o sistema de escolha de players do Marcos
Sistema de seleção de player
Henrique0102- Membro
- PONTOS : 3026
REPUTAÇÃO : 3
Respeito as regras :
Re: Instanciar
Provavelmente você está tentando acessar algo que não existe, talvez você tenha esquecido de anexar um GameObject ou algo do tipo em uma variável no Inspector, ou de anexar pelo próprio script.
MiguelLuiz- Programador
- PONTOS : 3087
REPUTAÇÃO : 10
Idade : 21
Áreas de atuação : Programação C#(Intermediário), Modelagem com Blender (Iniciante - Intermediário).
Respeito as regras :
Re: Instanciar
Isso que eu quero saber como eu faço pq o meu veiculo ta salvo como prefab e eu já linkei o obj que falta na prefab mas sempre da isso não tem com linkar automáticamente quando iniciar a cena??
HeF Soft- Membro
- PONTOS : 2849
REPUTAÇÃO : 4
Respeito as regras :
Re: Instanciar
Tem sim e so fazer assim tipo
Void start
Minhavariavel recebe gameobject.findwithtag ( tagdoquejvcvaipegar)
Void start
Minhavariavel recebe gameobject.findwithtag ( tagdoquejvcvaipegar)
Phph09- Profissional
- PONTOS : 3789
REPUTAÇÃO : 240
Idade : 19
Respeito as regras :
Re: Instanciar
Tem sim e so fazer assim tipo
Void start
Minhavariavel recebe gameobject.findwithtag ( tagdoquejvcvaipegar)
Void start
Minhavariavel recebe gameobject.findwithtag ( tagdoquejvcvaipegar)
Phph09- Profissional
- PONTOS : 3789
REPUTAÇÃO : 240
Idade : 19
Respeito as regras :
Re: Instanciar
mas no caso não é um gameobject é um graphic como eu faço??Phph09 escreveu:Tem sim e so fazer assim tipo
Void start
Minhavariavel recebe gameobject.findwithtag ( tagdoquejvcvaipegar)
Henrique0102- Membro
- PONTOS : 3026
REPUTAÇÃO : 3
Respeito as regras :
Re: Instanciar
alguem pode me ajudar??
Henrique0102- Membro
- PONTOS : 3026
REPUTAÇÃO : 3
Respeito as regras :
Re: Instanciar
MinhaVariavel = GameObject.FindWithTag("Sua Tag").GetComponent < OTipoDoSeuComponente > ();
MiguelLuiz- Programador
- PONTOS : 3087
REPUTAÇÃO : 10
Idade : 21
Áreas de atuação : Programação C#(Intermediário), Modelagem com Blender (Iniciante - Intermediário).
Respeito as regras :
Re: Instanciar
Boa tarde cara! cadê o script com problema?Henrique0102 escreveu:alguem pode me ajudar??
Abraço!
Re: Instanciar
não tem script com problema eu só quero saber como eu instancio um graphic na minha variavel pela Void Start
Henrique0102- Membro
- PONTOS : 3026
REPUTAÇÃO : 3
Respeito as regras :
Re: Instanciar
Como assim nao tem Script? kkHenrique0102 escreveu:não tem script com problema eu só quero saber como eu instancio um graphic na minha variavel pela Void Start
Voce tem que ter algum script, poste esse script que voce esta utilizando, para podermos te ajudar!
Provavelmente voce tera que usar Getcomponent.
Duarte- Programador
- PONTOS : 3354
REPUTAÇÃO : 97
Idade : 24
Áreas de atuação : Programação
Desenvolvedor Android
Respeito as regras :
Re: Instanciar
- Código:
using UnityEngine;[size=14][/size]using UnityEngine.UI;[size=14][/size]using UnityEngine.EventSystems;[size=14][/size]using UnityEngine.Events;[size=14][/size]using System.Collections;[size=14][/size][size=14][/size]public class Volante : MonoBehaviour{[size=14][/size][size=14][/size] public Graphic UI_Element;[size=14][/size] RectTransform rectT;[size=14][/size] Vector2 centerPoint;[size=14][/size] float wheelAngle = 0f;[size=14][/size] float wheelPrevAngle = 0f;[size=14][/size] bool wheelBeingHeld = false;[size=14][/size] public WheelCollider RodaFrenteDir, RodaFrenteEsq;[size=14][/size][size=14][/size] void Start(){[size=14][/size] rectT = UI_Element.rectTransform;[size=14][/size] InitEventsSystem();[size=14][/size] UpdateRect();[size=14][/size] }[size=14][/size] void FixedUpdate(){[size=14][/size] RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] }[size=14][/size] void Update(){[size=14][/size] Debug.Log (wheelAngle);[size=14][/size] if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){[size=14][/size] float deltaAngle = 200.0f * Time.deltaTime;[size=14][/size] if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )[size=14][/size] wheelAngle = 0f;[size=14][/size] else if( wheelAngle > 0f )[size=14][/size] wheelAngle -= deltaAngle;[size=14][/size] else[size=14][/size] wheelAngle += deltaAngle;[size=14][/size] }[size=14][/size] rectT.localEulerAngles = Vector3.back * wheelAngle;[size=14][/size] }[size=14][/size] void InitEventsSystem(){[size=14][/size] EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();[size=14][/size] if (events == null) {[size=14][/size] events = UI_Element.gameObject.AddComponent<EventTrigger> ();[size=14][/size] }[size=14][/size] if (events.triggers == null) {[size=14][/size] events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();[size=14][/size] }[size=14][/size] EventTrigger.Entry entry = new EventTrigger.Entry();[size=14][/size] EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();[size=14][/size] UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerDown;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( DragEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.Drag;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( ReleaseEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerUp;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] }[size=14][/size] void UpdateRect(){[size=14][/size] Vector3[] corners = new Vector3[4];[size=14][/size] rectT.GetWorldCorners( corners );[size=14][/size] for( int i = 0; i < 4; i++ ){[size=14][/size] corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );[size=14][/size] }[size=14][/size] Vector3 bottomLeft = corners[0];[size=14][/size] Vector3 topRight = corners[2];[size=14][/size] float width = topRight.x - bottomLeft.x;[size=14][/size] float height = topRight.y - bottomLeft.y;[size=14][/size] Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );[size=14][/size] centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );[size=14][/size] }[size=14][/size] public void PressEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] wheelBeingHeld = true;[size=14][/size] wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] }[size=14][/size] public void DragEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){[size=14][/size] if (pointerPos.x > centerPoint.x) {[size=14][/size] wheelAngle += wheelNewAngle - wheelPrevAngle;[size=14][/size] } else {[size=14][/size] wheelAngle -= wheelNewAngle - wheelPrevAngle;[size=14][/size] }[size=14][/size] }[size=14][/size] wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );[size=14][/size] wheelPrevAngle = wheelNewAngle;[size=14][/size] }[size=14][/size] public void ReleaseEvent( BaseEventData eventData ){[size=14][/size] DragEvent( eventData );[size=14][/size] wheelBeingHeld = false;[size=14][/size] }[size=14][/size]}
o script é esse eu só quero linkar automaticamente aquele "UI_Element"
HeF Soft- Membro
- PONTOS : 2849
REPUTAÇÃO : 4
Respeito as regras :
Re: Instanciar
- Código:
using UnityEngine;[size=14][/size]using UnityEngine.UI;[size=14][/size]using UnityEngine.EventSystems;[size=14][/size]using UnityEngine.Events;[size=14][/size]using System.Collections;[size=14][/size][size=14][/size]public class Volante : MonoBehaviour{[size=14][/size][size=14][/size] public Graphic UI_Element;[size=14][/size] RectTransform rectT;[size=14][/size] Vector2 centerPoint;[size=14][/size] float wheelAngle = 0f;[size=14][/size] float wheelPrevAngle = 0f;[size=14][/size] bool wheelBeingHeld = false;[size=14][/size] public WheelCollider RodaFrenteDir, RodaFrenteEsq;[size=14][/size][size=14][/size] void Start(){[size=14][/size] rectT = UI_Element.rectTransform;[size=14][/size] InitEventsSystem();[size=14][/size] UpdateRect();[size=14][/size] }[size=14][/size] void FixedUpdate(){[size=14][/size] RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] }[size=14][/size] void Update(){[size=14][/size] Debug.Log (wheelAngle);[size=14][/size] if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){[size=14][/size] float deltaAngle = 200.0f * Time.deltaTime;[size=14][/size] if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )[size=14][/size] wheelAngle = 0f;[size=14][/size] else if( wheelAngle > 0f )[size=14][/size] wheelAngle -= deltaAngle;[size=14][/size] else[size=14][/size] wheelAngle += deltaAngle;[size=14][/size] }[size=14][/size] rectT.localEulerAngles = Vector3.back * wheelAngle;[size=14][/size] }[size=14][/size] void InitEventsSystem(){[size=14][/size] EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();[size=14][/size] if (events == null) {[size=14][/size] events = UI_Element.gameObject.AddComponent<EventTrigger> ();[size=14][/size] }[size=14][/size] if (events.triggers == null) {[size=14][/size] events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();[size=14][/size] }[size=14][/size] EventTrigger.Entry entry = new EventTrigger.Entry();[size=14][/size] EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();[size=14][/size] UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerDown;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( DragEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.Drag;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( ReleaseEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerUp;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] }[size=14][/size] void UpdateRect(){[size=14][/size] Vector3[] corners = new Vector3[4];[size=14][/size] rectT.GetWorldCorners( corners );[size=14][/size] for( int i = 0; i < 4; i++ ){[size=14][/size] corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );[size=14][/size] }[size=14][/size] Vector3 bottomLeft = corners[0];[size=14][/size] Vector3 topRight = corners[2];[size=14][/size] float width = topRight.x - bottomLeft.x;[size=14][/size] float height = topRight.y - bottomLeft.y;[size=14][/size] Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );[size=14][/size] centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );[size=14][/size] }[size=14][/size] public void PressEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] wheelBeingHeld = true;[size=14][/size] wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] }[size=14][/size] public void DragEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){[size=14][/size] if (pointerPos.x > centerPoint.x) {[size=14][/size] wheelAngle += wheelNewAngle - wheelPrevAngle;[size=14][/size] } else {[size=14][/size] wheelAngle -= wheelNewAngle - wheelPrevAngle;[size=14][/size] }[size=14][/size] }[size=14][/size] wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );[size=14][/size] wheelPrevAngle = wheelNewAngle;[size=14][/size] }[size=14][/size] public void ReleaseEvent( BaseEventData eventData ){[size=14][/size] DragEvent( eventData );[size=14][/size] wheelBeingHeld = false;[size=14][/size] }[size=14][/size]}
HeF Soft- Membro
- PONTOS : 2849
REPUTAÇÃO : 4
Respeito as regras :
Re: Instanciar
se copiou os script e colou de forma errada, copie todo o conteúdo do script, e use o botão "script" do editor do fórum e clique com o botão direito do mouse e escolha "colar sem formatação".HeF Soft escreveu:
- Código:
using UnityEngine;[size=14][/size]using UnityEngine.UI;[size=14][/size]using UnityEngine.EventSystems;[size=14][/size]using UnityEngine.Events;[size=14][/size]using System.Collections;[size=14][/size][size=14][/size]public class Volante : MonoBehaviour{[size=14][/size][size=14][/size] public Graphic UI_Element;[size=14][/size] RectTransform rectT;[size=14][/size] Vector2 centerPoint;[size=14][/size] float wheelAngle = 0f;[size=14][/size] float wheelPrevAngle = 0f;[size=14][/size] bool wheelBeingHeld = false;[size=14][/size] public WheelCollider RodaFrenteDir, RodaFrenteEsq;[size=14][/size][size=14][/size] void Start(){[size=14][/size] rectT = UI_Element.rectTransform;[size=14][/size] InitEventsSystem();[size=14][/size] UpdateRect();[size=14][/size] }[size=14][/size] void FixedUpdate(){[size=14][/size] RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);[size=14][/size] }[size=14][/size] void Update(){[size=14][/size] Debug.Log (wheelAngle);[size=14][/size] if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){[size=14][/size] float deltaAngle = 200.0f * Time.deltaTime;[size=14][/size] if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )[size=14][/size] wheelAngle = 0f;[size=14][/size] else if( wheelAngle > 0f )[size=14][/size] wheelAngle -= deltaAngle;[size=14][/size] else[size=14][/size] wheelAngle += deltaAngle;[size=14][/size] }[size=14][/size] rectT.localEulerAngles = Vector3.back * wheelAngle;[size=14][/size] }[size=14][/size] void InitEventsSystem(){[size=14][/size] EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();[size=14][/size] if (events == null) {[size=14][/size] events = UI_Element.gameObject.AddComponent<EventTrigger> ();[size=14][/size] }[size=14][/size] if (events.triggers == null) {[size=14][/size] events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();[size=14][/size] }[size=14][/size] EventTrigger.Entry entry = new EventTrigger.Entry();[size=14][/size] EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();[size=14][/size] UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerDown;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( DragEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.Drag;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] entry = new EventTrigger.Entry();[size=14][/size] callback = new EventTrigger.TriggerEvent();[size=14][/size] functionCall = new UnityAction<BaseEventData>( ReleaseEvent );[size=14][/size] callback.AddListener( functionCall );[size=14][/size] entry.eventID = EventTriggerType.PointerUp;[size=14][/size] entry.callback = callback;[size=14][/size] events.triggers.Add( entry );[size=14][/size] }[size=14][/size] void UpdateRect(){[size=14][/size] Vector3[] corners = new Vector3[4];[size=14][/size] rectT.GetWorldCorners( corners );[size=14][/size] for( int i = 0; i < 4; i++ ){[size=14][/size] corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );[size=14][/size] }[size=14][/size] Vector3 bottomLeft = corners[0];[size=14][/size] Vector3 topRight = corners[2];[size=14][/size] float width = topRight.x - bottomLeft.x;[size=14][/size] float height = topRight.y - bottomLeft.y;[size=14][/size] Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );[size=14][/size] centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );[size=14][/size] }[size=14][/size] public void PressEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] wheelBeingHeld = true;[size=14][/size] wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] }[size=14][/size] public void DragEvent( BaseEventData eventData ){[size=14][/size] Vector2 pointerPos = ( (PointerEventData) eventData ).position;[size=14][/size] float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );[size=14][/size] if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){[size=14][/size] if (pointerPos.x > centerPoint.x) {[size=14][/size] wheelAngle += wheelNewAngle - wheelPrevAngle;[size=14][/size] } else {[size=14][/size] wheelAngle -= wheelNewAngle - wheelPrevAngle;[size=14][/size] }[size=14][/size] }[size=14][/size] wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );[size=14][/size] wheelPrevAngle = wheelNewAngle;[size=14][/size] }[size=14][/size] public void ReleaseEvent( BaseEventData eventData ){[size=14][/size] DragEvent( eventData );[size=14][/size] wheelBeingHeld = false;[size=14][/size] }[size=14][/size]}
Abraço!
Re: Instanciar
- Código:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections;
public class Volante : MonoBehaviour{
public Graphic UI_Element;
RectTransform rectT;
Vector2 centerPoint;
float wheelAngle = 0f;
float wheelPrevAngle = 0f;
bool wheelBeingHeld = false;
public WheelCollider RodaFrenteDir, RodaFrenteEsq;
void Start(){
rectT = UI_Element.rectTransform;
InitEventsSystem();
UpdateRect();
}
void FixedUpdate(){
RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
}
void Update(){
Debug.Log (wheelAngle);
if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){
float deltaAngle = 200.0f * Time.deltaTime;
if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )
wheelAngle = 0f;
else if( wheelAngle > 0f )
wheelAngle -= deltaAngle;
else
wheelAngle += deltaAngle;
}
rectT.localEulerAngles = Vector3.back * wheelAngle;
}
void InitEventsSystem(){
EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();
if (events == null) {
events = UI_Element.gameObject.AddComponent<EventTrigger> ();
}
if (events.triggers == null) {
events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();
UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerDown;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( DragEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.Drag;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( ReleaseEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerUp;
entry.callback = callback;
events.triggers.Add( entry );
}
void UpdateRect(){
Vector3[] corners = new Vector3[4];
rectT.GetWorldCorners( corners );
for( int i = 0; i < 4; i++ ){
corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );
}
Vector3 bottomLeft = corners[0];
Vector3 topRight = corners[2];
float width = topRight.x - bottomLeft.x;
float height = topRight.y - bottomLeft.y;
Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );
centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );
}
public void PressEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
wheelBeingHeld = true;
wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
}
public void DragEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){
if (pointerPos.x > centerPoint.x) {
wheelAngle += wheelNewAngle - wheelPrevAngle;
} else {
wheelAngle -= wheelNewAngle - wheelPrevAngle;
}
}
wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );
wheelPrevAngle = wheelNewAngle;
}
public void ReleaseEvent( BaseEventData eventData ){
DragEvent( eventData );
wheelBeingHeld = false;
}
}
HeF Soft- Membro
- PONTOS : 2849
REPUTAÇÃO : 4
Respeito as regras :
Re: Instanciar
HeF Soft escreveu:
- Código:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections;
public class Volante : MonoBehaviour{
public Graphic UI_Element;
RectTransform rectT;
Vector2 centerPoint;
float wheelAngle = 0f;
float wheelPrevAngle = 0f;
bool wheelBeingHeld = false;
public WheelCollider RodaFrenteDir, RodaFrenteEsq;
void Start(){
rectT = UI_Element.rectTransform;
InitEventsSystem();
UpdateRect();
}
void FixedUpdate(){
RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
}
void Update(){
Debug.Log (wheelAngle);
if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){
float deltaAngle = 200.0f * Time.deltaTime;
if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )
wheelAngle = 0f;
else if( wheelAngle > 0f )
wheelAngle -= deltaAngle;
else
wheelAngle += deltaAngle;
}
rectT.localEulerAngles = Vector3.back * wheelAngle;
}
void InitEventsSystem(){
EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();
if (events == null) {
events = UI_Element.gameObject.AddComponent<EventTrigger> ();
}
if (events.triggers == null) {
events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();
UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerDown;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( DragEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.Drag;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( ReleaseEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerUp;
entry.callback = callback;
events.triggers.Add( entry );
}
void UpdateRect(){
Vector3[] corners = new Vector3[4];
rectT.GetWorldCorners( corners );
for( int i = 0; i < 4; i++ ){
corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );
}
Vector3 bottomLeft = corners[0];
Vector3 topRight = corners[2];
float width = topRight.x - bottomLeft.x;
float height = topRight.y - bottomLeft.y;
Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );
centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );
}
public void PressEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
wheelBeingHeld = true;
wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
}
public void DragEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){
if (pointerPos.x > centerPoint.x) {
wheelAngle += wheelNewAngle - wheelPrevAngle;
} else {
wheelAngle -= wheelNewAngle - wheelPrevAngle;
}
}
wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );
wheelPrevAngle = wheelNewAngle;
}
public void ReleaseEvent( BaseEventData eventData ){
DragEvent( eventData );
wheelBeingHeld = false;
}
}
Tente substituir seu script por este, nao sei se esta correto pq fiz pelo cel!
Coloque a tag do Objeto no lugar indicado, comentei o Script nas linhas que adicionei!
- Código:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections;
public class Volante : MonoBehaviour{
private GameObject Objeto;
public Graphic UI_Element;
RectTransform rectT;
Vector2 centerPoint;
float wheelAngle = 0f;
float wheelPrevAngle = 0f;
bool wheelBeingHeld = false;
public WheelCollider RodaFrenteDir, RodaFrenteEsq;
void Start(){
Objeto = GameObject.FindGameObjectWithTag ("tagdoObjeto");//Aqui voce coloca a Tag do Objeto que tem o Graphic
UI_Element = Objeto.GetComponent<Graphic> ();
rectT = UI_Element.rectTransform;
InitEventsSystem();
UpdateRect();
}
void FixedUpdate(){
RodaFrenteDir.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
RodaFrenteEsq.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);
}
void Update(){
Debug.Log (wheelAngle);
if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) ){
float deltaAngle = 200.0f * Time.deltaTime;
if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )
wheelAngle = 0f;
else if( wheelAngle > 0f )
wheelAngle -= deltaAngle;
else
wheelAngle += deltaAngle;
}
rectT.localEulerAngles = Vector3.back * wheelAngle;
}
void InitEventsSystem(){
EventTrigger events = UI_Element.gameObject.GetComponent<EventTrigger>();
if (events == null) {
events = UI_Element.gameObject.AddComponent<EventTrigger> ();
}
if (events.triggers == null) {
events.triggers = new System.Collections.Generic.List<EventTrigger.Entry> ();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent();
UnityAction<BaseEventData> functionCall = new UnityAction<BaseEventData>( PressEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerDown;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( DragEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.Drag;
entry.callback = callback;
events.triggers.Add( entry );
entry = new EventTrigger.Entry();
callback = new EventTrigger.TriggerEvent();
functionCall = new UnityAction<BaseEventData>( ReleaseEvent );
callback.AddListener( functionCall );
entry.eventID = EventTriggerType.PointerUp;
entry.callback = callback;
events.triggers.Add( entry );
}
void UpdateRect(){
Vector3[] corners = new Vector3[4];
rectT.GetWorldCorners( corners );
for( int i = 0; i < 4; i++ ){
corners[i] = RectTransformUtility.WorldToScreenPoint( null, corners[i] );
}
Vector3 bottomLeft = corners[0];
Vector3 topRight = corners[2];
float width = topRight.x - bottomLeft.x;
float height = topRight.y - bottomLeft.y;
Rect _rect = new Rect( bottomLeft.x, topRight.y, width, height );
centerPoint = new Vector2( _rect.x + _rect.width * 0.5f, _rect.y - _rect.height * 0.5f );
}
public void PressEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
wheelBeingHeld = true;
wheelPrevAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
}
public void DragEvent( BaseEventData eventData ){
Vector2 pointerPos = ( (PointerEventData) eventData ).position;
float wheelNewAngle = Vector2.Angle( Vector2.up, pointerPos - centerPoint );
if( Vector2.Distance( pointerPos, centerPoint ) > 20f ){
if (pointerPos.x > centerPoint.x) {
wheelAngle += wheelNewAngle - wheelPrevAngle;
} else {
wheelAngle -= wheelNewAngle - wheelPrevAngle;
}
}
wheelAngle = Mathf.Clamp( wheelAngle, -200, 200 );
wheelPrevAngle = wheelNewAngle;
}
public void ReleaseEvent( BaseEventData eventData ){
DragEvent( eventData );
wheelBeingHeld = false;
}
}
Duarte- Programador
- PONTOS : 3354
REPUTAÇÃO : 97
Idade : 24
Áreas de atuação : Programação
Desenvolvedor Android
Respeito as regras :
Re: Instanciar
e como eu faço pra qnd ele for criado ele ser filho de um canvas??
Henrique0102- Membro
- PONTOS : 3026
REPUTAÇÃO : 3
Respeito as regras :
Re: Instanciar
Public GameObject Canvas;
Void Start(){
transform.Parent = Canvas;
}
assim ou algo do tipo
Void Start(){
transform.Parent = Canvas;
}
assim ou algo do tipo
Phph09- Profissional
- PONTOS : 3789
REPUTAÇÃO : 240
Idade : 19
Respeito as regras :
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos