D
D
DimaD0106d2021-08-23 15:31:24
Unity
DimaD0106d, 2021-08-23 15:31:24

How to do +x + 5 in unity?

here is the game:

61239545a25bf524383886.png

When you press the space button, the cube moves to the position of the white object, well, it moves directly into it, but I want it to be x + 5 more to the side more so that it is next to it and not in it, here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class block : MonoBehaviour
{
    public float speed;
    public float jump;
    public Transform pos;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        transform.Translate(Vector3.up * speed * Time.deltaTime );

        if (Input.GetKey(KeyCode.S))
        transform.Translate(Vector3.down * speed * Time.deltaTime );

        if (Input.GetKey(KeyCode.A))
        transform.Translate(Vector3.left * speed * Time.deltaTime );

        if (Input.GetKey(KeyCode.D))
        transform.Translate(Vector3.right * speed * Time.deltaTime );

        if (Input.GetKey(KeyCode.Space))
        transform.position = pos.transform.position, x + 5;



    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PRoGRamm_InG, 2021-08-31
@PRoGRamm_InG

transform.position = pos.transform.position;
transform.position.x += 5;
but i'm not sure it will work

N
NoNameDeveloper, 2021-09-10
@NoNameDeveloper

transform.position = new Vector3(pos.transform.position.x + 5, pos.transform.position.y, pos.transform.position.z);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question