Search Unity

Very Strange Movement..

Discussion in 'Scripting' started by Deleted User, Oct 23, 2014.

  1. Deleted User

    Deleted User

    Guest

    Ok, so I wanted to do a third person camera so I went over to 3dbuzz to play with their demo videos and basically make one line for line from their video.

    I got it all working. camera works pretty good but my problem is..
    The basic movement of the character controller is not working..
    I must admit.. the basics here are so simple so I am not even sure why its not working..

    So, I go about using their simple code..

    Code (CSharp):
    1.         var deadZone = 0.1f;
    2.         TP_Motor.Instance.MoveVector = Vector3.zero;
    3.  
    4.         float ForwardOrBackword = Input.GetAxis ("Vertical");
    5.         float RightOrLeft = Input.GetAxis ("Horizontal");
    6.  
    7.         //if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
    8.         TP_Motor.Instance.MoveVector += new Vector3(0f,0f,ForwardOrBackword);
    9.  
    10.         //if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
    11.         TP_Motor.Instance.MoveVector += new Vector3(RightOrLeft,0f,0f);
    12.  
    13.         if (MoveVector.x != 0 || MoveVector.z != 0)
    14.         {
    15.             transform.rotation =
    16.                 Quaternion.Euler(
    17.                     transform.eulerAngles.x,
    18.                     Camera.main.transform.eulerAngles.y,
    19.                     transform.eulerAngles.z);
    20.  
    21.         }
    22.  
    23.         //Transform MoveVecctor to WorldSpace
    24.         MoveVector = transform.TransformDirection(MoveVector);
    25.  
    26.         //Normalize MoveVector if Magnitude > 1
    27.         if (MoveVector.magnitude > 1)
    28.             MoveVector = Vector3.Normalize(MoveVector);
    29.  
    30.         //Multiple MoveVector by MoveSpeed
    31.         MoveVector *= MoveSpeed;
    32.  
    33.         //Convert Units/Frame to Units/Spec
    34.         //Multiply MoveVector by Delta Time..
    35.         MoveVector *= Time.deltaTime;
    36.  
    37.         //Move Character in WorldSpace.
    38.         TP_Controller.CharacterController.Move(MoveVector);
    39.  
    40.  
    I went ahead and slimmed it down some..
    But does anyone see any major issues with the script that stick out that I may be missing ?
     
  2. Parallaxe

    Parallaxe

    Joined:
    Apr 9, 2013
    Posts:
    118
    Hi shawndg

    It's a bit hard to tell, since your code looks a bit like a mixture between TP_Controller and TP_Motor. I also can't tell the context of the snippet. E.g. if the snippet is placed within Update() in TP_Controller. However, here are some thoughts, based on my own implementation (I did the same as you and went through the 3dbuzz tutorial (-: ):

    In my implementation the part TP_Motor.Instance.MoveVector += new Vector3(0f,0f,ForwardOrBackword); is in TP_Controller and within Update(). But I don't work with variables (like "ForwardOrBackword") but rather with Input.GetAxis("Vertical")

    I have no such thing as in lines 13 - 21, so I can't give you any recommendation on that (-;

    Lines 23-38 look (quite) the same as in my code in TP_Motor. Only difference is that I have applied gravity and vertical velocity. But that won't make any difference to your problem.

    Therefore, I'd guess that the issue is somewhere in lines 4 - 11. However, depending in which context (Update() etc.) you are using them.

    Hope it helps (-:
     
  3. Deleted User

    Deleted User

    Guest

    Ok, I went back to what I think is the original code based off the tutorial and problem is still occurring.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TP_Controller : MonoBehaviour {
    5.  
    6.     public static CharacterController CharacterController;
    7.     public static TP_Controller Instance;
    8.  
    9.     void Awake()
    10.     {
    11.         CharacterController = GetComponent("CharacterController") as CharacterController;
    12.         Instance = this;
    13.         TP_Camera.UseExistingOrCreateNewMainCamera();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         //if (Camera.main == null)
    19.                         //return;
    20.  
    21.         GetLocomotionInput();
    22.  
    23.         TP_Motor.Instance.UpdateMotor ();
    24.  
    25.     }
    26.  
    27.     void GetLocomotionInput()
    28.     {
    29.         var deadZone = 0.1f;
    30.         TP_Motor.Instance.MoveVector = Vector3.zero;
    31.  
    32.         if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
    33.         TP_Motor.Instance.MoveVector += new Vector3(0f,0f,Input.GetAxis ("Vertical"));
    34.  
    35.         if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
    36.         TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis ("Horizontal"),0f,0f);
    37.  
    38.     }
    39. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TP_Motor : MonoBehaviour {
    5.  
    6.     public static TP_Motor Instance;
    7.     public float MoveSpeed = 10f;
    8.     public Vector3 MoveVector { get; set; }
    9.  
    10.  
    11.     void Awake ()
    12.     {
    13.         Instance = this;
    14.  
    15.     }
    16.  
    17.     public void UpdateMotor ()
    18.     {
    19.         SnapAlignCharacterWithCamera();
    20.         ProcessMotion ();
    21.     }
    22.  
    23.     void ProcessMotion()
    24.     {
    25.         //Transform MoveVecctor to WorldSpace
    26.         MoveVector = transform.TransformDirection(MoveVector);
    27.  
    28.         //Normalize MoveVector if Magnitude > 1
    29.         if (MoveVector.magnitude > 1)
    30.             MoveVector = Vector3.Normalize(MoveVector);
    31.  
    32.         //Multiple MoveVector by MoveSpeed
    33.         MoveVector *= MoveSpeed;
    34.  
    35.         //Convert Units/Frame to Units/Spec
    36.         //Multiply MoveVector by Delta Time..
    37.         MoveVector *= Time.deltaTime;
    38.  
    39.         //Move Character in WorldSpace.
    40.         TP_Controller.CharacterController.Move(MoveVector);
    41.     }
    42.  
    43.     void SnapAlignCharacterWithCamera()
    44.     {
    45.         if (MoveVector.x != 0 || MoveVector.z != 0)
    46.         {
    47.             transform.rotation =
    48.                 Quaternion.Euler(
    49.                     transform.eulerAngles.x,
    50.                     Camera.main.transform.eulerAngles.y,
    51.                     transform.eulerAngles.z);
    52.  
    53.         }
    54.  
    55.     }
    56.  
    57.  
    58. }
    59.  
    Camera is pretty decent though, but movement is just all over the place..
    And the logic makes perfect sense so I am wondering if maybe its my character or input settings..
     
  4. Deleted User

    Deleted User

    Guest

    Still having problems here, I rewrote most of this and the problem is the same..
    I cant click w or up to move forward... I got all other directions working properly.. very confused now..
     
  5. Deleted User

    Deleted User

    Guest

    I got it all working...

    The problem was my player object, I had a child on it that had a collider on it and it was colliding with my parent object whenever I tried to move forward...

    OMG that was a pain to figure it out..
    Posting this so if anyone else runs into this odd issue,..
     
  6. Parallaxe

    Parallaxe

    Joined:
    Apr 9, 2013
    Posts:
    118
    Glad you could resolve it. Thanks for sharing (-: