E
evoExtabyte
Guest
this is my code, at the end it shows this error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float WantedVelX;
float WantedVelZ;
float Grav;
float Angle;
float MouseX;
float MouseY;
private float waveslice;
private float horizontal;
private float vertical;
private float totalAxes;
private float translateChange;
[Header("OnGroundVariables")]
public float Acceleration;
public float Deceleration;
public float MaxSpeed;
public float GravityAccel;
public float TransitionSpeed;
public Animator CameraRig;
[Space]
[Header("MouseVariables")]
public float MouseSenitivity;
[Space]
[Header("Jumping And GroundCheck Variables")]
public bool IsGrounded;
public GameObject GroundCheckPos;
public float Distance;
public LayerMask GroundMask;
public float JumpHeight;
[Space]
[Header("SlidingVariables")]
public bool IsSliding;
public Transform OriginalRotation;
public Animator CameraAnim;
public float SpeedBoost;
[Space]
[Header("Bunny Hopping Variables")]
public float MaxAirSpeed;
public float AirAccel;
public float AirDeccel;
[Space]
[Header("Gun Swaying Animation")]
public float MoveSpeed;
public Transform GunTransform;
float timer = 0.0f;
float YPos;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;
Vector3 WantedVelocity;
Vector3 Velocity;
CharacterController CC;
// Start is called before the first frame update
void Start()
{
CC = this.GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
GetInput();
Vel();
DoMovement();
CameraEffect();
Slide();
}
void DoMovement()
{
if (IsSliding == false)
{
CC.Move(transform.TransformDirection(Vector3.ClampMagnitude(Velocity, MaxSpeed)) * Time.deltaTime);
}
else
{
CC.Move(OriginalRotation.TransformDirection(Velocity) * Time.deltaTime);
MouseY = Mathf.Clamp(MouseY, -90, 90);
}
transform.Rotate(Vector3.up, MouseX);
MouseY = Mathf.Clamp(MouseY, -90, 40);
transform.Rotate(Vector3.up, MouseX);
Camera.main.transform.localEulerAngles = new Vector3(MouseY, 0, 0);
OriginalRotation.transform.position = transform.position;
}
void GetInput()
{
WantedVelX = Input.GetAxisRaw("Horizontal");
WantedVelZ = Input.GetAxisRaw("Vertical");
MouseX = Input.GetAxis("Mouse X") * MouseSenitivity;
MouseY -= Input.GetAxis("Mouse Y") * MouseSenitivity;
if (IsGrounded == true && Input.GetKey(KeyCode.C))
{
IsSliding = true;
}
else
{
IsSliding = false;
OriginalRotation.transform.rotation = transform.rotation;
}
IsGrounded = Physics.CheckSphere(GroundCheckPos.transform.position, Distance, GroundMask);
}
void Vel()
{
WantedVelocity = new Vector3(WantedVelX, 0, WantedVelZ);
if(IsGrounded == false)
{
Grav -= GravityAccel * Time.deltaTime;
}
else
{
if (Input.GetKeyUp(KeyCode.Space))
Grav = 0;
}
if (Input.GetButtonDown("Jump") && IsGrounded == true && IsSliding == false)
{
Grav = Mathf.Sqrt(JumpHeight * -2 * -GravityAccel);
}
if (Input.GetButtonDown("Jump") && IsGrounded == true && IsSliding == true && Velocity.z > 0)
{
Grav = Mathf.Sqrt((JumpHeight + 1f) * -2 * -GravityAccel);
}
Velocity.y = Grav;
if (IsSliding == false)
{
if (IsGrounded == true)
if (WantedVelX < 0 || WantedVelX > 0 || WantedVelZ < 0 || WantedVelZ > 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxSpeed, Acceleration * Time.deltaTime);
}
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxSpeed, Acceleration * Time.deltaTime);
}
}
else
{
if (IsGrounded == true)
if (WantedVelX < 0 || WantedVelX > 0 || WantedVelZ < 0 || WantedVelZ > 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxAirSpeed, AirDeccel * Time.deltaTime);
}
if (WantedVelX == 0 || WantedVelZ == 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxAirSpeed, AirDeccel * Time.deltaTime);
}
}
}
void CameraEffect()
{
CameraAnim.SetBool("Slide", IsSliding);
if (IsSliding == false)
{
if (WantedVelX < 0)
{
Angle = Mathf.Lerp(Angle, -1, TransitionSpeed * Time.deltaTime);
}
if (WantedVelX > 0)
{
Angle = Mathf.Lerp(Angle, 1, TransitionSpeed * Time.deltaTime);
}
if (WantedVelX == 0)
{
Angle = Mathf.Lerp(Angle, 0, TransitionSpeed * Time.deltaTime);
}
}
else
{
Angle = Mathf.Lerp(Angle, 0, TransitionSpeed * Time.deltaTime);
}
CameraRig.SetFloat("X", Angle);
}
void Slide()
{
if (Input.GetKeyDown(KeyCode.C) && IsGrounded == true && WantedVelZ == 1)
{
Velocity.z = SpeedBoost;
Velocity.x = 0;
}
if (IsSliding == true)
{
Velocity.z = Mathf.Lerp(Velocity.z, 0, 1f * Time.deltaTime);
CC.center = new Vector3(0, 0.75f, 0);
CC.height = 1.5f;
}
else
{
CC.center = new Vector3(0, 1f, 0);
CC.height = 2f;
}
}
void DoGunSway()
{
if (IsGrounded == true && IsSliding == false)
{
Vector3 WantedPos = Vector3.ClampMagnitude(new Vector3(WantedVelX, YPos, WantedVelZ), 0.2f);
GunTransform.localPosition = Vector3.Lerp(GunTransform.localPosition, WantedPos, MoveSpeed);
}
waveslice = 0.0f;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
{
timer = 0.0f;
}
else
{
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2)
{
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0)
{
translateChange = waveslice * bobbingAmount;
totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
YPos = midpoint + translateChange;
}
else
{
YPos = midpoint;
}
else if (IsGrounded == false || IsSliding == true)
{
Vector3 YVel = new Vector3(0, Grav, 0);
Vector3 XZVel = new Vector3(Velocity.x, 0, Velocity.z);
Vector3 XZVelClamp = Vector3.ClampMagnitude(XZVel, 0.2f);
Vector3 YVelClamp = Vector3.ClampMagnitude(YVel, 0.2f);
Vector3 WantedPos = new Vector3(XZVelClamp.x, YVelClamp.y, XZVelClamp.z);
GunTransform.localPosition = Vector3.Lerp(GunTransform.localPosition, WantedPos, MoveSpeed);
}
}
}
Continue reading...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float WantedVelX;
float WantedVelZ;
float Grav;
float Angle;
float MouseX;
float MouseY;
private float waveslice;
private float horizontal;
private float vertical;
private float totalAxes;
private float translateChange;
[Header("OnGroundVariables")]
public float Acceleration;
public float Deceleration;
public float MaxSpeed;
public float GravityAccel;
public float TransitionSpeed;
public Animator CameraRig;
[Space]
[Header("MouseVariables")]
public float MouseSenitivity;
[Space]
[Header("Jumping And GroundCheck Variables")]
public bool IsGrounded;
public GameObject GroundCheckPos;
public float Distance;
public LayerMask GroundMask;
public float JumpHeight;
[Space]
[Header("SlidingVariables")]
public bool IsSliding;
public Transform OriginalRotation;
public Animator CameraAnim;
public float SpeedBoost;
[Space]
[Header("Bunny Hopping Variables")]
public float MaxAirSpeed;
public float AirAccel;
public float AirDeccel;
[Space]
[Header("Gun Swaying Animation")]
public float MoveSpeed;
public Transform GunTransform;
float timer = 0.0f;
float YPos;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;
Vector3 WantedVelocity;
Vector3 Velocity;
CharacterController CC;
// Start is called before the first frame update
void Start()
{
CC = this.GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
GetInput();
Vel();
DoMovement();
CameraEffect();
Slide();
}
void DoMovement()
{
if (IsSliding == false)
{
CC.Move(transform.TransformDirection(Vector3.ClampMagnitude(Velocity, MaxSpeed)) * Time.deltaTime);
}
else
{
CC.Move(OriginalRotation.TransformDirection(Velocity) * Time.deltaTime);
MouseY = Mathf.Clamp(MouseY, -90, 90);
}
transform.Rotate(Vector3.up, MouseX);
MouseY = Mathf.Clamp(MouseY, -90, 40);
transform.Rotate(Vector3.up, MouseX);
Camera.main.transform.localEulerAngles = new Vector3(MouseY, 0, 0);
OriginalRotation.transform.position = transform.position;
}
void GetInput()
{
WantedVelX = Input.GetAxisRaw("Horizontal");
WantedVelZ = Input.GetAxisRaw("Vertical");
MouseX = Input.GetAxis("Mouse X") * MouseSenitivity;
MouseY -= Input.GetAxis("Mouse Y") * MouseSenitivity;
if (IsGrounded == true && Input.GetKey(KeyCode.C))
{
IsSliding = true;
}
else
{
IsSliding = false;
OriginalRotation.transform.rotation = transform.rotation;
}
IsGrounded = Physics.CheckSphere(GroundCheckPos.transform.position, Distance, GroundMask);
}
void Vel()
{
WantedVelocity = new Vector3(WantedVelX, 0, WantedVelZ);
if(IsGrounded == false)
{
Grav -= GravityAccel * Time.deltaTime;
}
else
{
if (Input.GetKeyUp(KeyCode.Space))
Grav = 0;
}
if (Input.GetButtonDown("Jump") && IsGrounded == true && IsSliding == false)
{
Grav = Mathf.Sqrt(JumpHeight * -2 * -GravityAccel);
}
if (Input.GetButtonDown("Jump") && IsGrounded == true && IsSliding == true && Velocity.z > 0)
{
Grav = Mathf.Sqrt((JumpHeight + 1f) * -2 * -GravityAccel);
}
Velocity.y = Grav;
if (IsSliding == false)
{
if (IsGrounded == true)
if (WantedVelX < 0 || WantedVelX > 0 || WantedVelZ < 0 || WantedVelZ > 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxSpeed, Acceleration * Time.deltaTime);
}
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxSpeed, Acceleration * Time.deltaTime);
}
}
else
{
if (IsGrounded == true)
if (WantedVelX < 0 || WantedVelX > 0 || WantedVelZ < 0 || WantedVelZ > 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxAirSpeed, AirDeccel * Time.deltaTime);
}
if (WantedVelX == 0 || WantedVelZ == 0)
{
Velocity = Vector3.Lerp(Velocity, WantedVelocity * MaxAirSpeed, AirDeccel * Time.deltaTime);
}
}
}
void CameraEffect()
{
CameraAnim.SetBool("Slide", IsSliding);
if (IsSliding == false)
{
if (WantedVelX < 0)
{
Angle = Mathf.Lerp(Angle, -1, TransitionSpeed * Time.deltaTime);
}
if (WantedVelX > 0)
{
Angle = Mathf.Lerp(Angle, 1, TransitionSpeed * Time.deltaTime);
}
if (WantedVelX == 0)
{
Angle = Mathf.Lerp(Angle, 0, TransitionSpeed * Time.deltaTime);
}
}
else
{
Angle = Mathf.Lerp(Angle, 0, TransitionSpeed * Time.deltaTime);
}
CameraRig.SetFloat("X", Angle);
}
void Slide()
{
if (Input.GetKeyDown(KeyCode.C) && IsGrounded == true && WantedVelZ == 1)
{
Velocity.z = SpeedBoost;
Velocity.x = 0;
}
if (IsSliding == true)
{
Velocity.z = Mathf.Lerp(Velocity.z, 0, 1f * Time.deltaTime);
CC.center = new Vector3(0, 0.75f, 0);
CC.height = 1.5f;
}
else
{
CC.center = new Vector3(0, 1f, 0);
CC.height = 2f;
}
}
void DoGunSway()
{
if (IsGrounded == true && IsSliding == false)
{
Vector3 WantedPos = Vector3.ClampMagnitude(new Vector3(WantedVelX, YPos, WantedVelZ), 0.2f);
GunTransform.localPosition = Vector3.Lerp(GunTransform.localPosition, WantedPos, MoveSpeed);
}
waveslice = 0.0f;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
{
timer = 0.0f;
}
else
{
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2)
{
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0)
{
translateChange = waveslice * bobbingAmount;
totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
YPos = midpoint + translateChange;
}
else
{
YPos = midpoint;
}
else if (IsGrounded == false || IsSliding == true)
{
Vector3 YVel = new Vector3(0, Grav, 0);
Vector3 XZVel = new Vector3(Velocity.x, 0, Velocity.z);
Vector3 XZVelClamp = Vector3.ClampMagnitude(XZVel, 0.2f);
Vector3 YVelClamp = Vector3.ClampMagnitude(YVel, 0.2f);
Vector3 WantedPos = new Vector3(XZVelClamp.x, YVelClamp.y, XZVelClamp.z);
GunTransform.localPosition = Vector3.Lerp(GunTransform.localPosition, WantedPos, MoveSpeed);
}
}
}
Continue reading...