Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by omegacraft · Jan 02, 2016 at 01:49 PM · c#raycastobjectcapsulecast

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: alt text

untitled.png (11.0 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image allenallenallen · Jan 02, 2016 at 01:51 PM 0
Share

We need a few clarifications.

Are you shooting the object? Or is the player placing the object somewhere?

avatar image omegacraft allenallenallen · Jan 02, 2016 at 01:54 PM 0
Share

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);
         }
     }
 }
 
avatar image maccabbe omegacraft · Jan 03, 2016 at 06:48 AM 0
Share

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.

avatar image ShadyProductions · Jan 02, 2016 at 01:54 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omegacraft · Jan 03, 2016 at 08:31 PM 0
Share

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 :(

avatar image omegacraft · Jan 04, 2016 at 08:39 PM 0
Share

Thank you for adding those 2 lines of code, everything is now perfect!!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges