Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Orienting player controller to surface they are standing on using raycast, C#, rigid body controller

Discussion in 'Scripting' started by PatrickNeary, Dec 3, 2015.

  1. PatrickNeary

    PatrickNeary

    Joined:
    Feb 3, 2013
    Posts:
    2
    I am using a ray cast , and the rotation value of the object hit. This code works for primitives I have tested it on. Yet not for the rigid body ,or basic first person character controllers. When placed on these controllers , I get a stutter if in update,and an updated value for one frame if triggered using a bool. My guess is that the rigid body rotation value is set already and it's trying to implement both behaviors.. I really want to get this behavior in my project ,and I refuse to let this beat me, please help xD
    Here is the code I am using :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class orientPlayer : MonoBehaviour {
    5.  
    6.     GameObject firstRayHit;
    7.     GameObject lastHitobject;
    8.     //float gravity = -9.8f;
    9.  
    10.     void Awake()
    11.     {
    12.         RaycastHit hitInfo;
    13.         if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo))
    14.         {
    15.             firstRayHit = hitInfo.transform.gameObject;
    16.             lastHitobject = firstRayHit;
    17.         }      
    18.     }
    19.  
    20.    
    21.     //returns the gameobject hit by our raycast
    22.     private GameObject RayToGround()
    23.     {
    24.         RaycastHit hitInfo;
    25.         GameObject _hitObject;
    26.         if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo))
    27.         {
    28.            
    29.             transform.rotation = Quaternion.FromToRotation(Vector3.right, hitInfo.normal);
    30.         }
    31.         _hitObject = hitInfo.transform.gameObject;
    32.         return _hitObject.transform.gameObject;
    33.     }
    34.  
    35.    
    36.     //orient the player towards the sruface they are standing on . uses the returned gameObject from RayToGround
    37.     [ContextMenu("Orient that playaaa")] //This is awesome, lets you right click the componant in the inspector and execute this function =D
    38.     void OrientToGround()
    39.     {
    40.        
    41.         if (RayToGround() != lastHitobject)
    42.         {
    43.             transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, RayToGround().transform.rotation.z, transform.rotation.w);
    44.            
    45.             lastHitobject =RayToGround();
    46.             RayToGround();
    47.            
    48.             Debug.Log("OrientingPlayer");
    49.  
    50.  
    51.         }
    52.     }
    53.  
    54.     void FixedUpdate () {
    55.         Debug.Log(RayToGround().gameObject.name);
    56.         OrientToGround();
    57.     }
    58. }
    59.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Yes, if you have a Rigidbody, you should not be setting the transform.rotation directly. Try setting the rigidbody's rotation instead.
     
  3. PatrickNeary

    PatrickNeary

    Joined:
    Feb 3, 2013
    Posts:
    2
    I tried using get component rigid body but still nothing :(