Change AI Enemy Animation - c#

Currently, I'm making a game in unity, and I made a simple script to make the enemy follow the player but I can't change the animation of the enemy to the other side
gameplay:
EnemyController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
[Header("References")]
private Animator animator;
// The target to follow.
private Transform target;
private Rigidbody2D rb;
[Header("Movement")]
public float speed;
public float range;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
target = FindObjectOfType<PlayerMovement>().transform;
}
// Update is called once per frame
void Update()
{
FollowPlayer();
}
private void FollowPlayer()
{
animator.SetBool("isRunning", true);
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}

I fix it by using blend tree
Blend Tree:
Script:
private void FollowPlayer()
{
animator.SetBool("isRunning", true);
animator.SetFloat("Horizontal", target.position.x - transform.position.x);
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}

Related

Unity2d: the player stop moving when I add an animation

I have a problem in unity , the player mouvement is going good until I add an animation the player stop moving even if I press the keyboard keys,when I remove the animator compenent the player move normaly without problems !
I tried separate the animation script from the movement script and still theame problem , I don't think that the problem is comming from the code
playerAnimation code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerAnim : MonoBehaviour
{
Animator anim;
Rigidbody2D rb;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void FixedUpdate()
{
if (rb.velocity.x == 0)
anim.SetBool("isRunning", false);
else
anim.SetBool("isRunning", true);
if (rb.velocity.y > 0)
anim.SetBool("isJumping", true);
else
anim.SetBool("isJumping", false);
}
}
playerMovement script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControl : MonoBehaviour
{
Rigidbody2D rb;
private float horizontal;
public float runSpeed;
public float jumpPower;
public bool inTheGround;
private SpriteRenderer sp;
Animator anim;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
sp = gameObject.GetComponent<SpriteRenderer>();
anim = gameObject.GetComponent<Animator>();
}
private void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * runSpeed,
rb.velocity.y);
if (Input.GetButton("Jump")&& inTheGround)
{
rb.velocity = new Vector2(rb.velocity.x,jumpPower);
}
flipping();
Debug.Log(rb.velocity.x);
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("ground"))
inTheGround = true;
}
private void OnCollisionExit2D(Collision2D other)
{
if
(other.gameObject.CompareTag("ground")&&rb.velocity.y>0.1)
inTheGround = false;
}
void flipping()
{
if (Input.GetKey(KeyCode.RightArrow))
sp.flipX = false;
if (Input.GetKey(KeyCode.LeftArrow))
sp.flipX = true;
}
}
Check if Apply Root Motion on Animator Component is set to false. This setting can overwrite your changes of the object's position over time. if not - can you please provide more information, perfectly a screenshot of your player components, and Animator Controller.

How do I fix my 2D player movement in Unity?

I'm new to unity and have been trying to get the 2D Playermovement right for quite some time. Yet for some reason this error keeps popping up, anyone have any ideas?
this is the code I've been using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2d rb2D;
private float MovementSpeed;
private float JumpForce;
private bool IsJumping;
private float moveHorizontal;
private float moveVertical;
// Start is called before the first frame update
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2d>();
MovementSpeed = 3f;
JumpForce = 60f;
IsJumping = false;
}
// Update is called once per frame
void Update()
{
moveHorizontal = Input.GetAxisRaw("Horizontal");
moveVertical = Input.GetAxisRaw("Vertical");
}
void FixedUpdate ()
{
if(moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * MovementSpeed, 0f), ForceMode2D.Impulse);
}
}
}
Typo: It should be Rigidbody2D and not Rigidbody2d

My code to move in unity 2D game not working correctly

There is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovementScript : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value){
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, rigidbody.velocity.y);
rigidbody.velocity = moveInput;
}
}
But my character still mowing with speed of 2f and can fly up and down (i did it in first version of my code).
It looks like game not loaded newest code and don't know how to fix it.
I have selected auto refresh.

How to make sprites spawn in and disappear on screenbounds in unity

I'm trying to create a game where asteroids spawn in and despawn when touching a screenbound, I've looked at many tutorials but these doesn't work for me or say I got something wrong in the code while the code seems to be fine, how do I make it so they spawn in at random and despawn when they touch the screenbounds?
Code I got for now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class asteroid : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
private Vector2 screenBounds;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, -speed);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update()
{
if (transform.position.y < screenBounds.y)
{
Destroy(this.gameObject);
}
}
}
Try to check if the Renderer component is visible:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class asteroid : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
private Vector2 screenBounds;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, -speed);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update()
{
if (!GetComponent<Renderer>().isVisible)
{
Destroy(this.gameObject);
}
}
}
You can use this function:
void OnBecameInvisible()
{
Destroy(gameObject);
}

how do i drag and launch in untiy 2d?

I am a newbie in c# and i have been trying to get some code that will help me launch my character in the opposite direction of the drag. Like Angry Birds but i don't want the character to move while i am dragging. Here is my script that i copied from a video and it isn't working.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbMovement : MonoBehaviour
{
public float launchSpeed = 100;
Vector3 _initialPosition;
Rigidbody2D rb;
private Vector2 directionToInitialPosition;
private void Awake()
{
_initialPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
directionToInitialPosition = _initialPosition - transform.position;
}
private void OnMouseUp()
{
rb.AddForce(directionToInitialPosition *launchSpeed);
}
private void OnMouseDrag()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(newPosition.x, newPosition.y);
}
}
`
help me with a script that will launch my character opposite of the drag. Thanks in advance :)
Why don't you just remove the
private void OnMouseDrag()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(newPosition.x, newPosition.y);
}
this part of the script means that everytime the player drag the mouse/touch then the gameobject will follow the mouse. Because you don't want it, you can just remove it to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbMovement : MonoBehaviour
{
public float launchSpeed = 100;
Vector3 _initialPosition;
Rigidbody2D rb;
private Vector2 directionToInitialPosition;
private void Awake()
{
_initialPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
directionToInitialPosition = _initialPosition - transform.position;
}
private void OnMouseUp()
{
rb.AddForce(directionToInitialPosition *launchSpeed);
}
}

Categories