Why isn't the AI following the player? - c#

So I have written some code and everything works in it besides the AI following the player. I don't know what I did wrong so any help would be gladly appreciated!
public class SharkAI : MonoBehaviour
{
public float speed;
public Transform patrolPoints;
private float waitTime;
public float startWaitTime;
public float minX;
public float maxX;
public float minY;
public float maxY;
public float oldPosition;
public float newPosition;
private SpriteRenderer fishes;
public float eyeSightOfFish;
public float disToPlayer;
private Transform player;
// Start is called before the first frame update
void Start()
{
waitTime = startWaitTime;
patrolPoints.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
fishes = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
oldPosition = transform.position.x;
newPosition = patrolPoints.position.x;
Debug.Log("Player distance " + disToPlayer);
//distToPatrolPoint = Vector3.Distance(transform.position, patrolPoints[whichPoint].transform.position);
disToPlayer = Vector2.Distance(transform.position, player.transform.position);
transform.position = Vector2.MoveTowards(transform.position, patrolPoints.position, speed * Time.deltaTime);
if (newPosition > oldPosition)
{
fishes.flipX = true;
}
else { fishes.flipX = false; }
if(disToPlayer <= eyeSightOfFish)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, patrolPoints.position) <= 0.2f)
{
if (waitTime <= 0)
{
patrolPoints.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
so im not sure where this is wrong and ive tried a few things but I can't seem to make the ai follow the player.

Related

Adding FPS Sliding using the Character Controller

I'm currently working on a FPS "Speedrun" game & I want to add "Sliding" to the players movement. I already added "Crouching", but currently I'm stuck at getting the sliding done. (I'm using the Character Controller) I've tried many things, but nothing really worked. Heres the current PlayerMovement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
private Rigidbody rb;
public Transform cameraTransform;
public HeadBobController headBobController;
[Header("Movement")]
private float speedMultiplier = 1f;
public float speed = 12f;
public float sprintSpeed = 20f;
public float slopeSpeed = 0.01f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float sensitivity = 2f;
public float cameraRotationLimit = 85f;
[Header("Countermovement")]
public float counterMovement = 0.175f;
private float threshold = 0.01f;
public float maxSlopeAngle = 35f;
[Header("Sliding / Crouch")]
public float slideSpeed;
float originalHeight;
public float reducedHeight;
float targetHeight = 2f;
public float timeToCrouch = 0.25f;
float timeElapsed = 0;
bool canStopCrouching;
bool wantStopCrouch;
bool isRunning = false;
public bool isCrouching = false;
bool canUseHeadbob = true;
bool WillSlideOnSlopes = true;
bool IsSliding
{
get
{
if (controller.isGrounded && Physics.Raycast(transform.position, Vector3.down, out RaycastHit slopeHit, 3f))
{
hitPointNormal = slopeHit.normal;
return Vector3.Angle(hitPointNormal, Vector3.up) > controller.slopeLimit;
} else
{
return false;
}
}
}
[Header("Headbob")]
[SerializeField] private float walkBobSpeed = 14f;
[SerializeField] private float walkBobAmount = 0.05f;
[SerializeField] private float sprintBobSpeed = 18f;
[SerializeField] private float sprintBobAmount = 0.11f;
[SerializeField] private float crouchBobSpeed = 8f;
[SerializeField] private float crouchBobAmount = 0.025f;
private float defaultYPos = 0;
private float timer;
private Vector3 hitPointNormal;
[Header("Ground Check")]
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
public bool isGrounded;
[Header("Camera")]
float cameraRotationX = 0f;
float cameraRotationY = 0f;
public Camera camera;
public Transform orientation;
[Header("Other")]
public GameObject speedlines;
public ParticleSystem jumpParticle;
void Start()
{
rb = GetComponent<Rigidbody>();
controller = GetComponent<CharacterController>();
originalHeight = controller.height;
defaultYPos = camera.transform.localPosition.y;
//Cursor.lockState = CursorLockMode.Locked;
//Cursor.visible = false;
}
public void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
move = Vector3.ClampMagnitude(move, 1f);
if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
{
controller.Move(move * sprintSpeed * Time.deltaTime);
isRunning = true;
headBobController.amplitude = 0.1f;
headBobController.frequency = 30f;
} else
{
controller.Move(move * speed * speedMultiplier * Time.deltaTime);
isRunning = false;
headBobController.amplitude = 0.0426f;
headBobController.frequency = 19.1f;
}
if (Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
canStopCrouching = false;
} else
{
canStopCrouching = true;
}
if (canStopCrouching && wantStopCrouch && !Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
wantStopCrouch = false;
StopCrouch();
}
if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded)
{
//controller.height = Mathf.Lerp(reducedHeight, originalHeight, timeElapsed);
StartCrouch();
} else if(Input.GetKeyUp(KeyCode.LeftControl) && isGrounded && !Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
//controller.height = Mathf.Lerp(originalHeight, targetHeight, timeElapsed);
StopCrouch();
} else if(Input.GetKeyUp(KeyCode.LeftControl) && isGrounded && Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
wantStopCrouch = true;
}
if (Input.GetKey(KeyCode.LeftControl) && isRunning && isGrounded)
{
//Sliding
}
if (canUseHeadbob && isGrounded)
{
if (Mathf.Abs(move.x) > 0.1f || Mathf.Abs(move.z) > 0.1f)
{
timer += Time.deltaTime * (isCrouching ? crouchBobSpeed : isRunning ? sprintBobSpeed : walkBobSpeed);
camera.transform.localPosition = new Vector3(camera.transform.localPosition.x, defaultYPos + Mathf.Sin(timer) * (isRunning ? sprintBobAmount : walkBobAmount), camera.transform.localPosition.z);
}
}
if(WillSlideOnSlopes && IsSliding)
{
move += new Vector3(hitPointNormal.x, -hitPointNormal.y, hitPointNormal.z) * slopeSpeed;
}
if (isRunning)
{
speedlines.active = true;
} else {
speedlines.active = false;
}
controller.Move(move * speed * speedMultiplier * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
controller.Move(-transform.forward * 0.1f);
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
jumpParticle.Play();
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
// Rotate the player based on mouse input
float mouseX = Input.GetAxis("Mouse X") * sensitivity;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity;
cameraRotationY += mouseX;
cameraRotationX -= mouseY;
cameraRotationX = Mathf.Clamp(cameraRotationX, -cameraRotationLimit, cameraRotationLimit);
cameraTransform.localEulerAngles = new Vector3(cameraRotationX, cameraRotationY, 0f);
transform.eulerAngles = new Vector3(0f, cameraRotationY, 0f);
}
private void StartCrouch()
{
controller.height = reducedHeight;
speedMultiplier = 0.5f;
isCrouching = true;
}
private void StopCrouch()
{
controller.height = originalHeight;
speedMultiplier = 1f;
isCrouching = false;
}
}
It would be very nice if anyone could try to explain me how to implement it into this code using the Character Controller! :)
I've tried multiple Youtube Tutorial solutions & a lot in Online Forums, nothing really worked for me for some reason. Often the system wouldn't even work (nothing happened). I want the player to have the crouching ability (which is implemented) & to slide when isRunning & when the player isGrounded

