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
0
Question by boddole · Oct 22, 2014 at 10:24 AM · c#physicsangle

Trouble Understanding Vector3.Angle() Result

Hello everyone, I was looking at this post and tried to make my own little test script that could tell the angle between a projectile's velocity and the surface it hit.

The problem is, the angle returned is always either 90 or a result very close to 90 (89.xxx / 90.xxx), so I either set things up wrong, or I don't understand the result (and I should note that the upper plane is rotated 45 degrees, and the lower plane is rotated to 10 degrees).

Any clarification is appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class zTestProjectiles : MonoBehaviour {
     //goes on the actual projectile prefab object, 
     //makes sure object "explodes" or bounces.
     public float autoBounceAngle; //the angle at and over which a shell will always bounce
     private Rigidbody thisRigidbody;
 
     void Awake()
     {
         thisRigidbody = gameObject.GetComponent<Rigidbody>();
     }
 
     void OnCollisionEnter (Collision other)
     {
         Debug.Log (gameObject.name + " hit: " + other.gameObject.name);
         Vector3 impactNormal = other.contacts[0].normal;
         Debug.Log ("impactNormal is: " + impactNormal);
         Debug.DrawRay(other.contacts[0].point, other.contacts[0].normal, Color.red, 2.0f);
 
         Debug.Log ("thisRigidbody.velocity is: " + thisRigidbody.velocity);
 
         CalculateImpactAngle(impactNormal);
     }
 
     //method to determine if the shell should explode or bounce on impact:
     private void CalculateImpactAngle(Vector3 impactNormalAngle)
     {
         Debug.Log ("CalculateImpactAngle called");
 
         float angle = Vector3.Angle (thisRigidbody.velocity, -impactNormalAngle);
         Debug.Log ("angle is: " + angle);
     }
 }

alt text

testscene.jpg (40.7 kB)
Comment
Add comment
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

2 Replies

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

Answer by SimonTS · Oct 22, 2014 at 05:40 PM

You should not use your rigidbody velocity for this. Instead use the relativeVelocity field of your Collision object, like this:

 void OnCollisionEnter(Collision other) {
     float angle = Vector3.Angle(other.relativeVelocity, other.contacts[0].normal));
     // do stuff with angle
 }

This will give you the angle between your projectile and the other colliders surface normal.

lg

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 SimonTS · Oct 22, 2014 at 08:39 PM 1
Share

To illustrate this (shortly after collision occured):

vectors during collision

impact.jpg (72.2 kB)
avatar image boddole · Oct 23, 2014 at 01:16 AM 0
Share

Right on the money! Thank you.

avatar image
1

Answer by popuppirate · Oct 22, 2014 at 12:03 PM

Try the following:

 float angle = Vector3.Angle (thisRigidbody.transform.position.up, transform.somereference.up);

Make a reference object that is flat and non kinematic, like a plane, and use this as an up reference. The angle function will return the angle from the rigidbody's up to the reference's up in radians.

Convert radians to degrees with (180/(2*Mathf.PI))

This is off the top of my head, so sorry if it's not what you were after!

Cheers,

Popuppirate

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 boddole · Oct 23, 2014 at 01:18 AM 0
Share

This is off the top of my head, so sorry if it's not what you were after!

  • Not quite, but I still appreciate the effort.

avatar image Bunny83 · Oct 23, 2014 at 01:59 AM 1
Share

@popuppirate: Your radians to degrees factor is wrong ^^

Either use

 (180 / $$anonymous$$athf.PI)

or

 (360 / (2*$$anonymous$$athf.PI))

But since Unity has this value as constant the easiest and safest way is to simply use

 $$anonymous$$athf.Rad2Deg
 


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

30 People are following this question.

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

Related Questions

RigidBody Script Conflict 1 Answer

C# Physics.LineCast Argument is Out Range 1 Answer

C# Check Physics.Raycast Once 0 Answers

C# Construct Line Links Over Time 0 Answers

Unexpected "Jumping" Behavior 0 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