Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Eltrotraw · Jan 08, 2011 at 10:30 PM · raycasthitnormal

Resetting RaycastHit normals in code?

I'm working with someone on designing a game that utilizes bouncing shots off of walls, instantiated separately as blocks. The idea is that you're in a grid, taking turns firing at each other and utilizing the walls to bounce shots to hit the opponent.

The previous version of this game utilized the Physics Colliders to bounce shots off of the walls, but I'm utilizing RaycastHit now, utilizing the snippet of code derived from another question(http://answers.unity3d.com/questions/31050/bullet-reflected), but I'm still encountering some problems.

I've taken some screenshots to demonstrate.

http://i.imgur.com/ciUp8.jpg Now, in the first screenshot, you can see with the bullet trail that it bounces off of the walls somewhat properly.

http://i.imgur.com/qkxph.jpg In the second one... I can identify what's going wrong, but not how to fix it.

The problem is that it's identifying one normal properly, but will only stick to that normal until it is reset(somehow). If the normal is (1, 0, 0), it will not count a normal of (-1, 0, 0), for instance. It'll take a normal on another axis, such as (0, 1, 0), but won't take any other normals on that regard.

Here's the code if anyone was curious:

if(Physics.Raycast(transform.position, transform.forward, raycastTarget, 0.5) && raycastTarget.transform != transform && raycastTarget.transform.name == "WallYellow(Clone)") { Debug.DrawRay(raycastTarget.point, raycastTarget.normal * 10, Color.red); incidenceAngle = raycastTarget.point - transform.position; Debug.DrawRay(raycastTarget.point, -incidenceAngle* 10, Color.yellow); reflectionAngle = Vector3.Reflect(incidenceAngle, raycastTarget.normal); Debug.DrawRay(raycastTarget.point, reflectionAngle * 10,Color.green);

 bounce += 1;
 if(Tracer == false) {
     AudioSource.PlayClipAtPoint(bounced, transform.position);
 }
 if(bounce >= maxBounces)
     life=0;

 print("Normal: "+raycastTarget.normal+" Oh, and I'm supposed to bounce! Bounces left: "+(maxBounces-bounce));

 if(raycastTarget.normal.x != 0) {
     raycastTarget.normal.x = -raycastTarget.normal.x;
     rigidbody.velocity.x = -rigidbody.velocity.x;
 }
 else if(raycastTarget.normal.z != 0) {
     raycastTarget.normal.z = -raycastTarget.normal.z;
     rigidbody.velocity.z = -rigidbody.velocity.z;
 }
 print("Normal after bounce: "+raycastTarget.normal);

}

My question is, how do you make the normals identify itself more... dynamically, and not keep it confined to one normal at a time? I thought forcing the normals to reverse themselves through the code would work, but it still only recognizes the same normal on the appropriate axis(i.e. [1,0,0]) no matter what.

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 Loius · Jan 08, 2011 at 10:46 PM 0
Share

A 'normal' is the direction a polygon is facing... from your wording it seems you're trying to use them some way they're not supposed to be used. If the thing is raycasting to the "right" it will only hit polygons whose normals are pointed at least slightly "left"; perhaps that is your problem?

avatar image Eltrotraw · Jan 08, 2011 at 11:22 PM 0
Share

The thing is meant to raycast based on the "raycastTarget" variable, which I thought was dynamic.

The problem I'm describing is regarding the cubes. Yes, I realize what normals are - a cube has 6 normals facing outward.

The way the problem is now only recognizes 3 of these.

avatar image Eltrotraw · Jan 09, 2011 at 12:01 AM 0
Share

Sorry if I sounded hostile earlier anyway. I'm just getting frustrated at the code not exactly working. I did forget to mention that I had another method that I was playing around to work with this, but that utilized something different... but I thought the above code snippet was the easiest way to approach things.

avatar image Benproductions1 · Dec 11, 2012 at 04:50 AM 0
Share

Just out of curiosity, what game is that and how do I play it, if I can? It looks amazing :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eltrotraw · Jan 10, 2011 at 09:14 AM

Nevermind! Through some scrutiny there were some errors with how the code handled the normals to begin with, which means the fault was more in the code's structure and not in the RaycastHit or Raycast.

It mainly had to deal with the transform.forward defined in the Raycast if statement not updating after a bounce.

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

1 Person is following this question.

avatar image

Related Questions

rigidBody is heading to a slightly different angle to where im calculating it would be using Reflect 1 Answer

Rotating a character relative to the ground's rotation 1 Answer

RaycastHit.normal and Orientation to a Path 1 Answer

Get Material of Normal? 0 Answers

Why are RaycastHit surface normals of corners not interpolated or otherwise correct? 1 Answer


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