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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

New to coding please help.

Discussion in 'Scripting' started by austinmoody247, Dec 11, 2019.

  1. austinmoody247

    austinmoody247

    Joined:
    Dec 5, 2019
    Posts:
    3
    I am trying to add a sprint while running at a 45 degree angle but I cant get it to work, I can sprint forward and used this code else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift))
    {
    Debug.Log("hello");
    speed = sprintspeed;
    movedir = new Vector3(0, 0, 1);
    movedir = movedir * speed + movedirside * speed;
    movedir = transform.TransformDirection(movedir);
    }
    to attempt to get it to work, the debug.log func never played either.
     
  2. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    We need more of the code in the class to be able to say what's wrong. Use the code-tag when you post it, so it's easier to read.
     
  3. austinmoody247

    austinmoody247

    Joined:
    Dec 5, 2019
    Posts:
    3
    So I got it to work but im not sure if my coding is "correct" in how I wrote it.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class movement : MonoBehaviour
    6. {
    7.     public float speed = 4f;
    8.     public CharacterController Controller;
    9.     public float rotspeed = 80f;
    10.     public float gravity = 14f;
    11.     public CharacterController controller;
    12.     public Animator anim;
    13.     public float rot;
    14.     public Vector3 movedir = Vector3.zero;
    15.     public float backwardsSpeed = 2f;
    16.     public Vector3 movedirside = Vector3.left;
    17.     public float sprintspeed = 12f;
    18.     public AnimationCurve jumpfalloff;
    19.     public float jumpmultiplier;
    20.     public KeyCode jumpkey;
    21.     public float sidespeed = 5f;
    22.  
    23.     private bool isjumping;
    24.  
    25.  
    26.     void Start()
    27.     {
    28.    
    29.         controller = GetComponent<CharacterController>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.  
    36.  
    37.  
    38.         movedir.y -= gravity * Time.deltaTime;
    39.          controller.Move(movedir * Time.deltaTime);
    40.  
    41.  
    42.        
    43.  
    44.  
    45.         if (controller.isGrounded)
    46.         {
    47.             movedir = new Vector3(0, 0, 0);
    48.             if (Input.GetKey(KeyCode.LeftShift))
    49.             {
    50.                 speed = sprintspeed;
    51.             }
    52.             else if (Input.GetKeyUp(KeyCode.LeftShift))
    53.             {
    54.                 speed = 5f;
    55.             }
    56.            
    57.          
    58.            if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
    59.             {
    60.                 movedir = new Vector3(0, 0, 1);
    61.                 movedir = movedir * speed + movedirside * speed;
    62.                 movedir = transform.TransformDirection(movedir);
    63.             }
    64.  
    65.  
    66.  
    67.             else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
    68.             {
    69.                 movedir = new Vector3(0, 0, 1);
    70.                 movedir = movedir * speed - movedirside * speed;
    71.                 movedir = transform.TransformDirection(movedir);
    72.             }
    73.          else if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
    74.             {
    75.                 movedir = new Vector3(0, 0, 1);
    76.                 movedir = movedir * speed;
    77.                 movedir = transform.TransformDirection(movedir);
    78.             }
    79.        
    80.            
    81.             else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
    82.             {
    83.                 movedir = new Vector3(0, 0, -1);
    84.                 movedir = movedir * backwardsSpeed + movedirside * sidespeed;
    85.                 movedir = transform.TransformDirection(movedir);
    86.             }
    87.             else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
    88.             {
    89.                 movedir = new Vector3(0, 0, 1);
    90.                 movedir = -movedir * backwardsSpeed - movedirside * sidespeed;
    91.                 movedir = transform.TransformDirection(movedir);
    92.             }
    93.             else if (Input.GetKey("w"))
    94.             {
    95.  
    96.  
    97.                 movedir = new Vector3(0, 0, 1);
    98.                 movedir = movedir * speed;
    99.                 movedir = transform.TransformDirection(movedir);
    100.             }
    101.             else if (Input.GetKeyUp("w"))
    102.             {
    103.                 movedir = new Vector3(0, 0, 0);
    104.             }
    105.  
    106.  
    107.  
    108.  
    109.  
    110.             else if (Input.GetKey("s"))
    111.             {
    112.                 ;
    113.                 movedir = new Vector3(0, 0, -1);
    114.                 movedir = movedir * backwardsSpeed;
    115.                 movedir = transform.TransformDirection(movedir);
    116.             }
    117.  
    118.             else if (Input.GetKeyUp("s"))
    119.             {
    120.  
    121.                 movedir = new Vector3(0, 0, 0);
    122.             }
    123.             else if (Input.GetKey("d"))
    124.             {
    125.                 movedir = new Vector3(1, 0, 0);
    126.                 movedir = movedir * sidespeed;
    127.                 movedir = transform.TransformDirection(movedir);
    128.             }
    129.             else if (Input.GetKeyUp("d"))
    130.             {
    131.                 movedir = new Vector3(0, 0, 0);
    132.             }
    133.  
    134.             else if (Input.GetKey("a"))
    135.             {
    136.                 movedir = new Vector3(-1, 0, 0);
    137.                 movedir = movedir * sidespeed;
    138.                 movedir = transform.TransformDirection(movedir);
    139.             }
    140.             else if (Input.GetKeyUp("a"))
    141.             {
    142.                 movedir = new Vector3(0, 0, 0);
    143.  
    144.  
    145.             }
    146.  
    147.             if (Input.GetKeyDown(jumpkey) && !isjumping)
    148.             {
    149.                 isjumping = true;
    150.                 StartCoroutine(JumpEvent());
    151.             }
    152.  
    153.              IEnumerator JumpEvent()
    154.             {
    155.                 controller.slopeLimit = 90.00f;
    156.                 float timeInAir = 0.0f;
    157.                 do
    158.                 {
    159.                     float jumpforce = jumpfalloff.Evaluate(timeInAir);
    160.                     controller.Move(Vector3.up * jumpforce * jumpmultiplier * Time.deltaTime);
    161.                     timeInAir += Time.deltaTime;
    162.                     yield return null;
    163.  
    164.                 } while (!controller.isGrounded && controller.collisionFlags != CollisionFlags.Above);
    165.                 isjumping = false;
    166.                 movedir = new Vector3(0, 0, 0);
    167.                 speed = 5f;
    168.                 controller.slopeLimit = 60.00f;
    169.             }
    170.  
    171.  
    172.         }
    173.      
    174.  
    175. }
    176.    
    177. }