using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public float speed;
public Transform cameraTransform;
public float rotationSpeed;
public bool isAtk;
public Animator selfAnim;
void Update()
{
Move();
Attack();
}
void Move()
{
// Get discrete input values
int horizontal = 0;
int vertical = 0;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
vertical = 1;
}
else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
vertical = -1;
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
horizontal = 1;
}
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
horizontal = -1;
}
// Get the forward and right direction relative to the camera
Vector3 forward = cameraTransform.forward;
Vector3 right = cameraTransform.right;
// Project forward and right vectors onto the horizontal plane (y = 0)
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
// Calculate the direction to move based on input and camera orientation
Vector3 moveDirection = forward * vertical + right * horizontal;
// Update the character's position
transform.position += moveDirection * speed * Time.deltaTime;
if (moveDirection != Vector3.zero)
{
if(!isAtk)
{
if(selfAnim.GetInteger("run") != 1)
{
ResetAnims();
selfAnim.SetInteger("run", 1);
}
}
transform.position += moveDirection * speed * Time.deltaTime;
// Rotate the character to face the move direction using Slerp
Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
else if(moveDirection == Vector3.zero) //i.e. no input
{
if (!isAtk)
{
if (selfAnim.GetInteger("idle") != 1)
{
ResetAnims();
selfAnim.SetInteger("idle", 1);
}
}
}
}
void Attack()
{
if (Input.GetMouseButtonDown(0))
{
if(!isAtk)
{
isAtk = true;
ResetAnims();
selfAnim.SetInteger("attack", 1); //will falsify isAtk via animation event
}
//do attack
}
}
void ResetAnims()
{
selfAnim.SetInteger("idle", 0);
selfAnim.SetInteger("run", 0);
selfAnim.SetInteger("attack", 0);
}
void FalsifyAtk() //called at end of attack animation to falsify isAtk
{
isAtk = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fatcat : MonoBehaviour
{
public Rigidbody selfRb;
public float upForce; //ref: 600
public float backForce; //ref: 500
private void OnTriggerEnter(Collider other)
{
if(other.tag == "atk")
{
Transform theParent = other.transform.parent;
Vector3 dir = transform.position - theParent.position;
dir.y = 0;
dir.Normalize();
Vector3 force = dir * backForce + Vector3.up * upForce;
selfRb.velocity = Vector3.zero;
selfRb.AddForce(force);
}
}
}