Answer the question
In order to leave comments, you need to log in
How to do +x + 5 in unity?
here is the game:
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
transform.position = pos.transform.position;
transform.position.x += 5;
but i'm not sure it will work
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 questionAsk a Question
731 491 924 answers to any question