Unity C# Animator Transitions - c#

I'm trying to get my enemies run animation to play when he is moving, and switch back to idle when he has stopped. However my current code doesn't seem to be doing this and instead my enemy remains in the idle state constantly. I have checked that my variables are being set but they just don't seem to be getting filtered through to my animator to make the transitions. I also have an error which doesn't seem to stop the game from playing but pops up in the console. The error is Controller 'Pirate': Transition " in state 'Idle_Pirate' uses parameter 'walking' which is not compatible with condition type. I assume this is the culprit but after trying a few different suggestions from googling I am struggling to find a solution. This is the code from the script attached to my enemy. Apologies if it is a little crude I am still learning. Any help is greatly appreciated.
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
public float walkSpeed = 2.0f;
public float wallLeft = 0.0f;
public float wallRight = 2.0f;
float walkingDirection = 1.0f;
Vector3 walkAmount;
float timeCheck = 0.0f;
float walkCheck = 0.0f;
public float maxSpeed = 5f;
bool facingRight = true;
bool idle = true;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
}
void Update () {
if (timeCheck >= 2.0f) {
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight) {
walkingDirection = -1.0f;
Flip ();
} else if (walkingDirection < 0.0f && transform.position.x <= wallLeft) {
walkingDirection = 1.0f;
Flip ();
}
walkCheck = walkCheck + Time.deltaTime;
idle = false;
}
if (walkCheck >= 2.0f) {
idle = true;
walkAmount.x = 0;
timeCheck = 0.0f;
walkCheck = 0.0f;
}
timeCheck = timeCheck + Time.deltaTime;
transform.Translate(walkAmount);
anim.SetBool ("walking", idle);
}
void Flip () {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}

Managed to figure it out myself anyway, turns out I was using my animation in my animator instead of my sprites, must of dragged the wrong thing at some point. Thanks to those who took the time to read anyway.

Related

In Unity player movement speed is not being added to the first bullet fired

