0
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ I recommend spending a bit more time reading the documentation for AnimationClip (a piece of animation data), Animation (a legacy component that plays an AnimationClip, or can blend/cross fade animations manually), Animator (a component that automatically blends animation clips according to state machine transitions, layers, and blend trees), and how to go about animation in Unity in general. You're trying to assign an Animation component where it looks like you want to play an AnimationClip or set a control parameter. \$\endgroup\$
    – DMGregory
    Commented Apr 30, 2020 at 0:45

1 Answer 1

1
\$\begingroup\$

The error is saying, the Animator component has no .controller member, which is correct when you visit https://docs.unity3d.com/ScriptReference/Animator.html.

It looks to me as though you are trying to play some animations directly rather than through the Animator which is a state machine.

To play back clips that you have referenced in the script you would need to use the Animation (not Animator) component

Add a Animation component to the Gameobject on which your script is already attached.

A very stripped down example for playing an animation clip like you have currently.

public class AnimationClipExample : MonoBehaviour
{
    private Animation an;
    //be sure to assign your clip in the inspector
    public AnimationClip myAnimationClip;

    // Start is called before the first frame update
    void Start()
    {
        //You will either need to get a reference to this objects Animation Component via code or assigning it in the inspector, here I as you may guess I'm getting the reference through code.
        an = GetComponent<Animation>();
    }

    // Update is called once per frame
    void Update()
    {
        //if A is pressed down
        if (Input.GetKeyDown(KeyCode.A))
        {
            //set the animation clip
            an.clip = myAnimationClip;
            //play the clip
            an.Play();
        }
    }
}

I would hugely recommend looking over the Animator component and the Unity tutorials, as it allows you to free up the mess of If-else conditionals and being visual its much easier to understand whats going on.

https://www.youtube.com/watch?v=JeZkctmoBPw - Unity Animator Controller tutorial.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .