- Home /
How do i make the object at the end of my raycast not phase into a wall?
Its very hard to explain, but here are some pictures of what I mean:
We need a few clarifications.
Are you shooting the object? Or is the player placing the object somewhere?
Placing. Here is the script:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.ImageEffects;
public class Teleport : $$anonymous$$onoBehaviour {
public Transform cube;
public GameObject qub;
public Transform playa;
public VignetteAndChromaticAberration vigg;
public TeliPortation tali;
public AudioSource asas;
// Use this for initialization
void Start () {
qub.SetActive (false);
}
// Update is called once per frame
void Update () {
bool foundHit = false;
RaycastHit hit = new RaycastHit();
if (Input.GetButton ("Fire2")) {
vigg.intensity = 1;
foundHit = Physics.CapsuleCast (transform.position + (transform.up * 0.5f), transform.position + (transform.up * -0.5f), 0.2f, transform.forward, out hit, 35);
qub.SetActive (true);
}
if (Input.GetButtonUp ("Fire2")) {
if (!tali.stopped) {
vigg.intensity = 0;
}
if (tali.stopped) {
vigg.intensity = -3;
}
playa.transform.position = cube.position;
qub.SetActive (false);
}
if (foundHit) {
cube.position = hit.point;
cube.rotation = Quaternion.LookRotation (hit.normal);
}
}
}
If you place the object at the player and shoot a ray back towards the object (ignoring all other colliders) from the hit point (on the wall) you'll have a good idea of how far you can move the object before it starts phasing into the wall.
That is probably because you take the position of where you clicked so the position of the wall. You will need to make up some logic so it sets the player infront of the object orsomething.
Answer by AminAghajoon · Jan 03, 2016 at 09:05 AM
You can simply get your object bounds and create it on the right position; something like this :
GameObject go = new GameObject();
go.transform.position = new vector3(hit.point.x - bounds.x * 0.5f , hit.point.y - bounds.y * 0.5f ,hit.point.z - bounds.z * 0.5f);
I didn't want to give you the full solution , i just wanted to give you the idea . This is also another idea : you can find the direction and then put your circle object back by its radius.
correctPoint = Hitpoint - (directionVector.normalized * radius);
Or if you have a ray
correctPoint = ray.GetPoint(hitPoint - startPoint - radius);
that wont really work, because if lets say that all the axis bounds are -1.5 that would be fine for a while, if your ai$$anonymous$$g at the ground is perfect at the right wall, perfect, but if you aim up you go trough the ceiling and if you aim left same thing happens, and I don't thing I need to explain why, and my scripting knowlage isn't that good, so I don't know how to fix it :(
Thank you for adding those 2 lines of code, everything is now perfect!!
Answer by HellsPlumber · Jan 04, 2016 at 09:15 AM
As you can see in your images you need the object to be placed against the wall, but with an offset equal to half of the object's size or radius.
So the object's transform would be something like this:
transform.position = hit.point - new Vector3(0f,objectRadius/2f,0f);
You'd have to put the "objectRadius/2f" part in whichever part of the Vector3 is the axis you need. That should be enough for your example image, however if your game has other surfaces that use this mechanic (Ceilings and Floors for example) then you'll need to look into Normals.
Your answer
Follow this Question
Related Questions
Interaction with various objects 0 Answers
Multiple Cars not working 1 Answer
Raycast Snapping Problem. 0 Answers
Distribute terrain in zones 3 Answers
Referencing color change when object is 'deselected' 2 Answers