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. Dismiss Notice

Raycasting in Kinematic

Discussion in 'Scripting' started by skuky123, May 3, 2021.

  1. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14
    Hello to every1!

    Im trying to make a game similiar to golf with some abilites, one of ability is levitating above air with keydirection, for 5 seconds ball is levitating in the air and you can go left and right while ball is automatically moving forwards. I made everything working perfectly but theres issue If my ball reaches the slope, it just goes through it. Ball is kinematic while in air so code for levitating could work. Im trying to make raycasts that detects slope, when does it should change rotation and keep goin forward parallel to the slope. Code will be posted at the bottom of the thread. I need help with code that could rotate my ball while its in kinematic mode. Also I have "height" float that defines the lenghts of those raycasts, change of lenght only works while in kinemathic is false, if its true Im not able to change lenght.


    Heres code for raycasts;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SlopeBehaviourGroundCheck : MonoBehaviour
    6. {
    7.     public float height = 0.3f;
    8.     public float heightPadding = 0.05f;
    9.     public LayerMask ground;
    10.     public float maxGroundAngle = 90;
    11.     public bool debug;
    12.     public FloatVariable GlobalVal;
    13.  
    14.     float angle;
    15.     float groundAngle;
    16.  
    17.     Vector3 forward;
    18.     RaycastHit hitInfo;
    19.     public bool grounded;
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         CalculateForward();
    32.         CalculateGroundAngle();
    33.         CheckGround();
    34.         ApplyGravity();
    35.         DrawDebugLines();
    36.  
    37.     }
    38.    
    39.     void CalculateForward()
    40.     {
    41.         if(!grounded)
    42.         {
    43.             forward = transform.forward;
    44.             return;
    45.         }
    46.         if(GlobalVal.AbilityUfoPicked == true)
    47.         {
    48.         forward = Vector3.Cross(hitInfo.normal, -transform.right);
    49.     }
    50.     }
    51.  
    52.     void CalculateGroundAngle()
    53.     {
    54.         if(!grounded)
    55.         {
    56.             groundAngle = 90;
    57.             return;
    58.         }
    59.         groundAngle = Vector3.Angle(hitInfo.normal, transform.forward);
    60.     }
    61.  
    62.     void CheckGround()
    63.     {
    64.         if(Physics.Raycast(transform.position, -Vector3.up, out hitInfo, height + heightPadding, ground))
    65.         {
    66.             grounded = true;
    67.             GlobalVal.isGrounded = true;
    68.  
    69.  
    70.         }
    71.         else
    72.         {
    73.             grounded = false;
    74.         }
    75.     }
    76.    
    77.     void ApplyGravity()
    78.     {
    79.  
    80.     }
    81.    
    82.     void DrawDebugLines()
    83.     {
    84.         Debug.DrawLine(transform.position, transform.position + forward * height * 2, Color.blue);
    85.         Debug.DrawLine(transform.position, transform.position - Vector3.up * height, Color.green);
    86.     }
    87. }
    88.  

    Heres code for levitating;

    Code (CSharp):
    1. public IEnumerator moveObjectUP()
    2.     {
    3.         float totalMovementTime = 5f;
    4.         float currentMovementTime = 0f;
    5.         Vector3 pos = ball.transform.position;
    6.         currentposition = new Vector3(pos.x, pos.y, pos.z);
    7.         Vector3 Destination = new Vector3(pos.x, 1f, pos.z);
    8.         Vector3 Origin = new Vector3(pos.x, 0.1680522f, pos.z);
    9.         Debug.Log("1");
    10.         while (Vector3.Distance(ball.transform.localPosition, Destination) > 0)
    11.         {
    12.             ball.isKinematic = true;
    13.             currentMovementTime += Time.deltaTime;
    14.             ball.transform.localPosition = Vector3.Lerp(Origin, Destination, currentMovementTime / totalMovementTime);
    15.             currentposition = ball.transform.localPosition;
    16.             Debug.Log("2");
    17.             yield return null;
    18.  
    19.             Debug.Log("3");
    20.         }
    21.         if (Vector3.Distance(ball.transform.localPosition, Destination) == 0)
    22.         {
    23.  
    24.             Debug.Log("4");
    25.             ball.transform.localPosition = currentposition;
    26.            
    27.             keydirection = true;
    28.  
    29.  
    30.             yield return new WaitForSeconds(5f);
    31.             yield return StartCoroutine(moveObjectDOWN());
    32.         }
    33.  
    34.     }
    35.     public IEnumerator moveObjectDOWN()
    36.     {
    37.         float totalMovementTime = 5f;
    38.         float currentMovementTime = 0f;
    39.         Vector3 pos = transform.position;
    40.         currentposition = new Vector3(pos.x, pos.y, pos.z);
    41.         Vector3 Destination = new Vector3(pos.x, 0.1680522f, pos.z);
    42.         Vector3 Origin = new Vector3(pos.x, 1f, pos.z);
    43.         Debug.Log("7");
    44.         while (Vector3.Distance(ball.transform.localPosition, Destination) > 0)
    45.         {
    46.             ball.isKinematic = false;
    47.             keydirection = false;
    48.             currentMovementTime += Time.deltaTime;
    49.             ball.transform.localPosition = Vector3.Lerp(Origin, Destination, currentMovementTime / totalMovementTime);
    50.             currentposition = ball.transform.localPosition;
    51.            
    52.  
    53.  
    54.             yield return null;
    55.  
    56.             Debug.Log("8");
    57.         }
    58.         if (Vector3.Distance(ball.transform.localPosition, Destination) == 0)
    59.         {
    60.            
    61.             if (globPower.isGrounded == true)
    62.             {
    63.  
    64.                 ball.velocity = Vector3.zero;                                           //Movement na 0
    65.                 ball.angularVelocity = Vector3.zero;                                    //Rotacija na 0
    66.             }
    67.  
    68.             yield return new WaitForSeconds(1f);
    69.             globPower.isAbilityUsed = false;
    70.             yield return null;
    71.         }
    72.        
    73.  
    74.  
    75.  
    76.     }
     
  2. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14