I am new to Vuforia.
The gameobject to which the script is added, is a 3d object which is made visible on user-defined triggered image.
I know this is not a new question and I have gone through each of the thread/post on official Vuforia discussion blog for that matter but the problem still persists. And the problem seems very fundamental.
I have the following script attached to my gameobject :
void Update ()
{
if (Input.touchCount == 1)
{
// Touches performed on screen
Ray ray;
RaycastHit hit;
Debug.Log ("2");
if(Camera.main != null)
{
Debug.Log ("3");
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
hit = new RaycastHit();
Debug.Log ("33");
if(Physics.Raycast(ray, out hit))
{
Debug.Log ("4");
}
}
}
}
When I run the scene and touch on the gameobject, the Debug Console shows
2
3
33
BUT NOT 4. Somehow this ray doesn't hit the object.
This script works fine with the normal camera. Could anyone please shed some light on this.
Thanks
(as far as I can tell) Vuforia doesn't use the ARCamera for collision detection. Instead there is another 'Background Camera' (you can see it if you run your app in Unity and pause it; you'll find it in the Hierarchy pane). To access it use
Camera.allCameras[0]
instead of
Camera.main
Hope that helps
I think this is a bug between Collider class and ARCamera, but the solution is this:
Create a new Scene
Create a Cube or any gameobject with collider component.
NOT delete the Cube for any reason
Test with any hit algorithm(touch or mouse)
using System.Collections;
using UnityEngine;
public class rayoPrueba : MonoBehaviour {
void start () {print("entro"); }
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print("Si le jue");
}
}
Replace the mainCamera for ARCamera
Test Again
Put u`r Cube inside ImageTarget and the real model
Delete the Cube and lets dance!! I dont know why, but the bug is killed with this ...(Y) ..
The trick is... Never lose a gameObject with Collider Component from the scene..
If you are trying take a hit with RayCast on 3d model, you should sure add Box Collider Component on 3d model.
Related
I need to spawn a cube at the location where a raycasts hits, but the problem is that i spawn that cube at the edge of the collider that it hits. so its spawns the cube inside the other cube.
here is a picture
This isnt for a game, im just messing around but i think its an interesting problem.
Once you have the hit point you can instance the cube wherever you wish, either in the hit gameobject position getting the gameobject position goPos (not debugged code):
Vector3 goPos = Vector3.zero;
if (Physics.Raycast(ray, out hit)){
if (hit.collider != null) {
Debug.Log("hit!");
goPos = hit.collider.gameObject;
}
else {
Debug.Log("no hit");
}
}
Or, the hit position itself, with hit.point. Check the documentation
You can draw debug elements in the scene with OnDrawGizmos() to check if you might be changing the hit point somehow before instantiating your cube.
Also you would need to share some code to check how the hit point is obtained and the cube instantiated.
By default object is spawn at the center point of the object if you want to spawn at a custom position you can make a empty gameObject and make the cube a child of that then you can position the parent anywhere you want and make that a prefab to instantiate.
I can think of what objects I will use in the game and add them a box collider or mesh collider and start setting them up. Or I could use maybe raycast and detect the distance from the objects I want to avoid walking through ? Is that logic to use distance calculation with raycast ?
void FixedUpdate()
{
Vector3 direction = new Vector3(transform.position - lastPosition);
Ray ray = new Ray(lastPosition, direction);
RaycastHit hit;
if (Physics.Raycast(ray, hit, direction.magnitude))
{
// Do something if hit
}
this.lastPosition = transform.position;
}
Not sure if this is the right script to put on the player. Maybe need to use layer ? But the logic is that if the player is for example 0.3f distance form the object that the player will stop moving forward.
Using unity you don't really want to be doing any of this stuff from the code behind, instead you'd have a RigidBody or RigidBody2D component on your "player", and then a box Collider or mesh collider (or the 2d versions of those) on your collision objects.
This will handle all collisions and stop the player when they hit that object.
Another word of advice is, when handling collisions in the backend you don't want to use FixedUpdate as if an object is travelling fast enough it will pass through the collider between updates
I know this is quite the noob question, but whatever, there's only one way to learn.
I've created an empty GameObject in Unity, attached a script that is supposed to move a cube(my player) and gave my cube the tag "Player". After creating the cube, I was hoping to be able to move the cube without having to put the script on the cube itself. When the script is on the cube, it moves without a problem (I know this is how it probably should be done, but for trying to learn new things I wanted to do it this way).
Player Controller script
Cube properties
After failing to find the answer through Google, any insight at all is greatly appreciated!
Thank you
Update!
Here's the code as text now, since it was asked for.
public class GameCoreController : MonoBehaviour {
private GameObject PlayerMove;
public Rigidbody rb;
void Start ()
{
PlayerMove = GameObject.FindGameObjectWithTag("Player");
rb = GetComponent<Rigidbody>();
}
void Update()
{
// character movement
if (Input.GetKey(KeyCode.W))
{
PlayerMove.transform.Translate(0, 0, 0.25f);
}
if (Input.GetKey(KeyCode.S))
{
PlayerMove.transform.Translate(0, 0, -0.25f);
}
if (Input.GetKey(KeyCode.A))
{
PlayerMove.transform.Translate(-0.25f, 0, 0);
}
if (Input.GetKey(KeyCode.D))
{
PlayerMove.transform.Translate(0.25f, 0, -0);
}
}
I've updated the code from before to include PlayerMove.transform.Translate but I still have the same issue with the cube note moving. I've also included screenshots of my scene with the cube and the GameCoreController; the empty GameObject holding the script that is supposed to control the cube.
Thanks again for the help guys.
Update 2!
After deleting the cube and re-insert it into the scene it now moves. Thanks for the help everyone.
The reason that the cube won't move because in your code you didn't move its transform but you move the transform of the gameobject that you attached this script to.
transform.Translate move the transform of the gameobject that this script attach to. So if you want to move the cube, all you need to do is change from transform.Translate to PlayerMove.transform.Translate which will move the transform of PlayerMove gameobject which is your cube with "Player" tag on it
^ All of the above. Plus, in the screenshot, your rigidbody is not set to "is kinomatic". This means that physics will still be applied to it (like gravity). Rule of thumb: If you have a moving object where collision is important, it will need a rigidbody and a collider. If the object is not moved with physics commands (eg rigidbody.AddForce()) but rather manipulating the transform as you are, set the rigidbody "isKinomatic" property to true.
So I have this mesh that I generate using perlin noise
What I want to do is to be able to click to place an object in the game. My ultimate goal is to have a menu so you can place different objects but I want to just try to get a cube to work. I tried RayCasting:
public class Raycast : MonoBehaviour
{
Ray myRay; // initializing the ray
RaycastHit hit; // initializing the raycasthit
public GameObject objectToinstantiate;
// Update is called once per frame
void Update()
{
myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out hit))
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(objectToinstantiate, hit.point, Quaternion.identity);
Debug.Log(hit.point);
}
}
I could not get this to work... I had no real idea how to use this script either (like what object to place it on) I tried just making a cube and putting the script on that but It did not work. I got no errors it just did not work.
I also had a mesh collider on my mesh and a box collider on my cube.
I see that you are following the tutorial on YouTube "Procedural Landmass Generation", which I would say is an excellent way to understand the basics of procedural content generation.
For your question I would suggest you add a camera on the top of the terrain, from where you will be able to point with your mouse where you want to instantiate the object (the cube for example). You would need a function which raycasts to the your land tiles. Check if its gameobject's tag is "Terrain" and check if there is no water at that current location. If terrain check returns true and water check returns false, then instantiate your desired object at the "hit" position of the raycast.
You can put the script on any object you want. I'd propose the camera. After adding this script-component to the camera, drag your mesh (that you want to instantiate) into the "objectToinstantiate" slot you see in the script.
Then see if it works, and for performance move the if (Input.GetMouseButtonDown(0)) up in your code, before you do the raycast, like #Basile said.
Note: This script will work in play-mode on the game-view - not in the editor scene-view.
If you'd like this to work too, you should add [ExecuteInEditMode] above the public class ... line. But be careful with those, it's sometimes hard to stop those scripts :P
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Enemy1") {
enemyDamage++;
GameObject clone = (GameObject) Instantiate (tempBloodSplat,enemyObj.position,enemyObj.rotation);
Destroy (clone , 0.5f);
if (enemyDamage > 3) {
anim.SetFloat ("Die", 0.5f);
Destroy (enemyObj.gameObject , 5.0f);
}
Debug.Log ("Bullet is hitting Enemy");
}
}
This is my code, i used on BulletObject
My Bullet Object has Collider
My Enemy has a Collider
My Enemy has a Rigidbody
Bullet is not having Rigidbody
I have problem that when i shoot, the bullet is hitting the Enemy in his range, like the circle under the body of the Enemy,Image
Sometimes the bullet hits correctly (means enemy is damaged), but sometimes the bullet moves out without making any damage to the enemy,I don't know why its happening.Does the velocity of the bullet has any effect on it..
Please help or guide me to solve this problem, Thanks
If you are not using a rigidbody on the bullet, then you are probably updating the bullets position vector directly, and what could be happening is the following:
. Since the bullet is not a rigidbody, Unity does not 'assume' it should behave like one and thus does not do an actual physics simulation of the bullet movement (which would probably include a raycast from start position to end position and colision checking in between). If you have a problem with adding a rigidbody to the bullet, then do the raycast yourself. You will even learn a bit of how the physics simulation behind unity actually might work!
Good Luck!