So, I'm fairly new to coding with unity and c#, and I want to change my animations based on the player's mouse position. This is my script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 2f;
private bool right = true;
private bool left = false;
private bool up = false;
private bool down = true;
public Animation rightUpAnim;
public Animation leftUpAnim;
public Animation rightDownAnim;
public Animation leftDownAnim;
public string horizontalAxis;
public string verticalAxis;
private Vector2 movement = Vector2.zero;
private Rigidbody2D rb;
public Transform tf;
public Animator an;
private void Start() {
rb = GetComponent<Rigidbody2D>();
}
void Update() {
movement.x = Input.GetAxisRaw(horizontalAxis);
movement.y = Input.GetAxisRaw(verticalAxis);
}
private void FixedUpdate() {
rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
var mousePos = Input.mousePosition;
mousePos.x -= Screen.width / 2;
mousePos.y -= Screen.height / 2;
if (mousePos.y > tf.position.y) {
up = true;
down = false;
}
else if (mousePos.y < tf.position.y) {
down = true;
up = false;
}
if (mousePos.x > tf.position.x) {
right = true;
left = false;
}
else if (mousePos.x < tf.position.x) {
left = true;
right = false;
}
if (left && up) {
an.controller = leftUpAnim;
}
else if (right && up) {
an.controller = rightUpAnim;
}
else if (left && down) {
an.controller = leftDownAnim;
}
else if (right && down) {
an.controller = rightDownAnim;
}
}
}
However, I get an error saying that 'Animator' does not contain a definition for 'controller'. What is the correct way to do this?