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

Why is this happening (in air rotations)

Discussion in 'Scripting' started by Michael_93, Jul 2, 2014.

  1. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    So this code is near perfect for my goal however there is one bug that I can't figure out and it's really strange.
    When I'm in the air and holding either a or d on the ground it works fine. When In the air a few different things happen. If I jump then rotate the player immediately I completely stop as of hitting a wall after moving 360 degrees. If I wait a bit and jump high enough I appear to 'clip' the invisible wall. It's very hard to explain if you can just copy and paste this code into an empty scene and make the camera a child of the player you can see what I mean.

    I believe the problem is that the Input doesn't know I'm holding down a key when I 'hit the wall'. The thing is if this is true shouldn't I get stuck on the ground and on different height levels?

    If anyone has a better idea how I can achieve my movement goals please let me know.

    The camera is a child of the Player, I edited the input manager so gravity now = 0, and sensitivity now = 1.


    Code (CSharp):
    1.     void FixedUpdate () {
    2.      
    3.     using UnityEngine;
    4.     using System.Collections;
    5.  
    6.     public class tst : MonoBehaviour {
    7.     public float turnAirSpeed;
    8.     public float turnGroundSpeed;
    9.     public float jumpSpeed;
    10.     public float gravity;
    11.     public float constantSpeed;//5
    12.     private Vector3 moveDirection= Vector3.zero;
    13.     public float tiltAngle;
    14.  
    15.     private float tiltAroundZ;
    16.  
    17.         void Update () {
    18.         CharacterController controller = GetComponent<CharacterController>();
    19.         if (controller.isGrounded) {
    20.             if (Input.GetButton ("Jump"))
    21.             moveDirection.y = jumpSpeed;
    22.         }
    23.         tiltAroundZ = (Input.GetAxis ("Horizontal")) * tiltAngle;
    24.         if (Input.GetAxis ("Horizontal") == 1 || Input.GetAxis ("Horizontal") == -1) {
    25.                         Input.ResetInputAxes ();
    26.                 }
    27.  
    28.         transform.rotation = Quaternion.AngleAxis (tiltAroundZ, Vector3.up);    
    29.         moveDirection.y -= gravity*Time.deltaTime;
    30.  
    31.         controller.Move(moveDirection*Time.deltaTime);
    32.          
    33.         transform.Translate ((Vector3.forward*Time.deltaTime)*constantSpeed);
    34.         Debug.DrawRay(transform.position, Vector3.forward, Color.red,3.0F);
    35.     }
    36.     }
    Thank you for any help
    ps Happy Canada Day
     
    Last edited: Jul 4, 2014
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Cant be certain this will fix your problem, but you need to move any Input.(f) read functions into Update.
     
  3. Michael_93

    Michael_93

    Joined:
    Jan 12, 2014
    Posts:
    95
    thanks for the reply but it didn't change a thing.
    (I also updated the code)
     
    Last edited: Jul 4, 2014