I'm trying to make a 2D top-down shooter in Unity. I want it so when the you hold down the left mouse button the player fires a series of bullets until you run out of ammo. The player's movement speed is slowed while firing and the players movement speed should be added to the bullet's movement speed. For some reason the players movement speed is only applied to the bullets AFTER the first bullet is fired. The first bullet always seems to keep the slightly faster 'sprint' movement speed.
Weapon script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
private GameObject bulletPrefab;
private Transform firePoint;
private PlayerControls player;
public float fireForce = 10f;
private bool cooldown = false;
private int bullets;
public int bulletDamage;
public int maxAmmo;
public float fireRate = 0.5f;
public float reloadRate = 2.5f;
public bool noAmmo = false;
public float walkSpeed = 2f;
private float timeSinceLastShot = 0f;
void Update()
{
// increase time since last shot
timeSinceLastShot += Time.deltaTime;
// if left-click is held down
if (Input.GetMouseButton(0))
{
// if enough time has passed since last shot
if (timeSinceLastShot >= fireRate && noAmmo == false)
{
player.moveSpeed = walkSpeed;
bullets -= 1;
cooldown = true;
if (bullets <= 0)
{
noAmmo = true;
player.moveSpeed = player.baseSpeed;
}
// instantiate a bullet
GameObject bullet = Instantiate(bulletPrefab, firePoint.transform.position, Quaternion.identity);
bullet.GetComponent<Bullet>().bulletDamage = bulletDamage;
// add player movement speed to bullet's speed
bullet.GetComponent<Rigidbody2D>().velocity = player.GetComponent<Rigidbody2D>().velocity;
bullet.GetComponent<Rigidbody2D>().AddForce(transform.up * fireForce, ForceMode2D.Impulse);
// reset time since last shot
timeSinceLastShot = 0f;
}
}
// if left-click is not held down
else
{
cooldown = false;
// restore player movement speed
player.moveSpeed = player.baseSpeed;
}
}
public void FillMag()
{
bullets = maxAmmo;
noAmmo = false;
}
}
PlayerControls:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public float moveSpeed = 5f;
public float baseSpeed = 5f;
public int health;
public Weapon weapon;
private Rigidbody2D rb;
Vector2 mousePosition;
Vector2 moveDirection;
public float walkSpeed = 2f;
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = aimAngle;
}
}
The player is moving from left to right.
Player firing
According to Unity's lifecycle, inputs are only calculated just before the Update method is called, but physics are applied during the FixedUpdate method. Is this what is causing my problems? I've tried moving some calculations into FixedUpdate and LateUpdate but nothing seems to make any difference.
Any help is appreciated. I've been banging my head against this for a few days now. I'm an amature, so feel free to explain like I'm 5.
So there are 2 problems with your code:
if you are not holding down left mouse, but multiple click instead:
the else statement in the Update() will immediately set it player back to base speed, which is not really matter if you intent to holding your mouse.
There are 2 concurrent things happen
first, you set player moveSpeed in Weapon Update()
at the same time, you calculate player velocity in Player Update()
But it TAKES TIME for player to update velocity before you get it right to your weapon.
Therefore, I recommend you to use IEnumerator to delay the fire action.
public class Weapon : MonoBehaviour
{
void FixedUpdate()
{
// increase time since last shot
timeSinceLastShot += Time.deltaTime;
// if left-click is held down
if (Input.GetMouseButton(0))
{
// if enough time has passed since last shot
if (timeSinceLastShot >= fireRate && noAmmo == false)
{
//player.moveSpeed = walkSpeed;
StopAllCoroutines();
StartCoroutine(SetSpeed());
bullets -= 1;
cooldown = true;
if (bullets <= 0)
{
noAmmo = true;
player.moveSpeed = player.baseSpeed;
}
// instantiate a bullet
StartCoroutine(FireBulelt());
// reset time since last shot
timeSinceLastShot = 0f;
}
}
// if left-click is not held down
}
IEnumerator SetSpeed()
{
player.moveSpeed = walkSpeed;
yield return new WaitForSeconds(0.5f);
cooldown = false;
// restore player movement speed
player.moveSpeed = player.baseSpeed;
yield return null;
}
IEnumerator FireBulelt()
{
yield return new WaitForSeconds(0.05f);
GameObject bullet = Instantiate(bulletPrefab, player.transform.position, Quaternion.identity);
print(" player.GetComponent<Rigidbody2D>().velocit " + player.GetComponent<Rigidbody2D>().velocity);
// add player movement speed to bullet's speed
Rigidbody2D bulletRB = bullet.GetComponent<Rigidbody2D>();
print(" bulletRB.velocit " + bulletRB.velocity);
bulletRB.velocity = player.GetComponent<Rigidbody2D>().velocity;
print(" after bulletRB.velocit " + bulletRB.velocity);
bulletRB.AddForce(transform.up * fireForce, ForceMode2D.Impulse);
}
}
And you should add print() like I did to keep track of code behavior when you're debugging.

Problem with calling a function only one time in unity

