I am new to programming and would like to know how do I create an exchange system similar to that used in the game meaning that link:
https://www.youtube.com/watch?v=11PXll2K_rg&list=FLdeibbADKB476CmvWIyd21A&index=2
I want to know how my pump recognize the position of the arrow and start moving in her direction:
I thought I'd start doing this:
code used in the object that is in motion:
public float speed;
public int direction;
void Start () {
direction = 1;
}
void Update () {
if (gameObject.transform.position == GameObject.Find("ArrowUp").transform.position){
direction = 1;
}
if (transform.position == GameObject.Find("ArrowDown").transform.position)){
direction = 2;
}
if(direction == 1){
transform.position += new Vector3(0,speed*Time.deltaTime,0);
}
if(direction == 2 ){
transform.position += new Vector3(0,-(speed*Time.deltaTime),0);
}
}}
Type: I have a bomb going object moving toward the object that contains the arrow, Vector3 their positions X and Z are the same, the position is different in Y.
Arrow in position (0,10,0)
initial position of the pump (0,0,0)
the void start with this direction = 1, then the object moves up
then when the pump reaches the position (0,10,0), I want it to recognize that this same position in the Down Arrow then wanted to pump mudaçe direction = 2, then so would move down.
noting, that the arrow rotates 90 degrees at a time interval. so then the direction of motion would be different.
question: do I want to know how to recognize that this pump in the same position of the object arrow and then give the order to change its movement;
If the "Arrows" are oriented to Y Axis (green arrow), you can use transform.up
void Update () {
Vector3 ArrowUpPosition = GameObject.Find("ArrowUp").transform.position
Vector3 ArrowDownPosition = GameObject.Find("ArrowDown").transform.position
if (gameObject.transform.position == ArrowUpPosition ){
direction = 1;
}
if (transform.position == ArrowDownPosition )){
direction = 2;
}
if(direction == 1){
transform.position += ArrowUpPosition.transform.up * speed*Time.deltaTime;
}
if(direction == 2 ){
transform.position += ArrowDownPosition.transform.up * speed*Time.deltaTime;
}
Otherwise, you can use transform.right or transform.forward ( and -transform.forward, -transform.up, -transform.right)
AVOID GAMEOBJECT.FIND IN UPDATE: Get reference in Start()
AVOID GAMEOBJECT.FIND AT : Pass gameobjects to the script as fields, if you can.
AVOID THIS ISTRUCTIONS IN UPDATE: Do they in OnTriggerStay, if gameobjects have trigger colliders.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다