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

Resolved problem with Charactercontroller

Discussion in 'Scripting' started by Fuocartex, Jun 2, 2020.

  1. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    I wrote this script for the Charactercontroller of a simple cube but when I start the game, in addition to rotating it does absolutely nothing and I don't know why. you may find if there is any problem thanks.
    Code (CSharp):
    1. using System.Collections;
    2. //using System.Collections.Generic;
    3. //using System.Security.Cryptography;
    4. using UnityEngine;
    5.  
    6. public class Charactercontroller : MonoBehaviour
    7. {
    8.  
    9.     [System.Serializable]
    10.     public class MoveSettings
    11.     {
    12.      
    13.         public float rotateVel = 100;
    14.         public float forwardVel = 12;
    15.         public float jumpVel = 25;
    16.         public float distToGrounded = 0.1f;
    17.         public LayerMask ground;
    18.     }
    19.     [System.Serializable]
    20.     public class PhysSettings
    21.     {
    22.         public float downAccel = 0.75f;
    23.     }
    24.     [System.Serializable]
    25.     public class InputSettings
    26.     {
    27.         public float inputDelay = 0.1f;
    28.         public string FORWARD_AXIS = "Vertical";
    29.         public string TURN_AXIS = "Horizontal";
    30.         public string JUMP_AXIS = "Jump";
    31.     }
    32.  
    33.     public MoveSettings moveSetting = new MoveSettings();
    34.     public PhysSettings physSetting = new PhysSettings();
    35.     public InputSettings inputSetting = new InputSettings();
    36.  
    37.     Vector3 velocity = Vector3.zero;
    38.     Quaternion targetRotation;
    39.     Rigidbody rBody;
    40.     float forwardInput, turnInput, jumpInput;
    41.     public Quaternion TargetRotation
    42.     {
    43.         get { return targetRotation; }
    44.     }
    45.  
    46.     bool Grounded()
    47.     {
    48.         return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
    49.     }
    50.     void Start()
    51.     {
    52.         targetRotation = transform.rotation;
    53.         if (GetComponent<Rigidbody>())
    54.             rBody = GetComponent<Rigidbody>();
    55.         else
    56.         { Debug.LogError("the character needs rigidbody"); }
    57.         forwardInput = 0;
    58.         turnInput = 0;
    59.         jumpInput = 0;
    60.     }
    61.  
    62.     void GetInput()
    63.     {
    64.         forwardInput = Input.GetAxis(inputSetting.FORWARD_AXIS);
    65.         turnInput = Input.GetAxis(inputSetting.TURN_AXIS);
    66.         jumpInput = Input.GetAxisRaw(inputSetting.JUMP_AXIS);
    67.     }
    68.  
    69.     void Update()
    70.     {
    71.         GetInput();
    72.         Turn();
    73.     }
    74.  
    75.     void FixedUpdate()
    76.     {
    77.         Run();
    78.         Jump();
    79.  
    80.         //rBody.velocity = transform.TransformDirection(velocity);
    81.         rBody.velocity = velocity;
    82.      
    83.     }
    84.  
    85.      void Run()
    86.     {
    87.         if(Mathf.Abs(forwardInput)> inputSetting.inputDelay)
    88.         {
    89.             //move rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
    90.  
    91.             velocity.z = moveSetting.forwardVel * forwardInput;
    92.         }
    93.         else
    94.         {
    95.             //  rBody.velocity = Vector3.zero;
    96.             velocity.z = 0;
    97.         }
    98.     }
    99.  
    100.     void Turn()
    101.     {
    102.         if (Mathf.Abs(turnInput) > inputSetting.inputDelay)
    103.         {
    104.             targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * turnInput * Time.deltaTime, Vector3.up);
    105.         }
    106.         transform.rotation = targetRotation;
    107.     }
    108.     void Jump()
    109.     {
    110.         if(jumpInput > 0 && Grounded())
    111.         {
    112.             //jump
    113.             velocity.y = moveSetting.jumpVel;
    114.         }
    115.         else if(jumpInput == 0 && Grounded())
    116.         {
    117.             //zero out our velocity.y
    118.             velocity.y = 0;
    119.         }
    120.         else
    121.         {
    122.             //cedreas velocity.y
    123.             velocity.y -= physSetting.downAccel;
    124.         }
    125.              
    126.     }
    127. }
    128.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    What is your MoveSettings.forwardVel set to in the inspector?
     
  3. Fuocartex

    Fuocartex

    Joined:
    Jun 2, 2020
    Posts:
    13
    Cattura.PNG

    if I set it to 200 and I don't click anything else before (wasd) it moves a little then it stops




    if I write the code so the cube moves in all directions normally but, of course, it doesn't jump

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class prova : MonoBehaviour
    6. {
    7.  
    8.     [System.Serializable]
    9.     public class MoveSettings
    10.     {
    11.  
    12.         public float rotateVel = 100;
    13.         public float forwardVel = 12;
    14.         //public float jumpVel = 25;
    15.         //public float distToGrounded = 0.1f;
    16.         //public LayerMask ground;
    17.     }
    18.     [System.Serializable]
    19.     public class PhysSettings
    20.     {
    21.         //public float downAccel = 0.75f;
    22.     }
    23.     [System.Serializable]
    24.     public class InputSettings
    25.     {
    26.         public float inputDelay = 0.1f;
    27.         //public string FORWARD_AXIS = "Vertical";
    28.         //public string TURN_AXIS = "Horizontal";
    29.         //public string JUMP_AXIS = "Jump";
    30.     }
    31.  
    32.     public MoveSettings moveSetting = new MoveSettings();
    33.     public PhysSettings physSetting = new PhysSettings();
    34.     public InputSettings inputSetting = new InputSettings();
    35.  
    36.     //Vector3 velocity = Vector3.zero;
    37.     Quaternion targetRotation;
    38.     Rigidbody rBody;
    39.     float forwardInput, turnInput /*,jumpInput*/;
    40.     public Quaternion TargetRotation
    41.     {
    42.         get { return targetRotation; }
    43.     }
    44.  
    45.     /*bool Grounded()
    46.     {
    47.         return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
    48.     }*/
    49.     void Start()
    50.     {
    51.         targetRotation = transform.rotation;
    52.         if (GetComponent<Rigidbody>())
    53.             rBody = GetComponent<Rigidbody>();
    54.         else
    55.         { Debug.LogError("the character needs rigidbody"); }
    56.         forwardInput = 0;
    57.         turnInput = 0;
    58.         //jumpInput = 0;
    59.     }
    60.  
    61.     void GetInput()
    62.     {
    63.         forwardInput = Input.GetAxis(/*inputSetting.FORWARD_AXIS*/"Vertical");
    64.         turnInput = Input.GetAxis(/*inputSetting.TURN_AXIS*/ "Horizontal");
    65.         //jumpInput = Input.GetAxisRaw(inputSetting.JUMP_AXIS);
    66.     }
    67.  
    68.     void Update()
    69.     {
    70.         GetInput();
    71.         Turn();
    72.     }
    73.  
    74.     void FixedUpdate()
    75.     {
    76.         Run();
    77.         //Jump();
    78.  
    79.         //rBody.velocity = transform.TransformDirection(velocity);
    80.         //rBody.velocity = velocity;
    81.  
    82.     }
    83.  
    84.     void Run()
    85.     {
    86.         if (Mathf.Abs(forwardInput) > inputSetting.inputDelay)
    87.         {
    88.             //move rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
    89.  
    90.             //velocity.z = moveSetting.forwardVel * forwardInput;
    91.             rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
    92.         }
    93.         else
    94.         {
    95.             //  rBody.velocity = Vector3.zero;
    96.             //velocity.z = 0;
    97.             rBody.velocity = Vector3.zero;
    98.         }
    99.     }
    100.  
    101.     void Turn()
    102.     {
    103.         if (Mathf.Abs(turnInput) > inputSetting.inputDelay)
    104.         {
    105.             targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * turnInput * Time.deltaTime, Vector3.up);
    106.         }
    107.         transform.rotation = targetRotation;
    108.     }
    109.     /*void Jump()
    110.     {
    111.         if (jumpInput > 0 && Grounded())
    112.        {
    113.             //jump
    114.             velocity.y = moveSetting.jumpVel;
    115.         }
    116.         else if (jumpInput == 0 && Grounded())
    117.         {
    118.             //zero out our velocity.y
    119.             velocity.y = 0;
    120.         }
    121.         else
    122.         {
    123.             //cedreas velocity.y
    124.             velocity.y -= physSetting.downAccel;
    125.         }
    126.  
    127.     }*/
    128. }
    129.  
     
    Last edited: Jun 2, 2020