i'm fairly new to programming, and i have a problem with this code for my unity game, basically what happen is that the first time i try to call SelectGravity() it do it 2 or 3 times, after that it seems to work correctly, i can't figure out why.
IsRotating = false is called in another script and i'm using visual studio 2019 for coding, if this can help.
void Update()
{
Rotation = Input.GetAxisRaw("RotatoWorld");
if (Rotation != 0 && !IsRotating)
{
IsRotating = true;
SelectGravity();
}
Physics2D.gravity = new Vector2(XGravity, YGravity);
}
For those who asked this is the script in which i set IsRotating false
public IEnumerator Rotate90()
{
if(changeGravity.Rotation > 0.1)
{
Direction = 90;
}
else if(changeGravity.Rotation < -0.1)
{
Direction = -90;
}
float timeElapsed = 0;
Quaternion startRotation = transform.rotation;
Quaternion targetRotation = transform.rotation * Quaternion.Euler(0, 0, Direction);
while (timeElapsed < lerpDuration)
{
transform.rotation = Quaternion.Slerp(startRotation, targetRotation, timeElapsed / lerpDuration);
timeElapsed += Time.deltaTime;
yield return null;
}
transform.rotation = targetRotation;
StartCoroutine(CameraShake());
changeGravity.IsRotating = false;
}
and i've declared it like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeGravity : MonoBehaviour
{
CameraRotation cameraRotation;
PlayerController playerController;
private float XGravity;
private float YGravity;
public float Side;
public float Rotation;
public bool IsRotating;
// Start is called before the first frame update
void Start()
{
cameraRotation = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraRotation>();
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
Side = 0;
YGravity = -9.81f;
XGravity = 0f;
IsRotating = false;
}
Other than this i don't call this function or modify this variable anywhere.
Check the IsRotating boolean. It should modifying somewhere else or bool decleration function is not correct.
I found the problem was that the script was attached to more than one object, consequentially void update() were called two times per frame.

How to program a bunch of 1st person animations at different speeds

I have made 4 different types of animation clips in an animator for an an empty gameobject called "Animator". The main camera is a child of this.
The animations feature a running cycle, a walking cycle, a crouch cycle, and an idle cycle. They all have a trigger that let's them play.
How am I able to measure the speed of the player and execute these animations when the player reaches a certain speed. I have found someone else trying to do this and it works but only for idle and walk. But unfortunately I can't get the sprint and crouch to work. I'm not sure what to do, either have the sprint and crouch animations or just change the speed of the walk animation depending on whether the player is sprinting or crouching. I'll leave a comment where the code I found is.
Here's what I have in my player controller (thge trigger stop is for the idle animation):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
Animator _ar;
public float speed;
[Range(-5, -20)]
public float gravity = -9.81f;
public float sprintSpeed = 6f;
public float walkSpeed = 4f;
public float crouchSpeed = 2f;
public float standHeight = 1.6f;
public float crouchHeight = 1f;
Vector3 velocity;
bool isGrounded;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public Light _l;
//Set this to the transform you want to check
public Transform objectTransfom;
private float noMovementThreshold = 0.0001f;
private const int noMovementFrames = 1;
Vector3[] previousLocations = new Vector3[noMovementFrames];
public bool isMoving;
//Let other scripts see if the object is moving
public bool IsMoving
{
get { return isMoving; }
}
void Awake()
{
//For good measure, set the previous locations
for (int i = 0; i < previousLocations.Length; i++)
{
previousLocations[i] = Vector3.zero;
}
}
void Start()
{
_ar = GameObject.Find("Animator").GetComponentInChildren<Animator>();
}
// Update is called once per frame
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;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//Below here is the code I found. The if statements for isMoving, is what I put in to see if
//this worked.
//Store the newest vector at the end of the list of vectors
for (int i = 0; i < previousLocations.Length - 1; i++)
{
previousLocations[i] = previousLocations[i + 1];
}
previousLocations[previousLocations.Length - 1] = objectTransfom.position;
//Check the distances between the points in your previous locations
//If for the past several updates, there are no movements smaller than the threshold,
//you can most likely assume that the object is not moving
for (int i = 0; i < previousLocations.Length - 1; i++)
{
if (Vector3.Distance(previousLocations[i], previousLocations[i + 1]) >= noMovementThreshold)
{
//The minimum movement has been detected between frames
isMoving = true;
break;
}
else
{
isMoving = false;
}
}
if(isMoving == true)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = sprintSpeed;
_ar.SetTrigger("WalkSprint");
}
else if (Input.GetKeyUp(KeyCode.LeftControl))
{
speed = crouchSpeed;
_ar.SetTrigger("WalkCrouch");
//transform.localScale = new Vector3(0.8f, 0.5f, 0.8f);
}
else
{
speed = walkSpeed;
_ar.SetTrigger("Walk");
//transform.localScale = new Vector3(0.8f, 0.85f, 0.8f);
}
}
else
{
_ar.SetTrigger("Stop");
}
}
}
Unfortunately, as with many issue in Game Dev, this could be a number of different issues (or all of them!). Here is where you can start to debug:
Check your error log to see if there is anything obvious that jumps out at you, like a bad reference to a Game Object/Componenet.
Watch the animator. You should be able to have the Animator open, side-by-side with your game window while the game is running. You should be able to see the animations running and transitioning. See if something is not linked properly, or if an animation time is configured incorrectly, etc.
Add some debug statements, like outputting the current trigger being set. I would even verify that your inputs are configured correctly. Maybe add some additional conditionals at the beginning that debug what inputs are being pressed.
As #OnionFan said, your Input check is for KeyUp for the Crouch key. That should probably be KeyDown.

