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

A little help with my code.

Discussion in 'Scripting' started by Wilc0, Aug 20, 2020.

  1. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Im having an issue. When I started this script it didnt have all the "using system" stuff at the top. It had using system collections, using system collections generic and using unity engine. Now it has populated these and is throwing the code below. any help would be appreciated.

    Assets\Scripts\PlayerController.cs(5,14): error CS0234: The type or namespace name 'Remoting' does not exist in the namespace 'System' (are you missing an assembly reference?)

    [/quote]
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Remoting.Messaging;
    using System.Security.Cryptography;
    using System.Threading;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
    public float walkSpeed = 8f;
    public float jumpSpeed = 7f;

    // To keep our Rigid Body
    Rigidbody rb;

    // Flag to keep track of wether a jump started
    bool pressedJump = false;

    // Use this for initalization
    // Start is called before the first frame update
    void Start()
    {
    // Get the rigid body component for later use
    rb = GetComponent<Rigidbody>();

    // Get the Player Collider
    coll = GetComponent<Collider>();
    }

    // Update is called once per frame
    void Update()
    {
    // Handle Player walking
    WalkHandler();

    // Handle Player jumping
    JumpHandler();
    }

    // Make the player walk according to the user input
    void WalkHandler()
    {
    // Set x and z velocities to zero
    rb.velocity = new Vector3(0, rb.velocity.y, 0);

    // Distance ( Speed = Distance / time --> distance = speed * time)
    float distance = walkSpeed * Time.deltaTime;

    // Input on x ("Horizontal")
    float hAxis = Input.GetAxis("Horizontal");

    // Input on z ("Vertical")
    float vAxis = Input.GetAxis("Vertical");

    // Movement Vector
    Vector3 movement = new Vector3(hAxis * distance, 0f, vAxis * distance);

    // Current Position
    Vector3 currPosition = transform.position;

    // New Position
    Vector3 newPosition = currPosition + movement;

    // Move the Rigid body
    rb.MovePosition(newPosition);
    }
    // Checker whether the player can jump and make it jump
    void JumpHandler()
    {
    // Jump Axis
    float jAxis = Input.GetAxis("Jump");

    // Is grounded
    bool isGrounded = CheckGrounded();

    // Check if the player is pressing the jump key
    if (jAxis > 0f)
    {
    // Make sure we've no already jumped on this key press
    if (!pressedJump && isGrounded)
    {
    // We are jumping on the current key press
    pressedJump = true;

    // Jumping Vector
    Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);

    // Make the Player jump by adding velocity
    rb.velocity = rb.velocity + jumpVector;
    }
    }
    else
    {
    // Update flag so it can jump again if we press the jump key
    pressedJump = false;
    }
    }

    // Check if the object is grounded
    bool CheckGrounded()
    {
    // Object size in x
    float sizeX = coll.bounds.size.x;
    float sizeZ = coll.bounds.size.z;
    float sizeY = coll.bounds.size.y;

    //Position of the 4 bottom corners of the game object
    // We add 0.01 in the Y so that there is some distance between the point and the floor
    Vector3 corner1 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
    Vector3 corner2 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
    Vector3 corner3 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
    Vector3 corner4 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);

    // Send a short ray down the cube on all 4 corners to detect ground
    bool grounded1 = Physics.Raycast(corner1, new Vector3(0, -1, 0), 0.01f);
    bool grounded2 = Physics.Raycast(corner2, new Vector3(0, -1, 0), 0.01f);
    bool grounded3 = Physics.Raycast(corner3, new Vector3(0, -1, 0), 0.01f);
    bool grounded4 = Physics.Raycast(corner4, new Vector3(0, -1, 0), 0.01f);

    // If any corner is grounded, the object is grounded
    return (grounded1 || grounded2 || grounded3 || grounded4);
    }
    }
    [/quote]
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Based on your code, the only using statements you should need in your file are these:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    You probably don't even need the System one either.
    Delete all the rest.
     
    Wilc0 likes this.
  3. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Figured this one out. forgot to call the collider
     
    PraetorBlue likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    The error is quite straightforward. You never created a variable named
    coll
    , but you are trying to use it several times in the script.

    Try declaring a variable near your "rb" variable:

    Code (CSharp):
    1. // To keep our Rigid Body
    2. Rigidbody rb;
    3. // To keep our Collider
    4. Collider coll;
     
    Wilc0 likes this.
  5. Wilc0

    Wilc0

    Joined:
    Jun 3, 2020
    Posts:
    20
    Thank you very much for the help. I appreciate you.