- Home /
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);
}
}
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.
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?
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.
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.
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.
So I could make a Vector 3 variable and store that hit in it right?
Your answer
Follow this Question
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