Search Unity

Resolved Invalid term 'if'

Discussion in 'Scripting' started by Krakelin, Jan 25, 2021.

  1. Krakelin

    Krakelin

    Joined:
    Jan 21, 2021
    Posts:
    4
    Hi so i was using the code of Dani about movement (
    )
    and I tried to implement wallrunning! (
    )
    but I get the error Invalid expression term 'if'

    here's the sript

    Code (CSharp):
    1. // Some stupid rigidbody based movement by Dani
    2.  
    3. using System;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.  
    9.     //Wallrun variable
    10.     public LayerMask whatIsWall;
    11.     public float wallrunForce, maxWallrunTime, maxWallSpeed;
    12.     bool isWallRight, isWallLeft;
    13.     bool isWallRunning;
    14.     public float maxWallRunCameraTilt, wallRunCameraTilt;
    15.  
    16.  
    17.     private void WallRunInput()
    18.     {
    19.         if (Input.GetKey(KeyCode.D) && isWallRight) StartWallRun();
    20.         if (Input.GetKey(KeyCode.A) && isWallLeft) StartWallRun();
    21.     }
    22.  
    23.     private void StartWallRun()
    24.     {
    25.         rb.useGravity = false;
    26.         isWallRunning = true;
    27.  
    28.         if (rb.velocity.magnitude <= maxWallSpeed)
    29.         {
    30.             rb.AddForce(orientation.forward * wallrunForce * Time.deltaTime);
    31.  
    32.             //make sure that the Player stick to wall
    33.             if (isWallRight)
    34.                 rb.AddForce(orientation.right * wallrunForce / 5 * Time.Deltatime);
    35.             else
    36.                 rb.AddForce(-orientation.right * wallrunForce / 5 * Time.Deltatime);
    37.         }
    38.     }
    39.  
    40.     private void StopWallRun()
    41.     {
    42.         rb.useGravity = true;
    43.         isWallRunning = false;
    44.  
    45.     }
    46.     private void CheckForWall()
    47.     {
    48.         isWallRight = Physics.Raycast(transform.position, orientation.right, if, whatIsWall);
    49.         isWallLeft = Physics.Raycast(transform.position, -orientation.right, if, whatIsWall);
    50.  
    51.         //Leave the wallrun
    52.         if (!isWallLeft && !isWallRight) StopWallRun();
    53.  
    54.     }
    55.  
    56.     //Assingables
    57.     public Transform playerCam;
    58.     public Transform orientation;
    59.  
    60.     //Other
    61.     private Rigidbody rb;
    62.  
    63.     //Rotation and look
    64.     private float xRotation;
    65.     private float sensitivity = 50f;
    66.     private float sensMultiplier = 1f;
    67.  
    68.     //Movement
    69.     public float moveSpeed = 4500;
    70.     public float maxSpeed = 20;
    71.     public bool grounded;
    72.     public LayerMask whatIsGround;
    73.  
    74.     public float counterMovement = 0.175f;
    75.     private float threshold = 0.01f;
    76.     public float maxSlopeAngle = 35f;
    77.  
    78.     //Crouch & Slide
    79.     private Vector3 crouchScale = new Vector3(1, 0.5f, 1);
    80.     private Vector3 playerScale;
    81.     public float slideForce = 400;
    82.     public float slideCounterMovement = 0.2f;
    83.  
    84.     //Jumping
    85.     private bool readyToJump = true;
    86.     private float jumpCooldown = 0.25f;
    87.     public float jumpForce = 550f;
    88.  
    89.     //Input
    90.     float x, y;
    91.     bool jumping, sprinting, crouching;
    92.  
    93.     //Sliding
    94.     private Vector3 normalVector = Vector3.up;
    95.     private Vector3 wallNormalVector;
    96.  
    97.     void Awake()
    98.     {
    99.         rb = GetComponent<Rigidbody>();
    100.     }
    101.  
    102.     void Start()
    103.     {
    104.         playerScale = transform.localScale;
    105.         Cursor.lockState = CursorLockMode.Locked;
    106.         Cursor.visible = false;
    107.     }
    108.  
    109.  
    110.     private void FixedUpdate()
    111.     {
    112.         Movement();
    113.     }
    114.  
    115.     private void Update()
    116.     {
    117.         MyInput();
    118.         Look();
    119.         CheckForWall();
    120.         WallRunInput();
    121.     }
    122.  
    123.     /// <summary>
    124.     /// Find user input. Should put this in its own class but im lazy
    125.     /// </summary>
    126.     private void MyInput()
    127.     {
    128.         x = Input.GetAxisRaw("Horizontal");
    129.         y = Input.GetAxisRaw("Vertical");
    130.         jumping = Input.GetButton("Jump");
    131.         crouching = Input.GetKey(KeyCode.LeftControl);
    132.  
    133.         //Crouching
    134.         if (Input.GetKeyDown(KeyCode.LeftControl))
    135.             StartCrouch();
    136.         if (Input.GetKeyUp(KeyCode.LeftControl))
    137.             StopCrouch();
    138.     }
    139.  
    140.     private void StartCrouch()
    141.     {
    142.         transform.localScale = crouchScale;
    143.         transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
    144.         if (rb.velocity.magnitude > 0.5f)
    145.         {
    146.             if (grounded)
    147.             {
    148.                 rb.AddForce(orientation.transform.forward * slideForce);
    149.             }
    150.         }
    151.     }
    152.  
    153.     private void StopCrouch()
    154.     {
    155.         transform.localScale = playerScale;
    156.         transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
    157.     }
    158.  
    159.     private void Movement()
    160.     {
    161.         //Extra gravity
    162.         rb.AddForce(Vector3.down * Time.deltaTime * 10);
    163.  
    164.         //Find actual velocity relative to where player is looking
    165.         Vector2 mag = FindVelRelativeToLook();
    166.         float xMag = mag.x, yMag = mag.y;
    167.  
    168.         //Counteract sliding and sloppy movement
    169.         CounterMovement(x, y, mag);
    170.  
    171.         //If holding jump && ready to jump, then jump
    172.         if (readyToJump && jumping) Jump();
    173.  
    174.         //Set max speed
    175.         float maxSpeed = this.maxSpeed;
    176.  
    177.         //If sliding down a ramp, add force down so player stays grounded and also builds speed
    178.         if (crouching && grounded && readyToJump)
    179.         {
    180.             rb.AddForce(Vector3.down * Time.deltaTime * 3000);
    181.             return;
    182.         }
    183.  
    184.         //If speed is larger than maxspeed, cancel out the input so you don't go over max speed
    185.         if (x > 0 && xMag > maxSpeed) x = 0;
    186.         if (x < 0 && xMag < -maxSpeed) x = 0;
    187.         if (y > 0 && yMag > maxSpeed) y = 0;
    188.         if (y < 0 && yMag < -maxSpeed) y = 0;
    189.  
    190.         //Some multipliers
    191.         float multiplier = 1f, multiplierV = 1f;
    192.  
    193.         // Movement in air
    194.         if (!grounded)
    195.         {
    196.             multiplier = 0.5f;
    197.             multiplierV = 0.5f;
    198.         }
    199.  
    200.         // Movement while sliding
    201.         if (grounded && crouching) multiplierV = 0f;
    202.  
    203.         //Apply forces to move player
    204.         rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
    205.         rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
    206.     }
    207.  
    208.     private void Jump()
    209.     {
    210.         if (grounded && readyToJump)
    211.         {
    212.             readyToJump = false;
    213.  
    214.             //Add jump forces
    215.             rb.AddForce(Vector2.up * jumpForce * 1.5f);
    216.             rb.AddForce(normalVector * jumpForce * 0.5f);
    217.  
    218.             //If jumping while falling, reset y velocity.
    219.             Vector3 vel = rb.velocity;
    220.             if (rb.velocity.y < 0.5f)
    221.                 rb.velocity = new Vector3(vel.x, 0, vel.z);
    222.             else if (rb.velocity.y > 0)
    223.                 rb.velocity = new Vector3(vel.x, vel.y / 2, vel.z);
    224.  
    225.             Invoke(nameof(ResetJump), jumpCooldown);
    226.         }
    227.     }
    228.         if (isWallRunning)
    229.        {
    230.             readyToJump = false;
    231.  
    232.             //normal jump
    233.             if (isWallLeft && !Input.GetKey(KeyCode.D) || isWallRight && !Input.GetKey(KeyCode.A))
    234.             {
    235.                 rb.AddForce(Vector2.up* jumpForce * 1.5f);
    236.                 rb.AddForce(normalVector* jumpForce * 0.5f);
    237.             }
    238.  
    239.             //sidwards wallhop
    240.             if (isWallRight || isWallLeft && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) rb.AddForce(-orientation.up * jumpForce * 1f);
    241.             if (isWallRight && Input.GetKey(KeyCode.A)) rb.AddForce(-orientation.right * jumpForce * 3.2f);
    242.             if (isWallLeft && Input.GetKey(KeyCode.D)) rb.AddForce(orientation.right * jumpForce * 3.2f);
    243.  
    244.             //Always add forward force
    245.             rb.AddForce(orientation.forward * jumpForce * 1f);
    246.  
    247.             //Disable dashForceCounter if doublejumping while dashing
    248.             allowDashForceCounter = false;
    249.             Invoke(nameof(ResetJump), jumpCooldown);
    250.         }
    251.  
    252.     private void ResetJump()
    253.     {
    254.         readyToJump = true;
    255.     }
    256.  
    257.     private float desiredX;
    258.     private void Look()
    259.     {
    260.         float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
    261.         float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
    262.  
    263.         //Find current look rotation
    264.         Vector3 rot = playerCam.transform.localRotation.eulerAngles;
    265.         desiredX = rot.y + mouseX;
    266.  
    267.         //Rotate, and also make sure we dont over- or under-rotate.
    268.         xRotation -= mouseY;
    269.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    270.  
    271.         //Perform the rotations
    272.         playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, wallRunCameraTilt, 0);
    273.         orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
    274.    
    275.         if (Math.Abs(wallRunCameraTilt) < maxWallRunCameraTilt && isWallRunning && isWallRight)
    276.          wallRunCameraTilt += Time.deltaTime * maxWallRunCameraTilt * 2;
    277.         if (Math.Abs(wallRunCameraTilt) < maxWallRunCameraTilt && isWallRunning && isWallLeft)
    278.          wallRunCameraTilt -= Time.deltaTime * maxWallRunCameraTilt * 2;
    279.  
    280.         //Tilts camera back again
    281.         if (wallRunCameraTilt > 0 && !isWallRight && !isWallLeft)
    282.           wallRunCameraTilt -= Time.deltaTime * maxWallRunCameraTilt * 2;
    283.         if (wallRunCameraTilt < 0 && !isWallRight && !isWallLeft)
    284.          wallRunCameraTilt += Time.deltaTime * maxWallRunCameraTilt * 2;
    285.  
    286. }
    287.  
    288.     private void CounterMovement(float x, float y, Vector2 mag)
    289.     {
    290.         if (!grounded || jumping) return;
    291.  
    292.         //Slow down sliding
    293.         if (crouching)
    294.         {
    295.             rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
    296.             return;
    297.         }
    298.  
    299.         //Counter movement
    300.         if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x > threshold && x < 0))
    301.         {
    302.             rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x * counterMovement);
    303.         }
    304.         if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y > threshold && y < 0))
    305.         {
    306.             rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y * counterMovement);
    307.         }
    308.  
    309.         //Limit diagonal running. This will also cause a full stop if sliding fast and un-crouching, so not optimal.
    310.         if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed)
    311.         {
    312.             float fallspeed = rb.velocity.y;
    313.             Vector3 n = rb.velocity.normalized * maxSpeed;
    314.             rb.velocity = new Vector3(n.x, fallspeed, n.z);
    315.         }
    316.     }
    317.  
    318.     /// <summary>
    319.     /// Find the velocity relative to where the player is looking
    320.     /// Useful for vectors calculations regarding movement and limiting movement
    321.     /// </summary>
    322.     /// <returns></returns>
    323.     public Vector2 FindVelRelativeToLook()
    324.     {
    325.         float lookAngle = orientation.transform.eulerAngles.y;
    326.         float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
    327.  
    328.         float u = Mathf.DeltaAngle(lookAngle, moveAngle);
    329.         float v = 90 - u;
    330.  
    331.         float magnitue = rb.velocity.magnitude;
    332.         float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
    333.         float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
    334.  
    335.         return new Vector2(xMag, yMag);
    336.     }
    337.  
    338.     private bool IsFloor(Vector3 v)
    339.     {
    340.         float angle = Vector3.Angle(Vector3.up, v);
    341.         return angle < maxSlopeAngle;
    342.     }
    343.  
    344.     private bool cancellingGrounded;
    345.  
    346.     /// <summary>
    347.     /// Handle ground detection
    348.     /// </summary>
    349.     private void OnCollisionStay(Collision other)
    350.     {
    351.         //Make sure we are only checking for walkable layers
    352.         int layer = other.gameObject.layer;
    353.         if (whatIsGround != (whatIsGround | (1 << layer))) return;
    354.  
    355.         //Iterate through every collision in a physics update
    356.         for (int i = 0; i < other.contactCount; i++)
    357.         {
    358.             Vector3 normal = other.contacts[i].normal;
    359.             //FLOOR
    360.             if (IsFloor(normal))
    361.             {
    362.                 grounded = true;
    363.                 cancellingGrounded = false;
    364.                 normalVector = normal;
    365.                 CancelInvoke(nameof(StopGrounded));
    366.             }
    367.         }
    368.  
    369.         //Invoke ground/wall cancel, since we can't check normals with CollisionExit
    370.         float delay = 3f;
    371.         if (!cancellingGrounded)
    372.         {
    373.             cancellingGrounded = true;
    374.             Invoke(nameof(StopGrounded), Time.deltaTime * delay);
    375.         }
    376.     }
    377.  
    378.     private void StopGrounded()
    379.     {
    380.         grounded = false;
    381.     }
    382.  
    383. }
    384.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Your error message should have a line number in it. Could you please share that line number? Because this is quite a lot of code to look through.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
  4. Krakelin

    Krakelin

    Joined:
    Jan 21, 2021
    Posts:
    4
    Is this alright? sorry I am new to unity and coding in general I am trying to learn with videos but its not always clear hahaha


    upload_2021-1-24_23-55-35.png
     
  5. Krakelin

    Krakelin

    Joined:
    Jan 21, 2021
    Posts:
    4
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    These are incorrect:

    Code (CSharp):
    1. isWallRight = Physics.Raycast(transform.position, orientation.right, if, whatIsWall);
    2.         isWallLeft = Physics.Raycast(transform.position, -orientation.right, if, whatIsWall);
    Refer to Physics.Raycast to get the proper syntax:
    Unity - Scripting API: Physics.Raycast (unity3d.com)

    The other errors should go away when you fix this one, they are a "snowball effect" from the first one ;)
     
  7. Krakelin

    Krakelin

    Joined:
    Jan 21, 2021
    Posts:
    4
    Thanks a lot!