Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Us3rDan · Aug 24, 2021 at 08:40 AM · animationparticlesysteminstantiation

Animation Effect Only Plays when character is walking left

When I walk left and Jump, it plays the effect that I want to, which is instantiating an animation at the feet, however, when I do the same thing but walking right, it doesn't play. Here is the code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovemnt : MonoBehaviour { public ParticleSystem jumpdust; public Transform _jumpEffectLocation; public GameObject _jumpEffect; public Transform Player;

 [Header("Components")]
 private Rigidbody2D _rb;
 private Animator _anim;
 
 [Header("Layer Masks")]
 [SerializeField]private LayerMask _groundLayer;

 [Header("Movement Varaibles")]
 [SerializeField] private float _movementAcceleration = 50f;
 [SerializeField] private float _maxMoveSpeed = 10f;
 [SerializeField] private float _linearDrag = 9f;
 private float _horizontalDirection;
 private bool _changingDirection => (_rb.velocity.x > 0f && _horizontalDirection < 0f) || (_rb.velocity.x < 0f && _horizontalDirection > 0f);
 private bool _facingRight = true;

 [Header("Jump Varialbes")]
 [SerializeField] private float _jumpForce = 12f;
 [SerializeField] private float _airLinearDrag = 2.5f;
 [SerializeField] private float _fallMultiplier = 8f;
 [SerializeField] private float _lowJumpFallMultiplier = 5f;
 private bool _canJump => Input.GetButtonDown("Jump") && _onGround;


 [Header("Ground Collision Variables")]
 [SerializeField] private float _groundRaycastLenght;
 private bool _onGround;


 private void Jump()
 {
     _rb.velocity = new Vector2(_rb.velocity.x, 0f);
     _rb.AddForce(Vector2.up * _jumpForce, ForceMode2D.Impulse);

     _anim.SetBool("isJumping", true);
     _anim.SetBool("isFalling", false);
 }

 private void Start()
 {
     _rb = GetComponent<Rigidbody2D>();
     _anim = GetComponent<Animator>();
 }

 // Update is called once per frame
 private void Update()
 {
     _horizontalDirection = GetInput().x;
     if (Input.GetButtonDown("Jump") && _onGround) Jump();
     if (_canJump)
     {
         
         Jump();
         Instantiate(_jumpEffect, _jumpEffectLocation.position, _jumpEffectLocation.rotation);
         
     }
     

     _anim.SetBool("isGrounded", _onGround);
     _anim.SetFloat("horizontalDirection", Mathf.Abs(_horizontalDirection));
     

     if (_horizontalDirection < 0f && _facingRight)
     {
         Flip();
     }
     else if (_horizontalDirection > 0f && !_facingRight)
     {
         Flip();
     }

     if(_rb.velocity.y < 0f)
     {
         _anim.SetBool("isJumping", false);
         _anim.SetBool("isFalling", true);
         
     }
 }
 private void FixedUpdate()
 {
     CheckCollisions();
     MoveCharacter();
     ApplyLinearDrag();
     if (_onGround)
     {
         ApplyLinearDrag();

         _anim.SetBool("isJumping", false);
         _anim.SetBool("isFalling", false);
         
     }
     else 
     {
         ApplyAirLinearDrag();
         FallMultiplier();
     }
 }

 private Vector2 GetInput()
 {
     return new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
 }

 private void MoveCharacter()
 {
     _rb.AddForce(new Vector2(_horizontalDirection, 0f) * _movementAcceleration);

     if (Mathf.Abs(_rb.velocity.x) > _maxMoveSpeed)
         _rb.velocity = new Vector2(Mathf.Sign(_rb.velocity.x) * _maxMoveSpeed, _rb.velocity.y);
        
 }

 private void ApplyLinearDrag()
 {
     if (Mathf.Abs(_horizontalDirection) < 0.4f || _changingDirection)
     {
         _rb.drag = _linearDrag;
     }
     else
     {
         _rb.drag = 0f;
     }
 }

 private void ApplyAirLinearDrag()
 {
     _rb.drag = _airLinearDrag;
 }

 private void CheckCollisions()
 {
     _onGround = Physics2D.Raycast(transform.position * _groundRaycastLenght, Vector2.down, _groundRaycastLenght, _groundLayer);
 }

 private void OnDrawGizmos()
 {
     Gizmos.color = Color.green;
     Gizmos.DrawLine(transform.position, transform.position + Vector3.down * _groundRaycastLenght);
 }

 private void FallMultiplier()
 {
     if (_rb.velocity.y < 0)
     {
         _rb.gravityScale = _fallMultiplier;
     }
     else if (_rb.velocity.y >0 && !Input.GetButton("Jump"))
     {
         _rb.gravityScale = _lowJumpFallMultiplier;
        
     }
     else
     {
         _rb.gravityScale = 1f;
     }
 }

 void Flip()
 {
     _facingRight = !_facingRight;
     transform.Rotate(0f, 180f, 0f);
 }

 void CreateJumpDust()
 {
     jumpdust.Play();
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

303 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I make animations snap to a frame? 1 Answer

How active my particle system made in blender in unity? 1 Answer

Teleporting old particle system 0 Answers

Have particles follow animated character 1 Answer

Prevent particles from passing through each other 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges