I am working on the game. and I want to know that position (2,2,5) has an object or not?
whenever i place a object at that position it should debug YES.
Please help me to solve my doubt.
Hey you can use the unity Physics.CheckSphere method.
The code would look something like this.
Vector3 pos = new Vector3(2,2,5);
float radius = 4f; //Radius to check in;
if(Physics.CheckSphere(pos,radius))
{
//Found
print("Found Object");
}
You would need to put this in some sort of repeating function I'm guessing. Also it may be important to pass in a LayerMask to the checkSphere method so that you only detect specific objects, not the ground for ex.
Related
I don't want my player to be able to walk off ledges. I did this by shooting a single raycast downwards in front of the player, and if ground is NOT hit, then ignore input.
However this is jarring, especially if you diagonally walk along an edge you just completely stop, rather than 'slide' along it.
So I thought I could add two raycasts, one per side to detect which side the ledge is, then allow movement (or steer) the player as applicable.
The problem is I'm not sure how to proceed from here. I'm using a character controller for movement, my current code is like:
velocityXZ = velocity;
velocityXZ.y = 0; // we deal with gravity elsewhere
velocityXZ = inputDir * playerSpeed;
if (facingDropLeft || facingDropRight) {
velocityXZ.x = 0;
velocityXZ.z = 0;
}
velocity = new Vector3(velocityXZ.x, velocity.y, velocityXZ.z);
// handle gravity
charController.Move(velocity * Time.deltaTime);
Could anyone offer some insights into what direction to look into, or methods I will need?
I think that if you want to accomplish an enjoyable result you should use invisible walls. I think that probuilder can help you.
This is the approach I would have with this type of problem.
Use boxes to make wall than turn off the mesh renderers this will make invisible walls
Im working on RTS 2D izometric game and i wanna use A* pathfinding for moving units. I need to know current direction of instance, which is moving along the path.
A better solution, in my opinion, is to look at the AIPath object's desiredVelocity field, which points in the direction that your unit wants to go. For example:
private void Update() {
// Point the creature in the direction of movement
if (_aiDestination.target != null) {
_spriteRenderer.flipX = (_aiPath.desiredVelocity.x > 0.0f);
}
}
Docs here.
Figured it out. I used variable to store previous position and then compare it to current position. From that i could determine in which direction is instance moving.
I want to create a click-to-move game, and have got a nav-mesh agent with code which almost works, except my character always attempts to move to the same spot, regardless of where I click or the position of the camera. This is my function,
private Vector3 GetClickPosition()
{
Vector2 screenPosition = Input.mousePosition;
Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(screenPosition);
RaycastHit hitPosition;
Physics.Raycast(mouseWorldPosition, cam.transform.forward, out
hitPosition, 100, Floor);
return hitPosition.point;
}
Which is used in an on-click command connecting to the player. Everything is referenced and I'm sure the problem is with this piece of code... Thanks in advance.
My source for this code: https://www.youtube.com/watch?v=LoKNYlWWeSM
The source you reference works for me so perhaps something has gone slightly off in your version.
Try assigning the appropriate layer, the one that is referenced as Floor in your Physics.Raycast method, onto the terrain you want to navigate. You may have missed out on this if you renamed the groundLayer mentioned in the source but didn't create and assign the corresponding layer in the editor. Be sure to also set the floor you are using to be static in the Navigation -> Object tab.
I suspect your problem lies here:
Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(screenPosition);
Physics.Raycast(mouseWorldPosition, cam.transform.forward, out hitPosition, 100, Floor);
ScreenPointToWorldPoint() may already be raycasting to find where the world your mouse is (exactly how Unity determines the vector this method returns is not documented), raycasting again from that point in...the direction the camera is facing is probably screwing with you.
Instead, try using cam.ScreenPointToRay() as the parameter into your raycast.
I'd like to modify the rotation of the camera object via a gamepad. I have code that works in the editor, but doesn't work on iOS. (However, it looks like it's working but being pushed back on the 2nd frame by the VROne code).
I was able to get this working w/ the Rift, but haven't been able to figure it out with VROne yet. For the Rift I added an "offset" to the rotation that was changed by the GamePad joystick. The offset was calculated into the final rotation that also includes the players look direction.
Any idea what part of the code I'd modify to get this properly working with the VROne sdk?
If you want to use your own GameRotation angles (from the gamepad), the values might be getting overwritten by the integrated HeadTracking. Have you tried disabling the VR Head Tracking Script, in the VROneSDKHead object?
The HeadTracking angles are dealt with in the HeadTrackingIOS class, which calls the native static library. You can try adding your gamepad offset to the Quaternion returned on line 43.
Hope this helps, let us know if it worked!
EDIT: Nevermind, the below code works in the editor, but on the iOS device the camera is still forced ahead. I'll try the other answer.
Cool -- that may work, the above answer. It sounds like what I was looking for.
However, per a suggestion on the Unity forum, I implemented this solution: First I made the VROne Object a child of another empty game object, with the localPosition 0,0,0.
On rotation, I rotate the parent, but first make sure the parent is the same position of the child and then the child localPosition is 0,0,0 - -right now the child is doing the physical movement, and the parent does the rotation. I'm not sure if moving the character controller to the parent would work or make something else funky in the SDK, but this seems to work for now.
if (RightStickX.Value != 0)
{
transform.position = childObject.transform.position;
childObject.transform.localPosition = Vector3(0,0,0);
transform.eulerAngles.y += RightStickX.Value * rotationSpeed * Time.deltaTime;
}
Just in case this would help someone, I was able to solve it in a much simpler way.
The VrOneSDK object is a child of the object that I am rotating with my gamepad.
and then in 'vrHeadTracking.cs' I simply changed
transform.rotation = Quaternion.Inverse(initialRotation) * rot;
to
transform.localRotation = Quaternion.Inverse(initialRotation) * rot;
that way the head tracking is relative to my parent object's rotation.
I'm working to let a character push objects around. The problem is that as soon as he touches an object he starts moving it, even when the touch was accidental and the character isn't facing the direction of the object.
What I want to do is get the direction the object when a collision happens, and if the camara is really facing that direction allow the player to move it.
Right now I only managed to get the direction of the object, but I don't know how to compare that with the direction of the camera.
This is what I'm trying now:
void OnCollisionEnter(Collision col) {
float maxOffset = 1f;
if (col.gameObject.name == "Sol") {
// Calculate object direction
Vector3 direction = (col.transform.position - transform.position).normalized;
// Check the offset with the camera rotation (this doesn't work)
Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;
if(offset.x + offset.y + offset.z < maxOffset) {
// Move the object
}
}
You can try to achieve this in several different ways. And it is a bit dependent on how precise you mean facing the box.
You can get events when an object is visible within a certain camera, and when it enters or leaves using the following functions. With these commands from the moment the box gets rendered with that camera (so even if just a edge is visible) your collision will trigger.
OnWillRenderObject, Renderer.isVisible Renderer.OnBecameVisible, OnBecameInvisible
Or you could attempt to calculate whether and objects bounding box falls within the camera's view frustum, there for you could use the following geometry commands
GeometryUtility.CalculateFrustumPlanes, GeometryUtility.TestPlanesAABB
Or if you rather have a very precise facing you could also go for a Physics.Raycast So then you will only trigger the event when the ray is hitting the object.
Hope this helps ya out.
Take a compass obj and align it to ur device sorry object than when you move it you can always know where it points to.
theoretically it should work but maybe your object just moves around because of a bug in your simulator motion engine.