Crouching is Not Working But Sliding and Sprinting is - Solved

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
private float moveSpeed;
public float walkSpeed;
public float sprintSpeed;
public bool sliding;
public bool sprinting;
public float groundDrag;
[Header("Jumping")]
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[Header("Crouching")]
public float crouchSpeed;
public float crouchYScale;
private float startYScale;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
public KeyCode sprintKey = KeyCode.LeftShift;
public KeyCode crouchKey = KeyCode.LeftControl;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
[Header("Slope Handling")]
public float maxSlopeAngle;
private RaycastHit slopeHit;
private bool exitingSlope;
[Header("Sliding")]
public float maxSlideTime;
public float slideForce;
private float slideTimer;
public float slideSpeed;
public float slideYScale;
[Header("References")]
public Transform orientation;
private Rigidbody rb;
[Header("Input")]
private float horizontalInput;
private float verticalInput;
Vector3 moveDirection;
public MovementState state;
public enum MovementState
{
walking,
sprinting,
crouching,
sliding,
air
}
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
readyToJump = true;
startYScale = transform.localScale.y;
}
private void Update()
{
// ground check
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
MyInput();
SpeedControl();
StateHandler();
if (Input.GetKeyUp(sprintKey) || Input.GetKeyUp(KeyCode.W))
sprinting = false;
// handle drag
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if (Input.GetKeyDown(crouchKey) && sprinting)
StartSlide();
if (Input.GetKeyUp(crouchKey) && sliding)
StopSlide();
}
private void FixedUpdate()
{
MovePlayer();
if (sliding)
SlidingMovement();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
// when to jump
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
// start crouch
if (Input.GetKeyDown(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
}
if (Input.GetKeyDown(crouchKey) && sprinting)
{
transform.localScale = new Vector3(transform.localScale.x, slideYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
sliding = true;
moveSpeed = slideSpeed;
}
// stop crouch
if (Input.GetKeyUp(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
sliding = false;
}
}
private void StateHandler()
{
// Mode - Crouching
if (Input.GetKey(crouchKey))
{
state = MovementState.crouching;
moveSpeed = crouchSpeed;
}
// Mode - Sliding
if(Input.GetKey(crouchKey) && sprinting)
{
state = MovementState.sliding;
moveSpeed = slideSpeed;
}
// Mode - Sprinting
else if (grounded && Input.GetKey(sprintKey) && Input.GetKey(KeyCode.W))
{
state = MovementState.sprinting;
moveSpeed = sprintSpeed;
sprinting = true;
}
// Mode - Walking
else if (grounded)
{
state = MovementState.walking;
moveSpeed = walkSpeed;
}
// Mode - Air
else
{
state = MovementState.air;
}
}
private void MovePlayer()
{
// calculate movement direction
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
// on slope
if (OnSlope() && !exitingSlope)
{
rb.AddForce(GetSlopeMoveDirection() * moveSpeed * 20f, ForceMode.Force);
if (rb.velocity.y > 0)
rb.AddForce(Vector3.down * 80f, ForceMode.Force);
}
// on ground
else if (grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
// in air
else if (!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
// turn gravity off while on slope
rb.useGravity = !OnSlope();
}
private void SpeedControl()
{
// limiting speed on slope
if (OnSlope() && !exitingSlope)
{
if (rb.velocity.magnitude > moveSpeed)
rb.velocity = rb.velocity.normalized * moveSpeed;
}
// limiting speed on ground or in air
else
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
// limit velocity if needed
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
}
private void Jump()
{
exitingSlope = true;
// reset y velocity
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
exitingSlope = false;
}
private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
return angle < maxSlopeAngle && angle != 0;
}
return false;
}
private Vector3 GetSlopeMoveDirection()
{
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}
private void StartSlide()
{
sliding = true;
transform.localScale = new Vector3(transform.localScale.x, slideYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
slideTimer = maxSlideTime;
}
private void SlidingMovement()
{
Vector3 inputDirection = orientation.forward;
rb.AddForce(inputDirection.normalized * slideForce, ForceMode.Force);
slideTimer -= Time.deltaTime;
if (slideTimer <= 0)
StopSlide();
}
private void StopSlide()
{
sliding = false;
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
}
}
I have been trying for a very long time to fix this, although I'm not the best at coding. I decided to come onto stack overflow for some help. Thank you to everyone who tries to solves this problem :D. As the titles states everything here works, excluding the crouching, when I press ctrl my moveSpeed does not change to my crouchSpeed...

How can I make the animation match when the NPC walks left or right?

I have some NPCs that randomly walk within a range, however when I can't determine when they are walking left or right so the animation looks wrong.
I have a function called "Decrease" that warns inside the terminal when the object walks decreases or increases its position, but this function does not work as it should
So...
How can I make the animation match when the NPC walks left or right?
Here is my NPC configuration:
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCController : MonoBehaviour
{
private float waitTime;
private Animator _animator;
private SpriteRenderer _renderer;
public Transform moveSpot;
private bool _facingRight = true;
public float lastXVal;
public float speed;
public float startWaitTime;
public float minX;
public float maxX;
public float minY;
public float maxY;
void Awake()
{
_animator = GetComponent<Animator>();
_renderer = GetComponent<SpriteRenderer>();
}
void Start()
{
waitTime = startWaitTime;
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
{
//Change animation state
if (waitTime <= 0)
{
_animator.SetBool("Walk", true);
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
waitTime = startWaitTime;
}
else
{
_animator.SetBool("Walk", false);
_animator.SetBool("Idle", true);
waitTime -= Time.deltaTime;
}
}
}
public void Decrease()
{
if (transform.hasChanged)
{
if (transform.position.x < lastXVal)
{
//Update lastXVal
lastXVal = transform.position.x;
Debug.Log("Decreased!");
}
else if (transform.position.x > lastXVal)
{
//Update lastXVal
lastXVal = transform.position.x;
Debug.Log("Increased");
}
transform.hasChanged = false;
}
}
}
You can use dot product to calculate the direction of travel.
Vector2 moveDirection = (moveSpot.position - transform.position).normalized;
float dotValue = Vector2.Dot(Vector2.right, moveDirection)
// if dotValue is 1 then you are moving right and if dotValue is -1 you are moving left

How do I assign a new transform.position for a GameObject that's triggered by an OnTriggerEnter in Unity?

I have code in my PlayerScript that, on collision with the floor game object (which I've tagged with "FloorTag"), decrements a variable called "Lives." Lives is decrementing, so I know the collision is registering (and everything is tagged correctly). However, I also want the Player game object's position to be reset to a specific Vector3 I've declared when it collides with the floor. For some reason, though, it's not doing this. Where have I gone wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerScript : MonoBehaviour
{
public int Lives;
public Text LivesText;
public int Points;
public Text PointsText;
public Text GameOverText;
public CharacterController CharController;
public float moveSpeed = 9;
public float rotSpeed = 85;
float yVelocity = 0;
public float jumpForce = 2.5f;
public float gravityModifier = 0.025f;
bool prevIsGrounded;
Vector3 StartingPlatformCoords = new Vector3 (38, 16, 38);
// Start is called before the first frame update
void Start()
{
Lives = 8;
SetLivesText();
Points = 0;
SetPointsText();
prevIsGrounded = CharController.isGrounded;
//CharController = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
// L-R Forward-Back Motion
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
transform.Rotate(0, rotSpeed * Time.deltaTime * hAxis, 0);
Vector3 amountToMove = transform.forward * moveSpeed * Time.deltaTime * vAxis;
// Jump Motion
if (CharController.isGrounded)
{
if (!prevIsGrounded && CharController.isGrounded)
{
yVelocity = 0;
}
if (Input.GetKeyDown(KeyCode.Space))
{
yVelocity = jumpForce;
}
}
else
{
if (Input.GetKeyUp(KeyCode.Space))
{
yVelocity = 0;
}
yVelocity += Physics.gravity.y * gravityModifier;
}
amountToMove.y = yVelocity;
// Modify the y-value within this Vector3 (which contains an x, y, z, and some utility functions like distance etc.) manually
// Final Motion
CharController.Move(amountToMove);
// Update
prevIsGrounded = CharController.isGrounded;
// Camera
Vector3 camPos = transform.position + transform.forward * -10 + Vector3.up * 3;
Camera.main.transform.position = camPos;
Camera.main.transform.LookAt(transform);
//if (Input.GetKeyDown(KeyCode.Space))
//{
//yVelocity = jumpForce;
//}
//yVelocity += Physics.gravity.y * gravityModifier;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("FloorTag"))
{
transform.position = StartingPlatformCoords;
Lives -= 1;
SetLivesText();
Debug.Log(transform.position);
}
if (other.gameObject.CompareTag("CellTag"))
{
Points += 1;
SetPointsText();
}
}
void SetLivesText()
{
LivesText.text = "Lives: " + Lives.ToString();
if (Lives <= 0)
{
GameOverText.text = "Game Over";
}
}
void SetPointsText()
{
PointsText.text = "Score: " + Points.ToString();
}
}
CharacterController can override changes made to transform.position. Simplest workaround is to disable it, then re-enable it after directly modifying transform.position:
public CharacterController CharController;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("FloorTag"))
{
CharController.enabled = false;
transform.position = StartingPlatformCoords;
CharController.enabled = true;
Lives -= 1;
SetLivesText();
Debug.Log(transform.position);
}
if (other.gameObject.CompareTag("CellTag"))
{
Points += 1;
SetPointsText();
}
}

Unity3D Mecanim Character stopping before turning

so for the past few days I've been working on a character controller in Unity3D using mecanim. It's not based off of my own code, but off of a tutorial I found online, of course that tutorial was meant for Unity 4, so I am running in to small problems here and there, but nothing I couldn't fix up until now.
So the basic problem is that my character seems to (without reason) stop all his momentum and slowly turns around when I try to make a hard 180 degrees turn, afterwards he continues to run like normal again, but I don't see why he would suddenly stop turning.
Here is my character logic script:
using UnityEngine;
using System.Collections;
public class characterLogic : MonoBehaviour {
[SerializeField]
private Animator animator;
[SerializeField]
private FollowCamera gamecam;
[SerializeField]
private float directionSpeed = 1.5f;
[SerializeField]
private float directionDampTime = 0.25f;
[SerializeField]
private float rotationDegreePerSecond = 120f;
[SerializeField]
private float speedDampTime = 0.05f;
private float speed = 0.0f;
private float direction = 0.0f;
private float charAngle = 0f;
private float horizontal = 0.0f;
private float vertical = 0.0f;
private AnimatorStateInfo stateInfo;
private AnimatorTransitionInfo transInfo;
private int m_LocomotionId = 0;
private int m_LocomotionPivotLId = 0;
private int m_LocomotionPivotRId = 0;
private int m_LocomotionPivotLTransId = 0;
private int m_LocomotionPivotRTransId = 0;
public Animator Animator
{
get
{
return this.animator;
}
}
public float Speed
{
get
{
return this.speed;
}
}
public float LocomotionThreshold { get { return 0.2f; } }
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
if(animator.layerCount >= 2)
{
animator.SetLayerWeight(1, 1);
}
m_LocomotionId = Animator.StringToHash("Base Layer.Locomotion");
m_LocomotionPivotLId = Animator.StringToHash("Base Layer.LocomotionPivotL");
m_LocomotionPivotRId = Animator.StringToHash("Base Layer.LocomotionPivotR");
m_LocomotionPivotLTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotL");
m_LocomotionPivotRTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotR");
}
public void keysToWorldSpace (Transform root, Transform camera, ref float directionOut, ref float speedOut, ref float angleOut, bool isPivoting){
Vector3 rootDirection = root.forward;
Vector3 keyDirection = new Vector3 (horizontal, 0, vertical);
speedOut = keyDirection.sqrMagnitude;
//get camera rotation
Vector3 cameraDirection = camera.forward;
cameraDirection.y = 0.0f;
Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
//convert key input to world space coordinates
Vector3 moveDirection = referentialShift * keyDirection;
Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), moveDirection, Color.green);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), axisSign, Color.red);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), rootDirection, Color.magenta);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), keyDirection, Color.blue);
float angleRootToMove = Vector3.Angle(rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);
if (!isPivoting)
{
angleOut = angleRootToMove;
}
angleRootToMove /= 180f;
directionOut = angleRootToMove * directionSpeed;
}
// Update is called once per frame
void Update () {
if (animator) {
stateInfo = animator.GetCurrentAnimatorStateInfo(0);
transInfo = animator.GetAnimatorTransitionInfo(0);
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
charAngle = 0f;
direction = 0f;
keysToWorldSpace (this.transform, gamecam.transform, ref direction, ref speed, ref charAngle, isInPivot());
animator.SetFloat ("Speed", speed);
animator.SetFloat ("Direction", direction, directionDampTime, Time.deltaTime);
if(speed > LocomotionThreshold){
if(!isInPivot()){
Animator.SetFloat("Angle", charAngle);
}
}
if(speed < LocomotionThreshold && Mathf.Abs(horizontal) < 0.05f){
animator.SetFloat("Direction", 0f);
animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
}
Debug.Log(Speed);
Debug.Log(charAngle);
}
}
void FixedUpdate() {
if (IsInLocomotion () && ((direction >= 0 && horizontal >= 0) || (direction < 0 && horizontal < 0))) {
Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, rotationDegreePerSecond * (horizontal < 0f ? -1f : 1f), 0f), Mathf.Abs(horizontal));
Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
this.transform.rotation = (this.transform.rotation * deltaRotation);
}
}
public bool isInPivot(){
return stateInfo.fullPathHash == m_LocomotionPivotLId ||
stateInfo.fullPathHash == m_LocomotionPivotRId ||
transInfo.nameHash == m_LocomotionPivotLTransId ||
transInfo.nameHash == m_LocomotionPivotRTransId;
}
public bool IsInLocomotion(){
return stateInfo.fullPathHash == m_LocomotionId;
}
}
I believe it either has to do something with this script or with the transitions within mecanim. I also ported the finished product (found here: https://github.com/jm991/UnityThirdPersonTutorial ) of the tutorial over to Unity 5 and didn't experience the same problem there, I am not entirely sure what the difference is which gives me this problem, but if any of you know or find out, please let me know.
I found the problem myself already!
Here is the new code in case anyone is interested in the future:
using UnityEngine;
using System.Collections;
public class characterLogic : MonoBehaviour {
[SerializeField]
private Animator animator;
[SerializeField]
private FollowCamera gamecam;
[SerializeField]
private float directionSpeed = 1.5f;
[SerializeField]
private float directionDampTime = 0.25f;
[SerializeField]
private float rotationDegreePerSecond = 120f;
[SerializeField]
private float speedDampTime = 0.05f;
[SerializeField]
private float fovDampTime = 3f;
private float horizontal = 0.0f;
private float vertical = 0.0f;
private float speed = 0.0f;
private float direction = 0.0f;
private float charAngle = 0f;
private const float SPRINT_SPEED = 2.0f;
private const float SPRINT_FOV = 75.0f;
private const float NORMAL_FOV = 60.0f;
private const float WALK_SPEED = 0.1f;
private AnimatorStateInfo stateInfo;
private AnimatorTransitionInfo transInfo;
private int m_LocomotionId = 0;
private int m_LocomotionPivotLId = 0;
private int m_LocomotionPivotRId = 0;
private int m_LocomotionPivotLTransId = 0;
private int m_LocomotionPivotRTransId = 0;
public Animator Animator
{
get
{
return this.animator;
}
}
public float Speed
{
get
{
return this.speed;
}
}
public float LocomotionThreshold { get { return 0.2f; } }
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
if(animator.layerCount >= 2)
{
animator.SetLayerWeight(1, 1);
}
m_LocomotionId = Animator.StringToHash("Base Layer.Locomotion");
m_LocomotionPivotLId = Animator.StringToHash("Base Layer.LocomotionPivotL");
m_LocomotionPivotRId = Animator.StringToHash("Base Layer.LocomotionPivotR");
m_LocomotionPivotLTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotL");
m_LocomotionPivotRTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotR");
}
public void keysToWorldSpace (Transform root, Transform camera, ref float directionOut, ref float speedOut, ref float angleOut, bool isPivoting){
Vector3 rootDirection = root.forward;
Vector3 keyDirection = new Vector3 (horizontal, 0, vertical);
speedOut = keyDirection.sqrMagnitude;
//get camera rotation
Vector3 cameraDirection = camera.forward;
cameraDirection.y = 0.0f;
Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, Vector3.Normalize(cameraDirection));
//convert key input to world space coordinates
Vector3 moveDirection = referentialShift * keyDirection;
Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), moveDirection, Color.green);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), axisSign, Color.red);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), rootDirection, Color.magenta);
Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), keyDirection, Color.blue);
float angleRootToMove = Vector3.Angle(rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);
if (!isPivoting)
{
angleOut = angleRootToMove;
}
angleRootToMove /= 180f;
directionOut = angleRootToMove * directionSpeed;
}
// Update is called once per frame
void Update () {
if (animator) {
stateInfo = animator.GetCurrentAnimatorStateInfo(0);
transInfo = animator.GetAnimatorTransitionInfo(0);
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
charAngle = 0f;
direction = 0f;
float charSpeed = 0f;
keysToWorldSpace (this.transform, gamecam.transform, ref direction, ref charSpeed, ref charAngle, isInPivot());
if (Input.GetButton("Sprint"))
{
speed = Mathf.Lerp(speed, SPRINT_SPEED, Time.deltaTime);
gamecam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gamecam.GetComponent<Camera>().fieldOfView, SPRINT_FOV, fovDampTime * Time.deltaTime);
}
else
{
speed = charSpeed;
gamecam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gamecam.GetComponent<Camera>().fieldOfView, NORMAL_FOV, fovDampTime * Time.deltaTime);
}
if (Input.GetButton("Walk"))
{
speed = Mathf.Lerp(speed, WALK_SPEED, Time.deltaTime);
}
else
{
speed = charSpeed;
}
animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
animator.SetFloat("Direction", direction, directionDampTime, Time.deltaTime);
if(speed > LocomotionThreshold){
if(!isInPivot()){
Animator.SetFloat("Angle", charAngle);
}
}
if(speed < LocomotionThreshold && Mathf.Abs(horizontal) < 0.05f){
animator.SetFloat("Direction", 0f);
animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
}
Debug.Log(Speed);
Debug.Log(charAngle);
}
}
void FixedUpdate() {
if (IsInLocomotion () && ((direction >= 0 && horizontal >= 0) || (direction < 0 && horizontal < 0))) {
Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, rotationDegreePerSecond * (horizontal < 0f ? -1f : 1f), 0f), Mathf.Abs(horizontal));
Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
this.transform.rotation = (this.transform.rotation * deltaRotation);
}
}
public bool isInPivot(){
return stateInfo.fullPathHash == m_LocomotionPivotLId ||
stateInfo.fullPathHash == m_LocomotionPivotRId ||
transInfo.nameHash == m_LocomotionPivotLTransId ||
transInfo.nameHash == m_LocomotionPivotRTransId;
}
public bool IsInLocomotion(){
return stateInfo.fullPathHash == m_LocomotionId;
}
}
It turned out that the guy who made the original code was using a dampTime on his speed due to which he wouldn't instantly stand still if he'd let go off a button. It's something he hadn't explained in his tutorial yet, so I must have missed it. Anyways I hope this might help anyone in the future with a similar problem.

Categories

Resources