- Home /
Making own character clickable
Greetings all,
I have a scene. What I am trying to do is create a 2D looking game which actually uses 3D assets but how its viewed by camera makes it seem 2D.
I have my own characters .fbx. I want to be able to insert them into the scene and click on them. When I click on them I will move the character and change the character animation too. On the character (fbx) I add a collider and rigid body.
The problem I am having is b4 I can even get to the animation & change position stuff, being able to click on the character is problematic.
i.e. I drag the character into the scene. I also create a generic cube asset via Unity. I have a text to update "you have clicked " just for visual rep of what has been clicked.
I have looked online about IPointerClick interface (implement this interface to receive OnPointerClick callbacks. I have an Event System in the Scene to allow click detection. PhysicsRaycaster is attached to the Camera) .
Also looked into tried the GetMouseButtonDown() method.
I have a script attached to the camera:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class youTubeObjectClicker : MonoBehaviour {
public Text youClickedHint;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//only if the button is down or screen clicked
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
if (hit.transform != null)
{
PrintName(hit.transform.gameObject);
print(Input.mousePosition);
Rigidbody rigBod;
//if it has a RB
if (rigBod = hit.transform.GetComponent<Rigidbody>()) {
print(Input.mousePosition);
}
}
}
} //end if clicked
} //end update
private void PrintName(GameObject go) {
print(go);
youClickedHint.text = go.name + " is clicked by mouse";
}
}
I have added the character to their own layer. The Physics Raycaster --> under Event Mask has this layer of characters included.
In the scene I have a cube, 3 animals of the same type using the same fbx and a character of a boy. The cube is the unity made 3D Object --> Cube. The others are .fbx's.
Press Play and you can click each character and the cube. The cube you can click anywhere on the cube. The fbx characters tho (with rigid body and collider added after import to Unity) I have to click a certain small point on the character for it to register the click.
So I wonder 1) What controls the selection area (via click , mouse pointer in Unity but will be finger touch on mobile) and 2) what is the best way to capture the click on each fbx character?
I would like to be able to click anywhere on the character. The area of the collider covers all the clickable area I want for the characters.
I am new to working with fbx's. Other work ive done with Unity so far uses Unity in-editor made assets.
Thanks
Answer by tormentoarmagedoom · Sep 14, 2018 at 08:25 AM
Good day.
I think the best way is using OnMouseDown() function. But you need to read the manual of the method, This method needs that the object you click have a Collider and (i dnt remeber) also a Rigidbody.
Check the manual.
Keep in mind that you have different methods of this style: OnMOuseOver, OnMouseDown, OnMouseUp
Good luck! Bye!
Thanks @ tormentoarmagedoom for taking time to reply.
Yep I read the docs, have a collider and rigid body as mentioned :) I just wondered if it was say the collider that 'registered the click' (this its this size that 100% holds the boundaries for the clickable area of the character its applied to). Or whether this is done by another component of this asset, its not clear from the docs.
With a little experience in Unity, I wondered what others have prefered to use for this job (method wise/ Unity API wise in general). I hear you say that On$$anonymous$$ouseDown() route u think is best, awesome! I shall persist with this one our of the two.
Cheers
Your answer
Follow this Question
Related Questions
Problem With Raycasthit Angle 0 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
camera.ScreenPointToRay always has same origin... 1 Answer
Why doesn't Physics.Raycast(ray, RaycastHit, distance) work? 1 Answer
Help Playing "Sound Clip" on RayCastHit? (RayCast help?) 2 Answers