I have a code that allows to position an object onto the other object by Raycast. Obviously, I am using Mesh Collider so everything works fine.
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<SelectableTrunk>())
{
RandomSTrunk.position = hit.point;
}
}
My question is the next. Is it possible to place the object (as I did) and then do one more raycasting to place the other object onto the already placed one?
When I am trying to do that - The mesh collider on the first object is breaking everything and nothing works.
Specify a layer for the hit object and then follow the code below:
public LayerMask hitPlaceLayer;
void RayCast()
{
// first ray cast
if (Physics.Raycast(transform.position, transform.forward, out var hit, hitPlaceLayer.value))
{
RandomSTrunk.position = hit.point;
}
// second raycast
if (Physics.Raycast(transform.position, transform.forward, out hit, hitPlaceLayer.value))
{
otherRandomTrunk.position = hit.point;
}
}
define layer for Hit place object:
Layer mask for raycasts:
Related
I started a new game project, and I have tried to find a way to only use a rigidbody2D component to move my player game object instead of using transform.position. And I can't come up with a good way to do it or find a tutorial or documentation about it.
I have got it to work with transform.position, but how could I do it with rigidbody2D?
For movement with RigidBody2D you have mutiple options which are physics based.
I assume that's the reason you want to use RigidBody2D in the first place.
RigidBody2D obj = GetComponent();
private void moveBody()
{
//direction towards your goal
Vector2D v = mousePosition - transform.position;
//Example 1 set the velocity
obj.velocity = v;
//Example 2 apply force
obj.AddForce(v, ForceMode2D.Impulse);
}
you can read more about RigidBody2D movement
here
You can throw an invisible collider on your stage and move your object to the position you want with the ray.
Ray ray;
public RaycastHit hit;
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f))
{
Debug.DrawRay(Camera.main.transform.position,hit.point);
transform.position = hit.point;
}
}
Best.
I have an object hit that contains a Polygon Collider, and in this object I have some objects that contain BoxCollider. Now I'm trying to detect when I click Polygon Collider, and when I click Box Collider. So when I click Box Collider, you should avoid the Polygon Collider.
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
if (hit.collider.GetType() != typeof(BoxCollider2D))
{
Debug.Log("Bad Click");
}
else
Debug.Log("Good Click");
}
So I can not find any way to help me. If anyone has any idea, thank you!!!
This shouldn't work at-all because RaycastHit and Physics.Raycast are used for 3D colliders. For 2D colliders, RaycastHit2D and Physics2D.Raycast should be used. Also, for checking if the object has BoxCollider2D or PolygonCollider2D attached to it, the GetComponent function is used instead of hit.collider.GetType(). It returns null when the component is not available.
Your raycast code should look like something below:
if (Input.GetMouseButtonDown(0))
{
Camera cam = Camera.main;
Vector2 wPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(wPoint, Vector2.zero);
//Check if we hit anything
if (hit)
{
if (hit.collider.GetComponent<BoxCollider2D>() != null)
{
Debug.Log("Bad Click");
}
else if (hit.collider.GetComponent<PolygonCollider2D>() != null)
Debug.Log("Good Click");
}
}
That should fix your problem but I suggest you use the new Event System with OnPointerClick. See #7 from my other answer on how to use it with a 2D collider.
I have imported a surface which I modeled with 'blender', I imported it with mesh collider. When I want to do a ray casting on this surface to place random objects it won't work. When I set a standard plane above this surface, the ray casting works on this plane. So I think my modeled surface isn't catching the ray casting, but I don't know why. Can anybody help me?
void Start ()
{
for(int i = 0; i < numberOfObjects; i++)
{
//What we will spawn
GameObject objectToSpawn = objectsToSpawn[Random.Range(0,objectsToSpawn.Length)];
Vector2 spawnPositionV2 = Random.insideUnitCircle*spawnRadius;
Vector3 spawnPosition = new Vector3(spawnPositionV2.x,0.0f,spawnPositionV2.y);
Vector3 transformOffsetSpawnPosition = transform.position+spawnPosition;
RaycastHit hit;
if (Physics.Raycast (transformOffsetSpawnPosition, Vector3.down, out hit)) {
Vector3 finalSpanPosition = hit.point;
Instantiate (objectToSpawn, finalSpanPosition, Quaternion.identity);
}
}
}
}
If you're using mesh collider you should set 'Convex' property to true in order to make it work with collisions. I believe it's actual for raycasting as well.
Also you can enter collider editing mode and check if mesh surface is covered by collider correctly.
i wanted to show the tooltip like this when the mouse over at the object, here is the example image:
and
i already tried this below code, but the message on the debug.log didn't showed up when i am hovering my mouse to the object, the object i give the name same like this:
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit) && hit.collider.gameObject.name == "Yify")
{
Debug.Log("Yify");
}
}
And here is my object (i use List to multiple the objects and each object i gave the name), (The object's name "Yify" is on the right side, dark green color):
Please help. Thank you.
This should work
void Update(){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit ;
if (Physics.Raycast (ray, hit, 100.0) && hit.collider.gameObject.name=="Yify") {
Debug.Log("Yify");
}
}
I have a character controller which moves along the z-axis with a constant speed. I'd like to know the floor name under the character controller. The Character controller never collides with the floor. It's a parallel movement. I used a Raycast to find the floor in C# using:
myray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myray, myhit, 1000)) {
Debug.DrawLine (ray.origin, hit.point);
print(myhit.collider.name);
}
This gives an error. Is there a better solution for this?
It's hard to tell what your issue is, but attaching this behaviour to your character-controlled object will get the name of the first object the raycast hits.
I'm using an inverted layer mask to ignore the "Player" layer, which I set the character-controlled object to. This is so the raycast doesn't hit the object before the floor.
using UnityEngine;
using System.Collections;
public class GetFloorName : MonoBehaviour
{
public string NameOfRaycastHitObject;
void Update ()
{
RaycastHit hitInfo;
int layerMask = ~(1 << LayerMask.NameToLayer("Player"));
float distance = 100f;
if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, distance, layerMask))
{
NameOfRaycastHitObject = hitInfo.collider.name;
}
}
}