GoConqr has a new look! Let us know what you think.

Notes

Primeiros passos - Unity

Davi Danil Portuguese 2 pages

Content of the notes (2)

Page 1

• Em ASSETS, separar as sprites (Pulo, ataque, morte, correr, IDLE)

• Em ANIMATION, criar as animações por quadros (no nosso exemplo utilizamos 12 quadros, mas tem que ver qual fica melhor)

• Em ANIMATOR, criar cada função. (no nosso exemplo, a animação de correr era FLOAT, e o resto TRIGGER).

• Em PLAYER colocaremos Circle Collider (Na parte de baixo do personagem)

• Ainda em PLAYER colocaremos Box Collider (Colocando na parte de cima do personagem e também dentro do Circle Collider)

• Criaremos o chão e colocaremos o nome de GROUND.

• Em GROUND, adicionar Box Collider sem cobrir as BORDAS.

• Clicar com botão direito em GROUND e colocar CREATE EMPTY.

• Nomearemos de BORDA.

• A BORDA, como o proprio nome já diz, irá nas bordas. Assim que criar uma borda perfeitamente, copiar e botar do outro lado.

• OBS: BORDA deverá ter box collider.

• Agora em PLAYER, criar um novo component (ADD COMPONENT), PROCURAR por PlayerController. Não encontrará, então clique em NEW SCRIPT.

• Abriremos o Visual Studio.

• Utilizaremos o seguinte código:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour {

public float speed;

public float jumpForce;

private Rigidbody2D rb;

private bool facingRight = true;

private bool jump = false;

private Animator anim;

private bool noChao = false;

private Transform groundCheck;

// Use this for initialization

void Start () {

rb = gameObject.GetComponent();

anim = gameObject.GetComponent();

groundCheck = gameObject.transform.Find("GroundCheck");

}

// Update is called once per frame

void Update () {

noChao = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

if(Input.GetButtonDown("Jump") && noChao)

{

jump = true;

anim.SetTrigger("Pulou");

}

}

void FixedUpdate()

{

float h = Input.GetAxisRaw("Horizontal");

anim.SetFloat("Velocidade", Mathf.Abs(h));

rb.velocity = new Vector2(h * speed, rb.velocity.y);

if(h > 0 && !facingRight)

{

Flip();

}

else if(h < 0 && facingRight)

{

Flip();

}

if (jump)

{

rb.AddForce(new Vector2(0, jumpForce));

jump = false;

}

}

void Flip()

{

facingRight = !facingRight;

Vector3 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;

}

}

• Mudaremos em PLAYER, Player Controller, Speed = 5, Jump Force = 500

Page 2

Information

Description
Note on Primeiros passos - Unity, created by Davi Danil on 13/05/2018.
Created by
Davi Danil
Views
0
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?