简体   繁体   中英

Why does my character only rotate to the target one time every time it doesn't move?

My question might sound confusing but I want to make my code so when my character doesn't move, he looks directly towards the closest enemy. The problem is that when I stop moving my character, it only does the command once and doesn't redirect to the enemy's new position. I tested out many things like making it so when i move, it looks at the enemy and it seems to work but whenever I set it to not move, it doesn't work.

using System.Collections.Generic;
using UnityEngine;
namespace work.working.worked
{
    public class Movement : MonoBehaviour
    {
        public float moveSpeed;
        public float rotationSpeed;
        public static bool ismoving;
        public Rigidbody2D rb;
        Vector2 movement;
        // Update is called once per frame
        void FixedUpdate()
        {
            rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        }
        void Update()
        {
            movement.x = Input.GetAxisRaw("Horizontal");
            movement.y = Input.GetAxisRaw("Vertical");
            if (Input.anyKey)
            {
                ismoving = true;
            }
            else
            {
                ismoving = false;
            }
            if (ismoving == true)
            {
                Vector2 direction = new Vector2(movement.x, movement.y);
                direction.Normalize();
                if (direction != Vector2.zero)
                {
                    Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, direction);
                    transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
                }
            }
            if (ismoving == false)
            {
                Vector3 direction = FindClosest.closestEnemy.position - transform.position;
                direction.Normalize();
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 85f;
                rb.rotation = angle;
            }
        }
    }
}

Instead of Input.anyKey you could check that the magnitude of movement vector to see if character is moving.

ismoving = movement.magnitude > 0.0f;
using System.Collections.Generic;
using UnityEngine;
namespace work.working.worked
{
    public class Movement : MonoBehaviour
    {
        public float moveSpeed;
        public float rotationSpeed;
        public bool ismoving;
        public Rigidbody2D rb;
        Vector2 movement;
        

        void FixedUpdate()
        {
            rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        }

        void Update()
        {
            movement.x = Input.GetAxisRaw("Horizontal");
            movement.y = Input.GetAxisRaw("Vertical");
            ismoving = movement.magnitude > 0.0f;

            if (ismoving)
            {
                Vector2 direction = new Vector2(movement.x, movement.y);
                direction.Normalize();
                if (direction != Vector2.zero)
                {
                    Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, direction);
                    transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
                }
            }
            else
            {
                Vector3 direction = FindClosest.closestEnemy.position - transform.position;
                direction.Normalize();
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 85f;
                rb.rotation = angle;
            }
        }
    }

Also don't make ismoving variable static or it gets set by every active Movement component in loaded scenes which can lead to unexpected behavior down the line. If its public, you can see it's state in Unity inspector by selecting the GameObject with Movement component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM