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

Can I make a character controller rotate with the player?

Discussion in 'Scripting' started by Draconic, Jul 6, 2014.

  1. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    The reason I am using a character controller is because the physics system I made was very glitchy (you could go through corners of walls and couldn't walk up slopes). The game is a 2D scroller. Changing the direction of gravity is a big part of the game, but the character controller doesn't rotate with the character when gravity is changed. How can I do this?

    Is there any way I could not use the character controller? Rigidbodies probably wouldn't work because I do not want the character to rotate on it's own.

    edit: I can already change the character controller y, but I need to change it's x and z rotation.
     
  2. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    You can't rotate it. While I was making a platformer I also had to rewrite few times changing from character controller (which has limited features) and rigid body (which is glitchy). This is what I wrote at the end:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RigidbodyController : MonoBehaviour {
    5.  
    6.     const float g = 40f;
    7.  
    8.     public float speed = 6f;
    9.  
    10.     const float jumpHeight = 2f;
    11.     static float jumpVelocilty = Mathf.Sqrt(2f*g*jumpHeight);
    12.  
    13.     bool is_grounded;
    14.  
    15.     [System.NonSerialized]
    16.     public Vector3 lastGroundPos;
    17.  
    18.     const float airAcceleration = 10f;
    19.     bool groundFrame;
    20.  
    21.     new Rigidbody rigidbody;
    22.     new Transform transform;
    23.  
    24.  
    25.     // IF OBJECT IS TOUCHING THE GROUND
    26.     public bool isGrounded{
    27.         get{
    28.             return is_grounded;  
    29.         }
    30.     }
    31.  
    32.     // VELOCITY OF OBJECT
    33.     public Vector3 velocity{
    34.         get{
    35.             return rigidbody.velocity;
    36.         }
    37.     }
    38.  
    39.     // LOOK AT POSITIVE OR NEGATIVE X
    40.     public void LookAt(float x){
    41.         if(x>0f){
    42.             transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.right);
    43.         }else if(x<0f){
    44.             transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.left);
    45.         }
    46.     }
    47.  
    48.     // WHERE IS OBJECT LOOKING
    49.     public float orientation{
    50.         get{
    51.             return Mathf.Sign(transform.forward.x);
    52.         }
    53.         set{
    54.             LookAt(value);
    55.         }
    56.     }
    57.  
    58.     float targetXVel;
    59.  
    60.     // OBJECT WILL TRY TO MOVE WITH CONSTANT speed
    61.     public void SetSpeed(float speed){
    62.         targetXVel = speed;
    63.     }
    64.  
    65.  
    66.  
    67.     bool canJump;
    68.     public bool Jump(){
    69.         bool jumped = canJump;
    70.         if(canJump){
    71.             Vector3 velocity = rigidbody.velocity;
    72.             velocity.y = jumpVelocilty;
    73.             rigidbody.velocity = velocity;  
    74.             is_grounded = false;
    75.             groundFrame = false;
    76.             canJump = false;
    77.         }
    78.         return jumped;
    79.     }
    80.  
    81.  
    82.     public void FixedUpdate(){
    83.  
    84.         is_grounded = groundFrame;
    85.         groundFrame = false;
    86.         if(!is_grounded && transform.parent != null){
    87.             transform.parent = null;
    88.         }
    89.  
    90.         float xChange;
    91.         xChange  =  targetXVel*speed - rigidbody.velocity.x ;
    92.  
    93.         LookAt(targetXVel);
    94.  
    95.         // If we are in the air acceleration is applied insted of instant change in velocity
    96.         if(!isGrounded){
    97.             xChange *= airAcceleration*Time.deltaTime;
    98.         }
    99.  
    100.         Vector3 velocityChange = new Vector3 ( xChange , 0f , 0f ) ;
    101.         Vector3 vel = rigidbody.velocity + velocityChange;
    102.  
    103.         vel.z = isGrounded ? -5f * transform.position.z : 0f;
    104.  
    105.         rigidbody.velocity = vel;
    106.  
    107.         Vector3 myGravity = new Vector3( 0f , -g , 0f );
    108.         rigidbody.AddForce(myGravity, ForceMode.Acceleration);
    109.  
    110.      
    111.     }
    112.  
    113.     public void SetLastGround(){
    114.         lastGroundPos = transform.position;
    115.     }
    116.  
    117.  
    118.     void OnCollisionStay(Collision collision){
    119.  
    120.         if(collision.contacts.Length == 0) return;
    121.  
    122.  
    123.  
    124.         Side side = MyMath.GetSide(collision.contacts[0].normal);
    125.         if(side == Side.Up && rigidbody.velocity.y <= .8f){
    126.             SetLastGround();
    127.             Transform otherTransform = collision.collider.transform;
    128.             if(collision.gameObject.tag == Tag.Moving){
    129.                 transform.parent = otherTransform;
    130.             }else if(otherTransform.parent != null && otherTransform.parent.tag == Tag.Moving){
    131.                 transform.parent = otherTransform.parent;
    132.             }
    133.          
    134.             groundFrame = true;
    135.             is_grounded = true;
    136.             canJump = true;
    137.         }
    138.     }
    139.  
    140.     void Awake(){
    141.         rigidbody = base.rigidbody;
    142.         transform = base.transform;
    143.     }
    144.  
    145.     public void Start(){
    146.         SetLastGround();
    147.         is_grounded = false;
    148.         groundFrame = false;
    149.         canJump = false;
    150.         targetXVel = 0f;
    151.         transform.parent = null;
    152.     }
    153.  
    154.  
    155. }
    156.  
    Maybe you can modify it for your project. Click try link in my signature to see how it works.
     
  3. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    Thank you! I am trying to implement something like this now. Nice game
     
  4. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Thanks. If you don't have moving platforms you can delete all code that is used to set parent of transform.