How Do I Fix Error Parameter 'Speed' does not exist [closed] - c#

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

Related

Animator setFloat doesn't response C# Unity3d

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;
}

My primary character controls the newly joined session character

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?

Flickering IsGrounded function

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);
}
}

Experiencing a weird collision bug between box colliders and tilemaps

This is my player movement code:
Footage of bug and inspector elements:
https://imgur.com/a/bmGqL1M
As you can see in this Imgur video, for some reason my player character, and also a pushable crate, can clip into this tilemap for a few pixels. I'm not quite sure why this is happening, since I've added a composite collider2D to the tilemap, to get rid of the very common "getting stuck in tilemap" bug, which isn't the case here, the clipping doesn't actually alter the movement in any way.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Movement")]
[SerializeField] private float movementSpeed;
[SerializeField] private float jumpForce;
[Header("Jumping")]
private bool isGrounded;
[SerializeField] private Transform feetPos;
[SerializeField] private float checkRadius;
[SerializeField] private LayerMask whatIsGround;
[SerializeField] private float hangTime;
private float hangCounter;
private Rigidbody2D rb;
private SpriteRenderer sr;
private float moveX;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
GetInput();
BetterJump();
}
void BetterJump()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded)
{
hangCounter = hangTime;
} else
{
hangCounter -= Time.deltaTime;
}
if (hangCounter > 0 && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * .5f);
}
}
void FixedUpdate()
{
Move();
}
void Move()
{
rb.position += new Vector2(moveX, 0) * Time.deltaTime * movementSpeed;
}
void GetInput()
{
moveX = Input.GetAxisRaw("Horizontal");
if(moveX < 0)
{
sr.flipX = true;
}
else if (moveX > 0)
{
sr.flipX = false;
}
}
}

How do I add running and animations to my player controller script?

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;
}
}

Categories