Search Unity

Detecting rigidbody.velocity after collision and set it to kinematic.

Discussion in 'Scripting' started by Foose, Jul 28, 2014.

  1. Foose

    Foose

    Joined:
    Jul 3, 2014
    Posts:
    21
    Hey guys. I really didnt know how to make it clear in the title, so this is my problem. So basically I have a farm environment and a field adjusted to the x and z axis (3D scene). The field is surrounded by fences which are rigidbodies. Now I want to move my player in this field BUT I want him to move from his current position to the fence in his moving direction with only one keypress. That's no problem. The problem is i want the player to be kinetic after he hit the fence. I cannot do a normal onCollision function because there is the opportunity that he is moving along the fence on one side which would trigger the onCollision function directly after the moving function has started. Then I had an idea. I should be able to detect at which point his velocity is below a given value because when he hits the fence, the velocity should automatically be less then it was before right? So I came up with this function. Problem is, it is not working^^

    Code (CSharp):
    1.      
    2.         if (Input.GetKeyDown (KeyCode.RightArrow)&& moves > 0 && rigidbody.isKinematic == true){
    3.             rigidbody.isKinematic = false;
    4.             rigidbody.velocity = new Vector3(0, 0, -speed);
    5.             if (rigidbody.velocity.z > speed) {
    6.                 rigidbody.isKinematic = true;
    7.                 moves = moves - 1;
    8.             }
    9. }
    10.  
    The rigidbody.velocity.z detection isnt working, so he character is stil moving a little along the fence, after he collided with it. Any ideas?
     
  2. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    A better solution would be to use a raycast to detect a collision with the fence. http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    fire some rays from the front of the player and if they hit the fence and the hit point is within a certain distance of the player you know the player has reached the fence.
     
  3. Foose

    Foose

    Joined:
    Jul 3, 2014
    Posts:
    21
    thx for the response. could you please give me a sample? I have no clue how to get the distance when shooting the ray :(
     
  4. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Sure just look at the second example in the page I linked. In this example the raycast is going down (-Vector3.up) so for your purposes you would probably want to use Vector3.forward instead.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Update() {
    6.         RaycastHit hit;
    7.         if (Physics.Raycast(transform.position, -Vector3.up, out hit))
    8.             float distanceToGround = hit.distance;
    9.        
    10.     }
    11. }
     
  5. Foose

    Foose

    Joined:
    Jul 3, 2014
    Posts:
    21
    still got nothing :(

    Edit. Found out the Ray is casting forward, back, left and right in camera alignment. but my character is moving along the axis. any idea how i can cast the ray in my characters movement direction?
     
    Last edited: Jul 29, 2014