Hello, I’m using untiy since it name was Unity3d but my brain merge into itself here on this simple problem
thanks in advance
using UnityEngine;
using UnityEngine.UI;
public class Chacarter_controller : MonoBehaviour
{
public float RotationX , RotationY , MinAngle, MaxAngle;
public GameObject DuckMode_Col, NormalMode_Col;
public Transform DuckMode_Transform, NormalMode_Transform;
public Camera PlayerCam;
public Rigidbody Rigid_Player;
public KeyCode Jump_key, Duck_key, Sprint_Key, Melee_key, Use_key;
public float Sensitivity, SprintSpeed, SprintMultiplier , SprintSpeedMax , PlayerMain_Speed, DuckMain_Speed, Jump_Power, OriginToFeet_Distance, HeadClearDistance;
public GameObject Active_Col;
public bool IsTochingGround, AboveClear;
public bool Ducking , MeleeCooling;
public float MeleeCooldown = 10, MeleeDamage, MeleeThrowForce , MeleeDistance, meleeSpeedMultiplier;
public Slider MeleeSlide;
public float MaxEquipDist , DistanceFromTarget;
public GameObject RayObject;
public GameObject EquipSign;
public float NonEquippedGunDist;
public Transform HoldPosition;
//Non-Public
RaycastHit AttackedRigidMelee;
Vector3 Move_Direction;
RaycastHit GunRayHit;
void Start()
{
Rigid_Player = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
GunSystem();
Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out AttackedRigidMelee);
if (AttackedRigidMelee.rigidbody != null)
{
DistanceFromTarget = (AttackedRigidMelee.transform.position - transform.position).magnitude;
}else
{
DistanceFromTarget = 0;
}
if (MeleeCooldown < 0)
{
MeleeCooldown = 0;
}
MeleeSlide.value = MeleeCooldown / 10;
if (Input.GetMouseButton(1))
{
Cursor.visible = false;
}
if (MeleeCooldown == 10 && MeleeCooldown > 0)
{
if (MeleeCooldown == 10 && Input.GetKeyDown(Melee_key) && AttackedRigidMelee.transform.gameObject != null && (AttackedRigidMelee.transform.position - transform.position).magnitude < MeleeDistance )
{
MeleeCooling = true;
AttackedRigidMelee.rigidbody.AddForceAtPosition(PlayerCam.transform.forward * MeleeThrowForce , AttackedRigidMelee.point);
}
}
if (MeleeCooldown == 0 && MeleeCooldown <= 10
)
{
MeleeCooling = false;
}
if (!Ducking && Input.GetKey(Sprint_Key))
{
SprintSpeed = Mathf.Lerp(SprintSpeed, SprintSpeedMax, SprintMultiplier * Time.deltaTime);
}
else
{
SprintSpeed = 1;
}
if (Input.GetKey(Duck_key))
{ Ducking = true; }
else if (AboveClear)
{ Ducking = false; }
if (Ducking)
{
PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position , DuckMode_Transform.position, DuckMain_Speed* Time.deltaTime);
DuckMode_Col.SetActive(true);
NormalMode_Col.SetActive(false);
Active_Col = DuckMode_Col;
}
else
{
PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position, NormalMode_Transform.position, DuckMain_Speed * Time.deltaTime);
DuckMode_Col.SetActive(false);
NormalMode_Col.SetActive(true);
Active_Col = NormalMode_Col;
}
//Rigid_Player.transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * Sensitivity);
RotationY += Input.GetAxisRaw("Mouse X") * Sensitivity;
RotationX += Input.GetAxisRaw("Mouse Y") * Sensitivity;
RotationX = Mathf.Clamp(RotationX, MinAngle, MaxAngle);
Rigid_Player.transform.eulerAngles = new Vector3(0,RotationY,0);
PlayerCam.transform.eulerAngles = new Vector3(-RotationX, RotationY, 0);
float HorizontalMovement = Input.GetAxisRaw("Horizontal");
float VerticalMovement = Input.GetAxisRaw("Vertical");
Move_Direction = (HorizontalMovement * transform.right + VerticalMovement * transform.forward).normalized;
if (IsTochingGround && Input.GetKeyDown(Jump_key) && AboveClear)
{
Rigid_Player.AddForce(Vector3.up * Jump_Power);
}
}
void OnCollisionStay(Collision collision)
{
if (Physics.Raycast(transform.position, Vector3.up, HeadClearDistance))
{
AboveClear = false;
}
else
{
AboveClear = true;
}
if (Physics.Raycast(transform.position, Vector3.down, OriginToFeet_Distance))
{
IsTochingGround = true;
}
else
{
IsTochingGround = false;
}
}
void FixedUpdate()
{
if (MeleeCooling)
{
MeleeCooldown--;
}
else if (MeleeCooldown < 10)
{
MeleeCooldown++;
}
Walking();
}
public void Walking()
{
Vector3 FixForY_Axis = new Vector3(0, Rigid_Player.velocity.y, 0);
Rigid_Player.velocity = Move_Direction * PlayerMain_Speed* SprintSpeed * Time.deltaTime;
Rigid_Player.velocity += FixForY_Axis;
}
void GunSystem()
{
Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out GunRayHit);
if (GunRayHit.rigidbody != null && GunRayHit.transform.GetComponent<GunMain>() != null)
{
NonEquippedGunDist = (transform.position - GunRayHit.transform.position).magnitude;
}
if(NonEquippedGunDist <= MaxEquipDist && GunRayHit.rigidbody != null )
{
if (GunRayHit.transform.gameObject.GetComponent<GunMain>().IsEquipped == false && GunRayHit.transform.gameObject.tag == "Gun")
{
EquipSign.SetActive(true);
if (Input.GetKeyDown(Use_key))
GunRayHit.transform.GetComponent<GunMain>().IsEquipped = true;
GunRayHit.transform.GetComponent<GunMain>().EquippedPlayer = gameObject;
}
}
else
{
EquipSign.SetActive(false);
}
}
}
on the 169th Line i get some “NullReferenceException:
Object reference not set to an instance of an object” error. Can you guys help me about it.