Change of direction in relation to Arrow

Marcio Vinicius

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;

Giulio Pierucci

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)

For performance!!! :

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to change direction of CALayer animation

분류에서Dev

Change the color of the arrow near the menuitem in c#

분류에서Dev

Keyboard shortcut for input language change works in only one direction

분류에서Dev

why negative value in rotateX in css doesnt change the direction of rotation

분류에서Dev

How to change the style of the arrow in a select box using css

분류에서Dev

How change keyboard navigation with down and next arrow key?

분류에서Dev

In vim, how do I make the left and right arrow keys change line?

분류에서Dev

Bullet firing in direction of rotation

분류에서Dev

jquery .animation() height direction

분류에서Dev

Navigate with arrow keys

분류에서Dev

Arrow popup dialog in Qt

분류에서Dev

Drawing an arrow with an outline

분류에서Dev

scala implicit convertion and arrow

분류에서Dev

Recursive SQL relation query

분류에서Dev

Laravel Querying Relation

분류에서Dev

OneToMany relation in datatable

분류에서Dev

ActiveRecord Relation with scope

분류에서Dev

Querying a relation in Laravel

분류에서Dev

Complex Relation in Mongoose

분류에서Dev

Direction of Estimate coefficient in a Log Regression

분류에서Dev

z direction of a matrix in a GLSL shader

분류에서Dev

disable opposit direction in range slider

분류에서Dev

How to rotate mapview to moving direction

분류에서Dev

Vyatta Firewall structure and Interface Direction

분류에서Dev

Transparent text background in an arrow in Visio

분류에서Dev

Laravel search in has many relation

분류에서Dev

JTextArea row, columns and character relation

분류에서Dev

recurrence relation dependent inversly on n

분류에서Dev

Yii relation between createURL and urlManager