I am new to Unity3D. I am trying to do a simple thing. But not able to do this. I have a .obj file which is a 3d key file. I do the followings:
Import this key (to assets) in unity3D
Add this key to scene (from assets to hierarchy)
Add a script to this key
Add the OnMouseDown() function to this script as follows -
void OnMouseDown()
{
Debug.Log ("clicked...");
}
But when I click the key no message is showing in console. Please tell me what is the problem?
make sure the gameobject is not at layer "Ignore Raycast"
Use the following inside your update function to see raycasting is working fine.
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
The OnMouseDown and it's related functions are all actually older systems. The newer system to use for this is the event system.
Specifically to get mouse clicks you would implement this interface in a class:
http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventTriggerType.PointerClick.html
But there is actually an easier way to do it without code.
Using this component:
http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
You can then visually forward the mouse click event to something else.
If you don't fully follow what I have said so far, the thing to really start at is learning the UI system. You can find video tutorials here:
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas
And just to make it clear, the UI system is not just for UI. The UI system runs off an EventSystem which is what you want to use to forward input events to any object 3D or 2D in the entire scene. The UI tutorials will explain the usage of that EventSystem.
You need to assign a collider to the 3d object if you want to fire it with OnMouseDown:
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
This event is sent to all scripts of the Collider or GUIElement.
You can also do like by override click function
public override void OnPointerClick(PointerEventData data)
{
Debug.Log("OnPointerClick called.");
}
Related
Hello guys I try to make a city building game the idea is very simple actually Instantiate an Building and move them with mouse after that hit the place button and place.
The issue is if the collider of a another building completely encloses the collider of the building that I am built I can't move new building.
I can probably explain better with pictures.
Issue 1
The first picture my new building you can see collider limits and the second one my old building that I already placed. I Understand the problem but I can not solve it.
And here it is my object drag code
private void OnMouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000f,(1 << LayerMask.NameToLayer("Ground"))))
{
Debug.Log(hitInfo.transform.name);
transform.position = SnapToGrid(hitInfo.point);
}
}
private Vector3Int SnapToGrid(Vector3 pos)
{
Vector3 tempPos = pos;
Vector3Int snappedPos;
snappedPos = new Vector3Int(Mathf.RoundToInt(tempPos.x), Mathf.RoundToInt(tempPos.y), Mathf.RoundToInt(tempPos.z));
return snappedPos;
}
Thanks for the help in advance
If I got your question right, I believe that you can solve the problem by enableing the "Is Trigger" option of the previously placed buildings colliders at the moment that a new building is being moved around. Enableing this option in an object makes it so other objects can pass trought it when collision happens.
I'm working on a Unity project, trying to test the UI interaction on the Quest II with hand tracking and ray casting.
I've set up a very basic scene with all the default assets (OVRCameraRig, UIHelper) and added just a button to test the UI interaction.
This is what my scene looks like:
The issue I have is, when I run the scene, the Ray is rotated 90 degrees, and it's attached to the wrist for some reason. I made a video here to show what I mean :
https://www.youtube.com/watch?v=5f12yfpugB8
It's still interacting with the UI though.
Then after watching some online tutorials, I commented out these lines in the HandInputSelector.cs, which was attached to the UIHelper:
void SetActiveController(OVRInput.Controller c)
{
/*
Transform t;
if(c == OVRInput.Controller.LTouch)
{
t = m_CameraRig.leftHandAnchor;
}
else
{
t = m_CameraRig.rightHandAnchor;
}
m_InputModule.rayTransform = t;
*/
}
and instead added a 2nd script to the UI helper, with these lines only:
public OVRHand hand;
public OVRInputModule inputModule;
private void Start()
{
inputModule.rayTransform = hand.PointerPose;
}
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
https://youtu.be/q3d0eG2LwY0
My Unity version is 2021.3.1f1
Can someone please tell me what I'm doing wrong?
Okay so basically im working on this test project that relies heavily on movement and momentum to complete levels, with a bit of parkour. I need a collider for two reasons, 1. The player touches an object at the end and gets put back into the menu. 2. If the player falls out of the map they die.
I tried a bit messing around with Colliders and at the Docs and while usually id figure it out ive been stumped for 20 minutes looking at Unity's docs and a few questions from here.
public GameObject objectCollider;
public GameObject anotherCollider;
void OnCollisionEnter(Collision collision)
{
if (CollisionDetection.IsTouching(objectCollider.gameObject, anotherCollider.gameObject))
{
Destroy(plr);
}
}
This is what I got so far. I get an error from here and if I switch it to if collider object == the other it errors out.
Basically what I want (If you perfer to just post the answer code but comments on it would still be helpful so i learn!) is for one gameobject (player (but in code its objectCollider)) to be detected if it touches the other (a cube (in code its anotherCollider)) and to execute code (for example Destroy(playerObject))
Thank you for any help you bring here, links, code anything!
Hopefully this is what you're looking for:
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "endObject")
{
//put back into the menu
}
}
Once your player enters a collision, it checks the name of whatever object it collided with. Then you can execute what code you want in the if statement.
You can use collision.collider for many other things, such as collision.collider.tag, but this should give you a start.
I have a player character, made of some cubes, spheres and a capsule. I created the empty object Player and all body parts of the player are a child of Player. I have two planes, with a moving platform in between. I can walk and jump on the normal planes and the walls, but when the player is on the moving platform the bodyparts of the player fall apart. Maybe it's something really stupid, but I just started with Unity.
This is what goes wrong, the player falls apart on the moving platform: http://nl.tinypic.com/r/207s3sz/9
And below the information about the overview, the player, the body parts, and the moving platform with according character-holder. All bodyparts have the same properties as the body part on the screenshot. Can anyone help me with what goes wrong here? How can I transport the whole player by the moving platform?
HoldCharacter script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoldCharacter : MonoBehaviour {
void OnTriggerEnter(Collider other) {
other.transform.parent = gameObject.transform;
}
void OnTriggerExit(Collider other)
{
other.transform.parent = null;
}
}
You just need to disable the isTrigger flag. Here are some insights
so how Is Trigger works is that... it will fire OnTriggerExit and OnTriggerEnter, but it will let the object go through it. If you disable the IsTrigger, then you need to move the logic to OnCollisionEnter on OnCollisionExit methods. If the isTrigger uncheck kind of worked, maybe is just the fact that you move the logic for HoldCharacter to OnCollisionEnter and OnCollisionEnd respectively Like this:
void OnCollisionEnter(Collision collisionInfo) {
collisionInfo.gameObject.transform.parent = gameObject.transform;
}
void OnCollisionExit(Collision collisionInfo) {
collisionInfo.gameObject.transform.parent = null;
}
Regards
If I'm correct, the children of objects with Rigidbodys have physics as well. Maybe put the rigidbody on a child of the player, like so.
Player
-head
-arms
-legs
-empty gameobject with rigidbody
Novice here - of course. I'm using Unity 5, and am looking to display on screen the coordinates of a player using a FPS camera. The main reason is I'm using a camera path animator to fly around a terrain scene I built for class, and think by logging coordinates via walking around in FP will make placing my camera path waypoints a lot easier.
What I have done:
-Created new empty game object -Added Component -> Rendering -> GUIText -Added Component -> New Script ->C#
Now I of course searched around to find a solution and found this:
function OnGUI () {
GUI.Label (Rect (10,120,500,100), "X = " + transform.position.x + " Y= " + transform.position.y + "Z= " + transform.position.z);
}
Which is great! But I am not a strong C# programmer by any means, but I do know this needs a Player variable of some sort yet. So please, could anyone simply direct me, or mind posting the code needed to make player coordinates appear on the screen. This doesn't have to be pretty or anything, I just need to write down the data as I walk around in FP. (unless you want brownie points and know of a way to log coordinates into a .txt file on button press... hahaha Thanks guys!
I'm not sure this is what you want....
Create new GameObject(GUIText) and make it's parent with your player game object. Now GUIText game object will follow your player object automatically
Add new script(i.e, PlayerPositionLogger.cs). to GUIText game object.
Find player script on void Start() on PlayerPositionLogger.cs or declare public Player player; and link player object on editor.
Update content of GUIText with player object.
gist.github.com/growingdever/39b758d5f3967c143744
Select the player object
Add component > New Script
Add the following code to the script (replace Start and Update)
void OnGUI() {
GUI.Box (Rect (10,10,100,90), "Player position: X:" + transform.position.x +
" Y: " + transform.position.y + " Z: " + transform.position.z);
}
When you enter playmode it should do what you want. Your main problem was GUI layout.