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

Do you have a sprint script that, when you press a key, it runs;

Discussion in 'Scripting' started by FoxHound9001, Sep 11, 2020.

  1. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    I tried with my methods, but it didn't work;
     
  2. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    You can implement this in many ways. Could you show us your approach (which doesnt work as you said) so we can help you and see why it isn't working?
     
  3. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Cinemachine;


    public class PlayerFreeLook1 : MonoBehaviour
    {
    public CinemachineFreeLook freeLookCam;
    Rigidbody rb;

    public float force = 5;


    public float forceJump = 300;
    public bool isFloor = false;


    private void Start()

    {

    rb = GetComponent<Rigidbody>();

    }

    private void Update()

    {

    Ray ray = new Ray(transform.position, new Vector3(0, -1, 0));
    isFloor = Physics.Raycast(ray, 0.7f);
    Debug.DrawRay(ray.origin, ray.direction * 0.7f, Color.red);
    Vector3 camDirection = (transform.position - freeLookCam.transform.position).normalized;

    camDirection = new Vector3(camDirection.x, 0, camDirection.z);

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 right = Vector3.Cross(Vector3.up, camDirection);

    Vector3 direction = camDirection * z + right * x;
    rb.AddForce(direction * force * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.Space) && isFloor)

    {
    rb.AddForce(Vector3.up * forceJump);
    }
    }
    }
     
  4. ScamTheMan

    ScamTheMan

    Joined:
    Oct 4, 2018
    Posts:
    75
    Please, next time use code tags, it will be much easier for everyone to read.
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    I see that you are beginner and don't really know what tthis whole code means so I think you don't need to use Assets that are meant for complex applications. I think you should remove Cinemachine and first understand Unity's main camera. If you want to use 3rd person camera movement, watch this video, it's really easy to understand:


    and this for 1st person movement:


    But to answer your question, I will share you my(actually 95% is by Brackeys) primitive code for sprinting
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public CharacterController Player;
    6.     public float speed = 12f;
    7.     public float sprintSpeed = 24f;
    8.     public float jumpHeight = 0.5f;
    9.  
    10.     public Transform groundCheck;
    11.     public float groundDistance = 0.7f;
    12.     public LayerMask groundMask;
    13.  
    14.     public float gravity = -9.81f;
    15.     public Camera myCam;
    16.  
    17.     Vector3 velocity;
    18.     bool isGrounded;
    19.  
    20.     void Update()
    21.     {
    22.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    23.         if(isGrounded && velocity.y < 0)
    24.         {
    25.             velocity.y = -2f;
    26.         }
    27.  
    28.         float z = Input.GetAxis("Vertical");
    29.         float x = Input.GetAxis("Horizontal");
    30.  
    31.         Vector3 move = transform.right * x + transform.forward * z;
    32.  
    33.         if(Input.GetKey("left shift"))
    34.         {
    35.             Player.Move(move * sprintSpeed * Time.deltaTime);
    36.             myCam.fieldOfView += 0.2f;
    37.             if(myCam.fieldOfView >= 70)
    38.             {
    39.                 myCam.fieldOfView = 70;
    40.             }
    41.         }
    42.         else
    43.         {
    44.             Player.Move(move * speed * Time.deltaTime);
    45.             myCam.fieldOfView -= 0.2f;
    46.             if (myCam.fieldOfView <= 60)
    47.             {
    48.                 myCam.fieldOfView = 60;
    49.             }
    50.         }
    51.  
    52.         if(isGrounded && Input.GetButton("Jump"))
    53.         {
    54.             velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    55.         }
    56.         velocity.y += gravity * Time.deltaTime;
    57.  
    58.         Player.Move(velocity * Time.deltaTime);
    59.  
    60.     }
    61. }
    This is whole code for 1st person movement, spriniting and jumping
    If you want just the code for sprinting:

    Code (CSharp):
    1. if(Input.GetKey("left shift"))
    2.         {
    3.             Player.Move(move * sprintSpeed * Time.deltaTime);
    4.             myCam.fieldOfView += 0.2f;
    5.             if(myCam.fieldOfView >= 70)
    6.             {
    7.                 myCam.fieldOfView = 70;
    8.             }
    9.         }
    10.         else
    11.         {
    12.             Player.Move(move * speed * Time.deltaTime);
    13.             myCam.fieldOfView -= 0.2f;
    14.             if (myCam.fieldOfView <= 60)
    15.             {
    16.                 myCam.fieldOfView = 60;
    17.             }
    18.         }
    There's added an effect which raise the FOV when sprinting (just like in minecraft)
     
    FoxHound9001 likes this.
  5. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    Thank you, I will try to modify my code and improve it further;
     
  6. FoxHound9001

    FoxHound9001

    Joined:
    Sep 11, 2020
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class PlayerFreeLook1 : MonoBehaviour
    7. {
    8.     public CinemachineFreeLook freeLookCam;
    9.  
    10.     Rigidbody rb;
    11.     public float force = 5;
    12.  
    13.     public float forceJump = 300;
    14.     public bool isFloor = false;
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.     private void Update()
    21.     {
    22.         Ray ray = new Ray(transform.position, new Vector3(0, -1, 0));
    23.         isFloor = Physics.Raycast(ray, 0.7f);
    24.         Debug.DrawRay(ray.origin, ray.direction * 0.7f, Color.red);
    25.  
    26.         Vector3 camDirection = (transform.position - freeLookCam.transform.position).normalized;
    27.         camDirection = new Vector3(camDirection.x, 0, camDirection.z);
    28.  
    29.         float x = Input.GetAxis("Horizontal");
    30.         float z = Input.GetAxis("Vertical");
    31.  
    32.      
    33.         Vector3 right = Vector3.Cross(Vector3.up, camDirection);
    34.        
    35.         Vector3 direction = camDirection * z + right * x;
    36.        
    37.         rb.AddForce(direction * force * Time.deltaTime);
    38.  
    39.         if (Input.GetKeyDown(KeyCode.Space) && isFloor)
    40.         {
    41.             rb.AddForce(Vector3.up * forceJump);
    42.         }
    43.     }
    44.  
    45. }
    46.