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

I Need Help please, i need to make Player WallRun

Discussion in 'Scripting' started by abdelrahmanyoussry509, Apr 28, 2020.

  1. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    hello i am new to unity so i suck with creating code from my mind (90% of the time it does not work) so here is my attempt

    -i tried to create an empty game object ,put it at the players shoulder and parent it to my player then create a raycast in that Empty object so when it detects the wall and player holding space it forces player up (using vector3) but problem is it does not work properly sometimes it does its job and then sometimes it does not and it is also very clunky and does not feel right just makes play go up
    (so i scraped the entire thing)

    -so i was hoping anyone would help me figure out how do i make this player wall run properly and please explain it to me as simple as possible since i am new to unity and not very intelligent , i would highly appreciate that. :D
    (i watched a some tutorials but my code is very different and uses a physics controller)


    Here is the motion script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Motion : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody rig;
    9.     public float sprintmodifire;
    10.     public Camera PlayerCam;
    11.  
    12.     public Transform groundchek;
    13.     public LayerMask groundmask;
    14.     private float t_adjustedSpeed;
    15.  
    16.     public float jumpforce;
    17.     private float BaseFOV;
    18.     private float FOVmodifre = 1.2f;
    19.  
    20.  
    21.     private float Lastrun;
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         rig = GetComponent<Rigidbody>();
    27.         BaseFOV = PlayerCam.fieldOfView;
    28.     }
    29.  
    30.     public IEnumerator GrapplePush()
    31.     {
    32.         rig.AddRelativeForce((Vector3.forward * t_adjustedSpeed / 7));
    33.         yield return new WaitForSeconds(1f);
    34.         FindObjectOfType<GrapplingGunScript>().isGrapling = false;
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void FixedUpdate()
    40.     {
    41.  
    42.      
    43.  
    44.         var grappler = FindObjectOfType<GrapplingGunScript>();
    45.         bool isGorunded;
    46.         isGorunded = Physics.Raycast(groundchek.position, Vector3.down, 0.1f, groundmask);
    47.         //Axis and motion
    48.         float t_Hmove = Input.GetAxis("Horizontal");
    49.         float t_Vmove = Input.GetAxis("Vertical");
    50.         //we save them both in a vector 3
    51.         Vector3 t_direction = new Vector3(  t_Hmove,0, t_Vmove);
    52.         // when we press w and d at same time we add 1from w on 1 from d so it makes player go faster so we nomlize (keep it 1)
    53.         t_direction.Normalize();
    54.      
    55.         //Controls
    56.         bool spirnt = Input.GetKey(KeyCode.LeftShift);
    57.         bool jump = Input.GetKey(KeyCode.Space);
    58.  
    59.         //motion and sprint and camera lerping
    60.  
    61.         bool isjumping = jump;
    62.         bool isSprinting = spirnt && t_Vmove >0 && !isjumping && isGorunded;
    63.  
    64.         if (isjumping && isGorunded )
    65.         {
    66.             rig.AddForce( Vector3.up * jumpforce);
    67.         }
    68.          t_adjustedSpeed = speed;
    69.      
    70.  
    71.  
    72.      
    73.  
    74.  
    75.         if (isSprinting)
    76.         {
    77.             t_adjustedSpeed = sprintmodifire * t_adjustedSpeed;
    78.          
    79.         }
    80.         if (grappler == null || !grappler.isGrapling)
    81.         {
    82.             Vector3 TargetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
    83.             TargetVelocity.y = rig.velocity.y;
    84.             rig.velocity = TargetVelocity;
    85.          
    86.         }
    87.         else if(grappler != null && grappler.isGrapling)
    88.         {
    89.  
    90.             StartCoroutine(GrapplePush());
    91.          
    92.          
    93.         }
    94.      
    95.      
    96.         if (isSprinting)
    97.         {
    98.             PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView,BaseFOV *FOVmodifre,Time.deltaTime*8f);
    99.         }
    100.         else
    101.         {
    102.             PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView, BaseFOV , Time.deltaTime * 8f);
    103.         }
    104.  
    105.      
    106.      
    107.     }
    108.  
    109.  
    110. }
    111.  
    and here is the looking around script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Look : MonoBehaviour
    6. {
    7.     public float MouseSenstivity;
    8.     public Transform PlayerBody;
    9.     private float rotationY;
    10.     public Transform weapon;
    11.     void Start()
    12.     {
    13.         Cursor.lockState = CursorLockMode.Locked;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.      float MouseX = Input.GetAxis("Mouse X") * MouseSenstivity * Time.deltaTime;
    20.      float MouseY = Input.GetAxis("Mouse Y") * MouseSenstivity * Time.deltaTime;
    21.      PlayerBody.Rotate(Vector3.up * MouseX);
    22.  
    23.      rotationY = rotationY - MouseY;
    24.      rotationY = Mathf.Clamp(rotationY, -90f, 90f);
    25.      
    26.         transform.localRotation = Quaternion.Euler(rotationY, 0f, 0f);
    27.         weapon.localRotation = Quaternion.Euler(rotationY / 10f, 0f, 0f);
    28.  
    29.  
    30.  
    31.     }
    32. }
    33.  
    oh and please ignore the grapple codes cuse that is for diffrent script so dont mind it
     
    Last edited: Apr 29, 2020
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    So first of all:
    on the second script, I would fix your indentations (just a good rule of thumb) :)
    only use FixedUpdate for physics-based scripting.

    Second, I don't see any code for the wall run...
    if you are wondering I would point you towards this video:
     
  3. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    Thank you it was the video i am searching for
     
    sbalanoff likes this.