Help with player movement.
I seem to be getting some odd error in my movement script. It was working originally today and all of a sudden this error came about. Player movement derived from Sebastian lague I'm not sure what to do next all seems correct in his script it's a bit beyond me at the moment any help would be great.
error is from the TurtleMovement() method.
https://www.youtube.com/watch?v=ZwD1UHNCzOc&t=929s
error is on line 457 Assertion failed on expression: 'CompareApproximately(SqrMagnitude(result), 1.0F)' UnityEngine.Transform:set_eulerAngles(Vector3) PlayerController:TurtleMovement() (at Assets/Scripts/PlayerController.cs:457) PlayerController:Update() (at Assets/Scripts/PlayerController.cs:176)
and error on line 466
transform.position assign attempt for 'Leonardo' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:Translate(Vector3, Space) PlayerController:TurtleMovement() (at Assets/Scripts/PlayerController.cs:466)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public LeonardoAttackTrailScript leoTrailAttack;
PlayerController playerController;
public Vector3 respawnPosition;
public Renderer rend;
public Rigidbody rigidBody;
public AudioSource leokick;
public AudioSource metalHitSound;
public AudioSource footPunchSound;
public AudioSource explosionSound;
public AudioSource shellShock;
public AudioSource electricshock;
public AudioSource death;
public AudioSource attackSound;
public GameObject lKatanaAttackBox, rKatanaAttackBox, lFootAttackBox, rFootAttackBox; //player attackboxes
//public AudioSource leoJump;
private TenticleShock tenticleShock;
private GameObject roadKillRodneyPrefab;
private FootDoDamage footdoDamage;
public BallSpawn ballSpawn;
public GameManager gameManager;
public CharacterStats characterStats;
public CameraFollow cameraFollow;
[Header("Player Settings")]
public float walkSpeed =2;
public float runSpeed =6;
public float gravity = -12f;
public float jumpHeight = 1f;
public int attknumber;
public bool canJump;
Animator anim;
[Range(0,1)]
public float airControlPercent;
private CharacterController controller;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
float velocityY;
// Use this for initialization
void Awake()
{
if(cameraFollow ==null)
{
cameraFollow = GameObject.Find("MainCamera").GetComponent<CameraFollow>();
}
respawnPosition = transform.position;
if(rend ==null)
{
rend = GameObject.Find("Leonardo").GetComponent<Renderer>();//originally "Leonardo
}
if (electricshock ==null)
{
electricshock = GameObject.Find("electricshock").GetComponent<AudioSource>();
}
if (death == null)
{
death = GameObject.Find("death").GetComponent<AudioSource>();
}
if (metalHitSound == null)
{
metalHitSound = explosionSound = GameObject.Find("metalhit").GetComponent<AudioSource>();
}
if (explosionSound == null)
{
explosionSound = GameObject.Find("explosion").GetComponent<AudioSource>();
}
if (tenticleShock != null)
{
tenticleShock = GameObject.Find("RoadKillRodney").GetComponent<TenticleShock>();
}
if (rigidBody ==null)
{
rigidBody = GetComponent<Rigidbody>();
}
if (ballSpawn == null)
{
ballSpawn = GetComponent<BallSpawn>();
}
if (footdoDamage ==null)
{
footdoDamage = GetComponent<FootDoDamage>();
}
if(characterStats == null)
{
characterStats = GetComponent<CharacterStats>();
}
if (gameManager ==null)
{
gameManager = GetComponent<GameManager>();
}
playerController = GetComponent<PlayerController>();
//int atkRange = Random.Range(1, 5);
// anim.SetInteger("Atk", atkRange);
attknumber = Random.Range(0,4);
if (leokick == null)
{
leokick = GetComponent<AudioSource>();
}
if (leoTrailAttack == null)
{
leoTrailAttack = GetComponent<LeonardoAttackTrailScript>();
}
if (controller == null)
{
controller = GetComponent<CharacterController>();
}
anim = GetComponent<Animator>();
if (attackSound == null)
attackSound = GetComponent<AudioSource>();
if(shellShock ==null)
shellShock = GetComponent<AudioSource>();
if(leokick == null)
leokick = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update ()
{
TurtleMovement();
Attack();
Jump();
JumpKick();
SpecialAttack();
//StartCoroutine(AttackSound());
}
void OnTriggerStay(Collider other)
{
}
IEnumerator StunWhileHit()
{
controller.enabled = false; //Disable character when hit for two seconds
playerController.enabled = false;//Disable character when hit for two seconds
anim.SetBool("hit", true);
yield return new WaitForSeconds(.3f);
anim.SetBool("hit", false);
controller.enabled = true; //Enable Character after it has been disabled.
playerController.enabled = true; //Enable Character after it has been disabled.
print("Leo is stunned while being hit.");
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "lfistattack" || other.tag == "rfootattack")
{
characterStats.leoHealthSlider.value = characterStats.healthLeo;
// footdoDamage.footPunchSound.Play();
footPunchSound.Play();
characterStats.healthLeo -= characterStats.damageTakenLeo;
anim.SetBool("hit", true);
print("laser hit us.");
// StartCoroutine(StunWhileHit());
print("we are getting hit by the foot.");
if (characterStats.healthLeo <=0)
{
//StopAllCoroutines();
playerController.LeonardoDeath(); // Calling death method
characterStats.LeoLives(); // take away a life every death
StartCoroutine(LeoRespawnRoutine());
print("Running LeoRespawnRoutine we have just died.");
}
}
else
{
anim.SetBool("hit", false);
anim.SetBool("idle", true);
}
if (other.tag =="rodneylaser")
{
characterStats.leoHealthSlider.value = characterStats.healthLeo;
characterStats.healthLeo -= characterStats.rodneyTenticleDamage;
//tenticleShock.AttackBoxOn();
anim.SetBool("tenticleattack", true);
anim.SetBool("idle", false);
if (characterStats.healthLeo <= 0)
{
playerController.LeonardoDeath(); // Calling death method
characterStats.LeoLives(); // take away a life every death
controller.enabled = false;
playerController.enabled = false;
StartCoroutine(LeoRespawnRoutine());
print("we are dead.");
}
}
if(other.tag == "rodneywhip")
{
characterStats.leoHealthSlider.value = characterStats.healthLeo;
characterStats.healthLeo -= characterStats.rodneyTenticleDamage;
anim.SetBool("tenticleattack", true);
anim.SetBool("idle", false);
print("hit with RodneyWhip.");
if (characterStats.healthLeo <= 0)
{
//RodneyStops attack once dead.
anim.SetBool("tenticleattack", false);
anim.SetBool("idle", true);
//RodneyStops attack once dead.
playerController.LeonardoDeath(); // Calling death method
characterStats.LeoLives(); // take away a life every death
controller.enabled = false;
playerController.enabled = false;
StartCoroutine(LeoRespawnRoutine());
print("we are dead.");
}
}
if (other.tag == "metalball")
{
//rigidBody.velocity = new Vector3(-70f, 60f, 0f);
characterStats.healthLeo -= ballSpawn.damageToGive; // Removes 100 from life bar instant death
characterStats.lifeLeo -= 1; //Removes a life from the lives
characterStats.leoLife.text = "x " + characterStats.lifeLeo.ToString(); //converts the string to a int showing remaining lives
characterStats.leoHealthSlider.value = characterStats.healthLeo; //shows life bar going to zero
playerController.LeonardoDeath(); // Calling death method
StartCoroutine(LeoRespawnRoutine());
print("MetalBall hit us Instant death.");
}
if(other.tag == "pizza")
{
characterStats.AddHealth();
//print("we have gotten some pizza");
}
if(other.tag == "rocksteadybullet")
{
print("RockSteadyBullets are hitting us.");
}
////RockSteadyAttack
if (other.tag == "tackle" || other.tag == "rocksteadykick")
{
characterStats.leoHealthSlider.value = characterStats.healthLeo;
// footdoDamage.footPunchSound.Play();
footPunchSound.Play();
characterStats.healthLeo -= characterStats.damageTakenLeo;
anim.SetBool("hit", true);
print("laser hit us.");
// StartCoroutine(StunWhileHit());
print("we are getting hit by the foot.");
if (characterStats.healthLeo <= 0)
{
//StopAllCoroutines();
playerController.LeonardoDeath(); // Calling death method
characterStats.LeoLives(); // take away a life every death
StartCoroutine(LeoRespawnRoutine());
print("Running LeoRespawnRoutine we have just died.");
}
}
else
{
anim.SetBool("hit", false);
anim.SetBool("idle", true);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "lfistattack" || other.tag == "rfootattack")
{
anim.SetBool("hit", false);
}
}
public void LeonardoDeath()
{
shellShock.Play();
death.Play();
anim.SetTrigger("shellshock");
controller.enabled = false;
playerController.enabled = false;
}
public void RespawnPlayer()
{
characterStats.leoHealthSlider.value = characterStats.healthLeo;
characterStats.healthLeo = 100;
//characterStats.LeoLives();
transform.position = respawnPosition;
controller.enabled = true;
playerController.enabled = true;
anim.SetTrigger("wearerespawned");
}
IEnumerator LeoRespawnRoutine()
{
yield return new WaitForSeconds(1f);
RespawnPlayer();
print("Running LeoRespawnRoutine()");
}
public void JumpKick()
{
// character must be already in jumping then press the 3 button to perform jumpkick
if(!controller.isGrounded && Input.GetKeyDown(KeyCode.Joystick1Button2))
{
leoTrailAttack.rend.enabled = true;
anim.SetBool("jumpkick", true);
anim.SetBool("kick",false);
lFootAttackBox.gameObject.SetActive(true);
lKatanaAttackBox.gameObject.SetActive(true);
rKatanaAttackBox.gameObject.SetActive(true);
print("Performing JumpKick.");
}
if (Input.GetKeyUp(KeyCode.Joystick1Button2))
{
lFootAttackBox.gameObject.SetActive(false);
lKatanaAttackBox.gameObject.SetActive(false);
rKatanaAttackBox.gameObject.SetActive(false);
}
//KeyBoard Controller
// character must be already in jumping then press the 3 button to perform jumpkick
if (!controller.isGrounded && Input.GetKeyDown(KeyCode.K))
{
leoTrailAttack.rend.enabled = true;
anim.SetBool("jumpkick", true);
anim.SetBool("kick", false);
lFootAttackBox.gameObject.SetActive(true);
lKatanaAttackBox.gameObject.SetActive(true);
rKatanaAttackBox.gameObject.SetActive(true);
print("Performing JumpKick.");
}
if (Input.GetKeyUp(KeyCode.K))
{
lFootAttackBox.gameObject.SetActive(false);
lKatanaAttackBox.gameObject.SetActive(false);
rKatanaAttackBox.gameObject.SetActive(false);
}
}
IEnumerator AttackSound()
{
yield return new WaitForSeconds(0.5f);
attackSound.Play();
print("Running Attack Coroutine.");
}
public void TurtleMovement()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
//keeps from character snapping after turning
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = (Input.GetKey(KeyCode.Joystick1Button0));
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
//Moves forward
transform.Translate(transform.forward *currentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
anim.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
//Gravity
velocityY += Time.deltaTime * gravity;
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
if (controller.isGrounded)
{
velocityY = 0;
}
/*
// when player is walking attack is disabled experimenting with this myself
if(running == true && currentSpeed > 1f)
{
CancelInvoke("Attack");
CancelInvoke("SpecialAttack");
print("we are canceling attacks when walking.");
}
*/
// Character Running
/* Turning off for now
if ((Input.GetKeyDown(KeyCode.Joystick1Button0)))
{
anim.SetBool("run", true);
anim.SetBool("idle", false);
print("We are pressing Button1 we are running");
}
if ((Input.GetKeyUp(KeyCode.Joystick1Button0)))
{
anim.SetBool("idle", true);
anim.SetBool("run", false);
//print("We are pressing Button1 we are running");
}
*/
}
public void Attack()
{
if(Input.GetKeyDown(KeyCode.Joystick1Button2))
{
leokick.Play();
leoTrailAttack.rend.enabled = true;
anim.SetBool("kick" ,true);
rFootAttackBox.gameObject.SetActive(true);
lKatanaAttackBox.gameObject.SetActive(true);
rKatanaAttackBox.gameObject.SetActive(true);
//anim.SetInteger("attknumber", attknumber);
anim.SetBool("idle", false);
}
if (Input.GetKeyUp(KeyCode.Joystick1Button2))
{
leoTrailAttack.rend.enabled = false;
//anim.SetInteger("attknumber", attknumber);
anim.SetBool("kick", false);
//weapon attack Hiding
lKatanaAttackBox.gameObject.SetActive(false);
rKatanaAttackBox.gameObject.SetActive(false);
rFootAttackBox.gameObject.SetActive(false);
anim.SetBool("idle", true);
}
//KeyBoard Controller
if (Input.GetKeyDown(KeyCode.K))
{
leokick.Play();
leoTrailAttack.rend.enabled = true;
anim.SetBool("kick", true);
rFootAttackBox.gameObject.SetActive(true);
lKatanaAttackBox.gameObject.SetActive(true);
rKatanaAttackBox.gameObject.SetActive(true);
//anim.SetInteger("attknumber", attknumber);
anim.SetBool("idle", false);
}
if (Input.GetKeyUp(KeyCode.K))
{
leoTrailAttack.rend.enabled = false;
//anim.SetInteger("attknumber", attknumber);
anim.SetBool("kick", false);
//weapon attack Hiding
lKatanaAttackBox.gameObject.SetActive(false);
rKatanaAttackBox.gameObject.SetActive(false);
rFootAttackBox.gameObject.SetActive(false);
anim.SetBool("idle", true);
}
}
public void SpecialAttack()
{
if(Input.GetKeyDown(KeyCode.Joystick1Button2) && (Input.GetKey(KeyCode.Joystick1Button1)))
{
CancelInvoke("jump");
anim.SetBool("jump", false);
canJump = false;
print("we are pressing both buttons at the same time.");
}
else
{
anim.SetBool("idle", true);
canJump = true;
}
//KeyBoard
if (Input.GetKeyDown(KeyCode.K) && (Input.GetKey(KeyCode.J)))
{
CancelInvoke("jump");
anim.SetBool("jump", false);
canJump = false;
print("we are pressing both buttons at the same time.");
}
else
{
anim.SetBool("idle", true);
canJump = true;
}
}
public void Jump()
{
if (Input.GetKey(KeyCode.Joystick1Button1))
{
//cameraFollow.enabled = false;//Turning off camera follow
if(controller.isGrounded && canJump ==true )
{
//leoJump.Play();
anim.SetBool("jump", true);
float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
velocityY = jumpVelocity;
//print("We are Jumping");
}
}
if (Input.GetKeyUp(KeyCode.Joystick1Button1))
{
//cameraFollow.enabled = false;//Turning off camera follow
anim.SetBool("jump", false);
anim.SetBool("idle", true);
}
// KeyBoard Jump
if (Input.GetKey(KeyCode.Space))
{
//cameraFollow.enabled = false;//Turning off camera follow
if (controller.isGrounded && canJump == true)
{
//leoJump.Play();
anim.SetBool("jump", true);
float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
velocityY = jumpVelocity;
//print("We are Jumping");
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
//cameraFollow.enabled = false;//Turning off camera follow
anim.SetBool("jump", false);
anim.SetBool("idle", true);
}
}
//This lets you control the character when in air via a rangeslider put in a public float further up.
float GetModifiedSmoothTime(float smoothTime)
{
if(controller.isGrounded)
{
return smoothTime;
}
if(airControlPercent == 0)
{
return float.MaxValue;
}
return smoothTime / airControlPercent;
}
}