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 NutellaDaddy · May 23, 2014 at 04:59 AM · raycastnormalshitvectors

How can I get back a hit?

I shoot down a raycast and I need to know how to get back the hit. I need to get back the hit and than get the normal of the hit so I can use the hit's Vectors(x,y,z). How could I do this. This is the script that I have right now. I am trying to make an fps movement script that is controlled based on the characters normals.Thanks for any help! If you have any other better way to calculate the normals so I can use their Vectors than please do tell. And the commented out lines of code are just what I used for basic walking without things like jumping and running and other such things.

 using System.Collections;
 
 //
 //THIS SCRIPT CONTROLS THE MOVEMENT OF THE PLAYER 
 //
 [RequireComponent (typeof (Rigidbody))]
 public class PlayerMovement : MonoBehaviour 
 {
     //ALL OF THE PUBLIC VARIABLES
     public float walkSpeed = 6.0f;
     public float runSpeed = 12.0f;
     public float jumpSpeed = 8.0f;
 
     //ALL OF THE PRIVATE VARIABLES
     private Vector3 moveDirection;
     private float speed;
 
     void Start () 
     {
         //speed = walkSpeed;
     }
     void Update () 
     {
         //moveDirection = new Vector3 (Input.GetAxisRaw ("Horizontal"),0, Input.GetAxisRaw ("Vertical")).normalized;
 
         RaycastHit hit;
         Physics.Raycast (transform.position, -Vector3.up, out hit);
 
         if (Physics.Raycast (transform.position, -Vector3.up, out hit))
         {
             //RIGHT HERE WOULD BE THE HITS NORMAL VECTORS(X,Y,Z)
         }
     }
     void FixedUpdate()
     {
         //rigidbody.MovePosition (rigidbody.position + transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
     }
 }
 
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

1 Reply

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

Answer by JasonBricco · May 23, 2014 at 05:02 AM

You use the RaycastHit struct to get the hit back. You made a reference for it there, and you gave it the 'out' parameter. That means that 'hit' variable is going to be filled with hit information after the Raycast function runs.

So, in your "//RIGHT HERE WOULD BE THE HITS NORMAL VECTORS(X,Y,Z)" part, you could use:

 hit.normal

To get the normal.

You also don't need two Raycast functions there like you have. The one in the if condition will cast the ray and return if it hit or not. If it did hit, it will run the code where your comment is.

Comment
Add comment · Show 6 · 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 NutellaDaddy · May 23, 2014 at 05:15 AM 0
Share

How could I get the rotation of the hit and than the normal of that and than store it in a Vector 3 to use for movement?

avatar image JasonBricco · May 23, 2014 at 05:19 AM 0
Share

I'm not sure what you mean by the 'rotation' of the hit. The hit point is just a point of collision, which doesn't have a rotation. Could you be more specific?

The hit.normal is going to be a Vector3 representing the normal from where was hit.

avatar image NutellaDaddy · May 23, 2014 at 05:34 AM 0
Share

Well what I'm trying to do is have this character that I'm using have jumping and better movement. It's harder because I am using a planetary gravity script to where he can walk on spheres(planets) and such. SO I need him to jump "up" no matter if he is upside down in global space or not.

avatar image JasonBricco · May 23, 2014 at 05:44 AM 0
Share

In that case, you'll just use the normal for the direction to jump in. So I believe you would just have a jump height Vector3 and multiply it by the normal, and then put that in CharacterController.$$anonymous$$ove or Transform.Translate or what have you. Note: I've never done anything like you're doing there, though, so I'm not entirely sure. You could experiment with that.

avatar image NutellaDaddy · May 23, 2014 at 05:17 PM 0
Share

So I could make a Vector 3 variable and store that hit in it right?

Show more comments

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

Raycast hit sign or somthing!??! 1 Answer

Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer

Only assignment, call, increment, decrement, and new object expressions can be used as a statement 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