Search Unity

Question Why does my player not properly fall off a rotating object?

Discussion in 'Scripting' started by AC67_Androi, Mar 1, 2023.

  1. AC67_Androi

    AC67_Androi

    Joined:
    Jan 3, 2023
    Posts:
    90
    I have a "wheel" type object that just spins in place. https://imgflip.com/gif/7cuiso (sorry for the poor quality) but when I jump on it, my player just glues to it which makes sense because I have it parented, but it refuses to continue moving upright and I just fall into the ground and then my player's rotation is permanently altered until I exit the runtime. This is similar to what I've encountered previously with a rigidbody where when I bump into things, I get knocked over and my rotation is altered but I fix it quickly by using rotation constraints. But I'm using a character controller for this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateCircle : MonoBehaviour
    5. {
    6.  
    7.     public GameObject Player;
    8.     public Vector3 RotateAmount;
    9.     public float speed = 2.0f;
    10.  
    11.     void Update()
    12.     {
    13.         transform.Rotate(RotateAmount * speed * Time.deltaTime);
    14.     }
    15.  
    16.     private void OnTriggerEnter(Collider other)
    17.     {
    18.         if (other.gameObject == Player)
    19.         {
    20.             Player.transform.parent = transform;
    21.         }
    22.     }
    23.  
    24.     private void OnTriggerExit(Collider other)
    25.     {
    26.         if (other.gameObject == Player)
    27.         {
    28.             Player.transform.parent = null;
    29.         }
    30.     }
    31. }
    Even without any parenting code, my player refuses to move. But with parenting, it just makes it even worse. Before I proceed with anything else like potentially using a raycast, all I'm trying to do at a bare minimum is simply get the movement at least somewhat working correctly.

    Is there even another way to get my player to move in the direction the wheel is spinning in?
     
    Last edited: Mar 1, 2023