- Home /
How to move object when click on specific tile?
I want to move polygon object (cube, triangle, etc) when i click on specific tile of the object. How to do?
][1]
Answer by ShadoX · Apr 01, 2016 at 06:21 AM
Try taking a look at http://answers.unity3d.com/questions/128747/detect-face-of-cube-clicked.html
Think a "simple" way to do it would be to: 1) detect which face was clicked and get the coordinates of it (x,y,z) 2) compare the coordinates to the center coordinates of the cube and use that to figure out which way the cube should start moving * can use static values for moving it by 1 block or probably just get the side from the cube object itself and then just move it 1 unit forward
Hope this helps in some way :)
Thank you, i'm a absolute beginner for Unity3D game development. I will try it then give you respond ASAP. Sorry for my english. Thank you again :)
@ShadoX i tested your suggestion. it works for detect face of objects. But when i rotate object by 90 degrees. it gives me same result.
Example: Before rotation: result of cube is South, Up, East ... After rotation: result of cube is South, Up, East...
What i want is: Before rotation: result of cube is South, Up, East ... After rotation: result of rotated cube like (if 90 degree rotated East, Up, North)
Here is my code:
using UnityEngine;
using System.Collections;
public class SideDetecter : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
public float speed = 50f;
public Transform gunObj;
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButton(0)) {
//transform.RotateAround(Vector3.zero, Vector3.up, speed * Time.deltaTime);
//Debug.Log ("$$anonymous$$ouse Clicked");
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("RayCast");
Vector3 inco$$anonymous$$gVec = hit.point - gunObj.position;
Vector3 reflectVec = Vector3.Reflect(inco$$anonymous$$gVec, hit.normal);
Debug.DrawLine(gunObj.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
}
//transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
GetHitFace(hit);
}
}
public enum $$anonymous$$CFace
{
None,
Up,
Down,
East,
West,
North,
South
}
public $$anonymous$$CFace GetHitFace(RaycastHit hit)
{
Debug.Log ("RayCast2");
Vector3 inco$$anonymous$$gVec = hit.normal - Vector3.up;
if (inco$$anonymous$$gVec == new Vector3(0, -1, -1)) {
Debug.Log ("$$anonymous$$CFace.South");
return $$anonymous$$CFace.South;
}
if (inco$$anonymous$$gVec == new Vector3(0, -1, 1)) {
Debug.Log ("$$anonymous$$CFace.North");
return $$anonymous$$CFace.North;
}
if (inco$$anonymous$$gVec == new Vector3(0, 0, 0)) {
Debug.Log ("$$anonymous$$CFace.Up");
return $$anonymous$$CFace.Up;
}
if (inco$$anonymous$$gVec == new Vector3(1, 1, 1)) {
Debug.Log ("$$anonymous$$CFace.Down");
return $$anonymous$$CFace.Down;
}
if (inco$$anonymous$$gVec == new Vector3(-1, -1, 0)) {
Debug.Log ("$$anonymous$$CFace.West");
return $$anonymous$$CFace.West;
}
if (inco$$anonymous$$gVec == new Vector3(1, -1, 0)) {
Debug.Log ("$$anonymous$$CFace.East");
return $$anonymous$$CFace.East;
}
return $$anonymous$$CFace.None;
}
}
Answer by AliSOft · Oct 04, 2017 at 08:04 AM
The following code has worked for me.
TransparentObject.transform.position = hit.transform.position + hit.normal;
TransparentObject.transform.rotation = hit.transform.rotation;
Let me know if it worked for you or not.
Answer by xPro-Gamerx · Jul 24, 2021 at 10:27 AM
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.Translate(0.1f, 0f, 0f);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(-0.1f, 0f, 0f);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(0.0f, 0f, -0.1f);
}
if (Input.GetKey(KeyCode.W))
{
transform.Translate(0.0f, 0f, 0.1f);
}
}
}
this is the way to move
Your answer
Follow this Question
Related Questions
Moving object away from newly created object created by script 0 Answers
Move object from A to B around Zero 1 Answer
Problem with player bouncing or jumping 1 Answer
Make object move (not teleport) to another object 1 Answer
Moving an object along one axis based on the position of a different object 0 Answers