Help with implementing animations to player, Camera snap to X axis.

Hello
Please treat me as ignorant.

I’ve started making a 2d side scrolling game and have run into a few problems.

The first problem: I would like to get my Main camera with it’s ‘camera 2d follow’ component to be snapped to the X axis so the camera can’t go up and down but can follow the character left and right (like old-school Mario style)

The second problem: I would love to know how I can take an animation I’ve made and then add it to the character or object. This one has cause me the most grief as I need to do it as soon as possible and I just can’t figure it out.

Things I could use some help with: adding things like a short dash, attacks, slower movement while going left on the x
axis.

I’ve looked on many youtube tutorials and they are either split up so I can’t find all the steps or it’s an out of date version of unity. It’s frustrating and wastes time.

I know I should team up with someone who knows how to do the technical stuff but it’s just not possible at this stage.

I would appreciate straightforward instructions from anyone and would be VERY appreciative of any kind of help to solve these problems.

for camera script:

public GameObject Player;
void update ()
{transform.position = new Vector3 (Player.transform.position.x, y, z);}

its not the full script, just lines for camera move. Y and Z are game depended (something like transform.position = new Vector3(Player.transform.position.x, 10, -10);.

what type of animation do you have ? Try to ask more exactly :slight_smile:

I have 2d sprites. They only move left and right.
Just figured the animation out myself. It was driving me crazy because I knew I had done it before but just couldn’t remember how to do it.

In case anyone else has this problem: Open the Animator tab. click on the state (i.e. idle, walk, jump etc) you want animated. Now drag the animation you’ve made from the assets window and drop it into the Motion input in whatever state you want.
As I type this I’m not sure about how to resize and reposition the animation on the character but I’ll post how to do it when I figure it out.

Thanks for your reply, Vakabaka!

Here is my camera script if it helps at all
It’s just the Camera2dFollow script from the free unity assets (I heard it’s decent)

using UnityEngine;
using System.Collections;

public class Camera2DFollow : MonoBehaviour {
   
    public Transform target;
    public float damping = 1;
    public float lookAheadFactor = 3;
    public float lookAheadReturnSpeed = 0.5f;
    public float lookAheadMoveThreshold = 0.1f;
   
    float offsetZ;
    Vector3 lastTargetPosition;
    Vector3 currentVelocity;
    Vector3 lookAheadPos;
   
    // Use this for initialization
    void Start () {
        lastTargetPosition = target.position;
        offsetZ = (transform.position - target.position).z;
        transform.parent = null;
    }
   
    // Update is called once per frame
    void Update () {
       
        // only update lookahead pos if accelerating or changed direction
        float xMoveDelta = (target.position - lastTargetPosition).x;

        bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;

        if (updateLookAheadTarget) {
            lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
        } else {
            lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);   
        }
       
        Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
        Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
       
        transform.position = newPos;
       
        lastTargetPosition = target.position;       
    }
}

my english is not good, but i will try to explain as i make it. I am new to unity to, and i dont like scripts from assets. They are little hard to understand. So i am trying to make realy easy script for me. If i need camera move only on x axe, so i would make that.

using UnityEngine;
using System.Collections;

public class Camera2DFollow : MonoBehaviour {

//this empty gameobject attached as child to player, its just for easy camera place.
//make empty gameobject put it little up from player, make it child of player, drag&drop in Hierarchy
//or you can just drag&drop your player in editor here
//may be not GameObject, but Transform should be used

public GameObject target;

void Start () {
//this just set camera direct on empty gameobject (as said easy camera place).
//you can move this gameobject in unity editor
transform.position = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
    }

    // Update is called once per frame
    void Update () {
// this move camera position to position of empty gameobject on x-axe every frame
//y&z will stay the same, as position from Start
transform.position = new Vector3(target.transform.position.x, transform.position.y, transform.position.z);
        }
}

i didnot checked it, may be something wrong, but it should work. And check z axe, or you can see nothing if it is wrong.

with animation it will be not so easy. Ok, you have 2d sprite. Do you have sprite sheet or you will just change sprite on animation ? Its late, i can try to explain it morning :slight_smile: