Hello,
I wish I had help or guidance to perform an action.
I have in my scene 5 cubes and a sphere, I wish that when I click on a cube at random that the sphere is positioned exactly the same place as the selected cube.
Thank you in advance for your help.
You can use Physics.Raycast for this:
RaycastHit hit = new RaycastHit();
public GameObject sphere;
void Update() {
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) {
if (hit.transform.gameObject.name == "Cube") {
sphere.transform.position = hit.transform.position;
}
}
}
}
Hello and welcome to unity community.
Firstly, Check out the unity official tutorials here :
You should get yourself familiar with C#, as it is the programming language used in Unity (although there are 2 others as aswell)
Secondly, to answer your question at hand. You just need to use the position property of the transform.
using UnityEngine;
using System.Collections;
public class CubesAndStuff: MonoBehaviour
{
Ray ray;
RaycastHit hit;
Transform object;
//Put this on your sphere
void Update()
{
//Be sure to have a main camera that is tagged "MainCamera" for this example to work.
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonDown(0))
{
transform.position = hit.transform.position;
}
}
}
}