Unity2D, can't sync my sprite across all clients (multiplayer)

I'm trying to learn about Unet, but I'm having trouble getting the hang of it, despite following a few tutorials.
I am making a 2d game, and what I'm currently stuck at, is updating the way my characters sprite is turning (left or right).
My player prefab has: Sprite renderer, rigidbody2D, Network Identity(set to local player authority) and Network Transform.
This is the code attached to each player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class CharController : NetworkBehaviour {
public float maxSpeed = 1f;
[SyncVar]
bool facingRight = true;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
void Moving()
{
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if(move > 0 && !facingRight)
{
RpcFlip();
}
else if(move < 0 && facingRight)
{
RpcFlip();
}
anim.SetFloat("MovingSpeed", Mathf.Abs(move));
}
private void FixedUpdate()
{
if (!isLocalPlayer)
{
return;
}
// handle input here...
Moving();
}
[ClientRpc]
void RpcFlip()
{
if (isLocalPlayer)
{
//facingRight = !facingRight;
//currentSprite.flipX = !currentSprite.flipX;
}
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
I know it's most likely something super simple I'm doing wrong, which just adds more pain to asking this, but any help is greatly appreciated! Thanks
Edit: The way my game is setup, one of the players is both a host and a client, I don't know if this makes it harder, but that is just the way unity does it, when playing locally.
I figured it out after reading the first 5 pages on google, and here is my final code, in case anyone else with the same problem stumbles in here:
public float maxSpeed = 1f;
Animator anim;
[SyncVar(hook = "FacingCallback")] //Everytime the bool netFacingRight is called, the method FacingCallback is called aswell.
//Since the bool only changes when CmdFlipSprite is called, it makes sure that CmdFlipSprite calls
//the change on our server, and FacingCallback calls it locally.
public bool netFacingRight = true;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
[Command]
public void CmdFlipSprite(bool facing)
{
netFacingRight = facing;
if (netFacingRight)
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = 1;
transform.localScale = SpriteScale;
}
else
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = -1;
transform.localScale = SpriteScale;
}
}
void FacingCallback(bool facing)
{
netFacingRight = facing;
if (netFacingRight)
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = 1;
transform.localScale = SpriteScale;
}
else
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = -1;
transform.localScale = SpriteScale;
}
}
////////
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
return;
}
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if ((move > 0 && !netFacingRight) || (move < 0 && netFacingRight))
{
netFacingRight = !netFacingRight;
CmdFlipSprite(netFacingRight);
anim.SetFloat("MovingSpeed", Mathf.Abs(move));
}
}
You aparently have to call the function both locally and on the server!

C# Unity Character jumps really weird

Hello guys!
I have a problem with my UNITY 5 code.
My character can jump but he jumps instantly and too fast.
It looks really weird.
I would greatly appreciate your feedback on my code!
using UnityEngine;
using System.Collections;
public class Gravity : MonoBehaviour {
private float inputDirection;
private float VerticalSpeed;
private float gravity = -2f;
private float speedmultiplier = 5f;
private bool jump;
private Vector3 moveVector;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
inputDirection = Input.GetAxis ("Horizontal");
moveVector = new Vector3 (inputDirection, VerticalSpeed, 0) * speedmultiplier;
controller.Move (moveVector * Time.deltaTime);
if (controller.isGrounded) {
VerticalSpeed = 0.0f;
jump = true;
} else {
VerticalSpeed = gravity;
jump = false;
}
if (Input.GetKey(KeyCode.X)) {
if(jump == true) {
VerticalSpeed += 25f;
}
}
}
}
You can try changing the vertical speed in your else to reflect a change over time.
Perhaps something like:
VerticalSpeed += gravity * Time.deltaTime
Instead of just setting it to gravity. You may need to play with your initial jump speed to get it to feel better, but this should start your jump fast-ish, slow down as you reach the top of the jump, and speed back up as you fall.

Categories