IsGrounded Function started flickering when I added OBJs to my project. It was working perfectly before I started working on it today it only started after I added some material and OBJS. I tried reverting to the state it was in before I added different elements but the problem still happened. It always occurs after about 30 seconds of running the game in the window. How can I fix this?
\using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//VARIABLES
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
public float _speed = 10;
public float _rotationSpeed = 180;
private Vector3 moveDirection;
private Vector3 Velocity;
private Vector3 rotation;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
[SerializeField] private float jumpHeight;
//REFERNCES
private CharacterController controller;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
Move();
this.rotation = new Vector3(0, Input.GetAxisRaw("Horizontal") * _rotationSpeed * Time.deltaTime, 0);
Vector3 move = new Vector3(0, 0, Input.GetAxisRaw("Vertical") * Time.deltaTime);
move = this.transform.TransformDirection(move);
controller.Move(move * _speed);
this.transform.Rotate(this.rotation);
}
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if (isGrounded && Velocity.y < 0)
{
Velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
if (isGrounded)
{
if (moveDirection != Vector3.zero && !Input.GetButtonDown("Fire3"))
{
Walk();
}
else if (moveDirection != Vector3.zero && Input.GetButtonDown("Fire3"))
{
Run();
}
else if (moveDirection == Vector3.zero)
{
Idle();
}
moveDirection *= moveSpeed;
if (Input.GetButtonDown("Jump"))
{
Jump();
}
}
controller.Move(moveDirection * Time.deltaTime);
Velocity.y += gravity * Time.deltaTime;
controller.Move(Velocity * Time.deltaTime);
}
private void Idle()
{
}
private void Walk()
{
moveSpeed = walkSpeed;
}
private void Run()
{
moveSpeed = runSpeed;
}
private void Jump()
{
Velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 hours ago.
Improve this question
I Tried To Follow A Tutorial On FPS Movement And The Animation Isn't Working
This Is My Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class PlayerController : MonoBehaviour
{
[Header("Move Variables")]
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
[SerializeField] private float jumpForce;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
[Header("Gravity")]
[SerializeField] private float gravity;
[SerializeField] private float groundDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private bool isCharacterGrounded = false;
private Vector3 velocity = Vector3.zero;
[Header("Animations")]
private Animator anim;
private void Start()
{
GetReferences();
InitVariables();
}
private void Update()
{
HandleIsGrounded();
HandelJumping();
HandelGravity();
HandelRunning();
HandleMovement();
HandleAnimations();
}
private void HandleMovement()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
moveDirection = new Vector3(moveX, 0, moveZ);
moveDirection = moveDirection.normalized;
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection * moveSpeed * Time.deltaTime);
}
private void HandelRunning()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
moveSpeed = runSpeed;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
moveSpeed = walkSpeed;
}
}
private void HandleAnimations()
{
if(moveDirection == Vector3.zero)
{
GetComponentInChildren<Animator>().SetFloat("Speed", 0);
}
else if(moveDirection != Vector3.zero && !Input.GetKeyDown(KeyCode.LeftShift))
{
GetComponentInChildren<Animator>().SetFloat("Speed", 0.5f);
}
else if(moveDirection != Vector3.zero && Input.GetKeyDown(KeyCode.LeftShift))
{
GetComponentInChildren<Animator>().SetFloat("Speed", 1);
}
}
private void HandleIsGrounded()
{
isCharacterGrounded = Physics.CheckSphere(transform.position, groundDistance, groundMask);
}
private void HandelGravity()
{
if (isCharacterGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void HandelJumping()
{
if (Input.GetKeyDown(KeyCode.Space) && isCharacterGrounded == true)
{
velocity.y += Mathf.Sqrt(jumpForce * -2 * gravity);
}
}
private void GetReferences()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
private void InitVariables()
{
moveSpeed = walkSpeed;
}
}
thank you
fajfewajfoiajfwsekflikwsekfwejkfjwsfcjusjefjjewjfjewjkfjwoljufjfjwe "just putting this to meet the detail requierments"sliojwfoljejofjfjfjolesjgforgopiefjfpeff dgjeoigjerug regjeogoefigj ogeigjoidg oiejgiodjfgoierf goidejgiojgioe goedjoigjerogj erogjiregjeitrjug oiejgioerjgoiejg ierjgoiegjoier gioerfjgioerjgioer goiegjorejgioer goe
I'm making 3d animation for charachter movement, and Animator.SetFloat(...) doesn't response in functions that I call in if statements, code below. Interesting thing that these Debug.Log's work
properly, when I walk console has "Walk", when I run console has "Run" etc.
I think I have problem with Scope, but I'm really sure.
Screenshots: animator props console logs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
[SerializeField] private float groundCheckDistance;
[SerializeField] private float gravity;
[SerializeField] private bool isGrounded;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float jumpHeight;
private Vector3 moveDirection;
private Vector3 velocity;
private CharacterController controller;
private Animator anim;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
void Update()
{
Move();
}
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if(isGrounded && velocity.y < 0) velocity.y = -2f;
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space)) Jump();
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) Walk();
else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift)) Run();
Idle();
moveDirection *= moveSpeed;
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Idle()
{
anim.SetFloat("speed", 0);
}
private void Walk()
{
anim.SetFloat("speed", 0.5f);
moveSpeed = walkSpeed;
Debug.Log("Walk");
}
private void Run()
{
moveSpeed = runSpeed;
Debug.Log("Run");
anim.SetFloat("speed", 1);
}
private void Jump()
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
isGrounded = false;
}
}
Ok, I've found the solution, I missed "else", because of that Function Idle() always had control under Animator.SetFloat(...)
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space)) Jump();
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) Walk();
else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift)) Run();
else Idle(); //this how it must looks like
moveDirection *= moveSpeed;
}
This is my movement script.
public class PlayerMovement : MonoBehaviour
{
// VARIABLES
public float movementSpeed;
public float walkSpeed;
public float runSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
public bool isGrounded;
public float groundCheckDistance;
public LayerMask groundMask;
public float gravity;
// References
public CharacterController controller;
public Animator anim;
PhotonView view;
private void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
view = GetComponent<PhotonView>();
}
private void Update()
{
if (view.IsMine)
{
Move();
}
}
private void Move()
{
if (view.IsMine)
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
// WALK
Walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
// RUN
Run();
}
else if(moveDirection == Vector3.zero)
{
// IDLE
Idle();
}
moveDirection *= movementSpeed;
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
private void Idle() {
if (view.IsMine)
{
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
}
private void Walk()
{
if (view.IsMine)
{
movementSpeed = walkSpeed;
anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
}
}
private void Run(){
if (view.IsMine)
{
movementSpeed = runSpeed;
anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class SpawnPlayers : MonoBehaviour
{
public GameObject playerPrefab;
public float minX;
public float maxX;
public float minZ;
public float maxZ;
public float minY;
public float maxY;
// Start is called before the first frame update
private void Start()
{
Vector3 randomPosition = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ));
player = PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
}
}
The code above is the join session code
I also have a really basic camera function that just allows the character to look but I don't think anything is wrong with that. The view.IsMine at least makes it so that the users can not control both characters at the same time but it still allows for other person controller and can't control his own character. Is there anyone that knows how to fix this issue?
i am using a 2 scripts, a player movement and a camera follow script. while jumping normally the jump is smooth, it is also smooth while moving my character. what i understand is that the camera follow script is somehow not allowing the jump to be smooth.here is the player movement script.
public class player_movement : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float jumpForce;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
movement();
}
private void movement()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (isGrounded == false && moveInput == 0)
{
anim.SetBool("is running", false);
}
else if (isGrounded == true && moveInput != 0)
{
anim.SetBool("is running", true);
}
else if(isGrounded == true && moveInput == 0)
{
anim.SetBool("is running", false);
}
else if (isGrounded == false && moveInput != 0)
{
anim.SetBool("is running", false);
}
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector2(0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector2(0, 180);
}
Jump();
}
private void Jump()
{
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space))
{
if (jumpTimeCounter > 0 && isJumping == true)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
private void LateUpdate()
{
//Jump();
}
}
and here is the camera follow script.
public class cammeraFollow : MonoBehaviour
{
public GameObject followObject;
public Vector2 followOffset;
public float speed = 3f;
private Vector2 threshold;
private Rigidbody2D rb;
void Start()
{
threshold = calculateThreshold();
rb = followObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 follow = followObject.transform.position;
float xDifference = Vector2.Distance(Vector2.right * transform.position.x, Vector2.right * follow.x);
float yDifference = Vector2.Distance(Vector2.up * transform.position.y, Vector2.up * follow.y);
Vector3 newPosition = transform.position;
if(Mathf.Abs(xDifference) >= threshold.x)
{
newPosition.x = follow.x;
}
if (Mathf.Abs(yDifference) >= threshold.y)
{
newPosition.y = follow.y;
}
float moveSpeed = rb.velocity.magnitude > speed ? rb.velocity.magnitude : speed;
transform.position = Vector3.MoveTowards(transform.position, newPosition, moveSpeed * Time.deltaTime);
}
private Vector3 calculateThreshold()
{
Rect aspect = Camera.main.pixelRect;
Vector2 t = new Vector2(Camera.main.orthographicSize * aspect.width / aspect.height, Camera.main.orthographicSize);
t.x -= followOffset.x;
t.y -= followOffset.y;
return t;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Vector2 border = calculateThreshold();
Gizmos.DrawWireCube(transform.position, new Vector3(border.x * 2, border.y * 2, 1));
}
}
can anyone help me with this one.
FixedUpdate is not called every frame but in fixed intervals instead. It's more for physics and gameplay code.
Try changing the camera follow script to Update.
I am new to Unity and have been having a hard time adding running and animations (I have the animations and they are set up in animator) to my FPS controller script. Can someone please help me add running and animations? I would be extremely grateful.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Animator anim;
float RotateX;
float RotateY;
public static bool GamePaused;
[SerializeField]
[Header("Game Objects")]
public GameObject Camera;
public GameObject PauseMenu;
public GameObject Player;
[Header("Movement Settings")]
public float WalkSpeed = 5.0f;
public float RunSpeed = 10.0f;
[Header("Rotation Settings")]
public float RorationSpeed;
public float MaxYAxis = 60.0f; // right
public float MinYAxis = -48.0f; // left
public bool Grounded;
private void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);
RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
RotateY += Input.GetAxis("Mouse Y") * RorationSpeed;
RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
}
}
Here is what I have to trigger the different animations in the animator, but the animation follows through when I release the key rather than switching immediately upon release.
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) ||
Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) ||
Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) ||
Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.RightArrow))
{
anim.SetBool("IsWalking", true);
}
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyDown(KeyCode.A) ||
Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) ||
Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) ||
Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.RightArrow))
{
anim.SetBool("IsWalking", false);
}
What I can suggest is that you use the horizontal and vertical axes, rather than hard-coded key presses.
More specifically, first of all (if you have not already) in the animator window create an idle state and a walking animation state. You will need somehow to transition between the two states. To do that you will need to create a new bool parameter (let's name it "isWalking" as you did) and create a new condition between the idle and the walking state. For example, set to transition between "idle" to "walking" when isWalking is true and transition between "walking" to "idle" when isWalking is false.
Now in your PlayerController script in the update or fixed update you can add the following code
horizontalMovement = Input.GetAxis("Horizontal");
verticalMovement = Input.GetAxis("Vertical");
//normalize vector so movement in two axis simultanesly is balanced.
moveDirection = (horizontalMovement * transform.right + verticalMovement * transform.forward).normalized;
/* based on your code although a rigid body solution or character controller would have been more robust */
transform.Translate(moveDirection * WalkSpeed * Time.deltaTime);
if (horizontal != 0 || vertical != 0)
{
animator.setFloat("isWalking",true);
}
else
{
animator.setFloat("isWalking",false);
}
However this solution is working is implemented based on the code you provided. If you want to switch in a more robust and easy to maintain script you can use this free controller that uses a rigidbody and has animations already installed and working.
https://github.com/PanMig/First-Person-Unity-Camera
Here is my working script with walking, running, jumping and animation triggers.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Animator anim;
float RotateX;
float RotateY;
Rigidbody rb;
public static bool GamePaused;
public bool isGrounded;
[SerializeField]
[Header("Game Objects")]
public GameObject Camera;
public GameObject PauseMenu;
public GameObject Player;
[Header("Movement Settings")]
public float DefaultSpeed = 5.0f;
public float WalkSpeed = 5.0f;
public float RunSpeed = 10.0f;
public float jumpForce = 2.5f;
public Vector3 jump;
[Header("Rotation Settings")]
public float RorationSpeed = 3.0f;
public float MaxYAxis = 60.0f;
public float MinYAxis = -48.0f;
private void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 1.0f, 0.0f);
}
void Update()
{
Rotation();
Movement();
Bool();
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void Rotation()
{
RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
RotateY += Input.GetAxis("Mouse Y") * RorationSpeed;
RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
}
void Movement()
{
transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
WalkSpeed = RunSpeed;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
WalkSpeed = DefaultSpeed;
}
}
void Bool()
{
if (Input.GetKeyDown(KeyCode.W))
{
anim.SetBool("IsWalkingForward", true);
}
if (Input.GetKeyDown(KeyCode.A))
{
anim.SetBool("IsWalkingLeft", true);
}
if (Input.GetKeyDown(KeyCode.S))
{
anim.SetBool("IsWalkingBack", true);
}
if (Input.GetKeyDown(KeyCode.D))
{
anim.SetBool("IsWalkingRight", true);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
anim.SetBool("IsWalkingForward", true);
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
anim.SetBool("IsWalkingLeft", true);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
anim.SetBool("IsWalkingBack", true);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
anim.SetBool("IsWalkingRight", true);
}
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("IsWalkingForward", false);
}
if (Input.GetKeyUp(KeyCode.A))
{
anim.SetBool("IsWalkingLeft", false);
}
if (Input.GetKeyUp(KeyCode.S))
{
anim.SetBool("IsWalkingBack", false);
}
if (Input.GetKeyUp(KeyCode.D))
{
anim.SetBool("IsWalkingRight", false);
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
anim.SetBool("IsWalkingForward", false);
}
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
anim.SetBool("IsWalkingLeft", false);
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
anim.SetBool("IsWalkingBack", false);
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
anim.SetBool("IsWalkingRight", false);
}
}
void OnCollisionStay()
{
isGrounded = true;
}
void OnCollisionExit()
{
isGrounded = false;
}
}