using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControll : MonoBehaviour
{
Animator anim;
private Rigidbody2D rBody2D;
public float moveForce = 1f;
public float moveSpeed = 1f;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animator>();
this.rBody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
if (h != 0)
{
this.transform.localScale = new Vector3(h, 1, 1);
Vector2 Force = new Vector2(h, 0) * moveForce;
Debug.Log(Force);
this.rBody2D.AddForce(Force);
this.anim.SetFloat("Speed", Mathf.Abs(Force.x));
}
else
{
this.rBody2D.velocity = Vector2.zero;
this.anim.SetFloat("Speed", 0);
}
}
}
RigidBody2D의 AddForce를 이용하여 구현한 영상
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControll : MonoBehaviour
{
Animator anim;
private Rigidbody2D rBody2D;
public float moveForce = 1f;
public float moveSpeed = 1f;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animator>();
this.rBody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
if (h != 0)
{
this.transform.localScale = new Vector3(h, 1, 1);
}
Vector2 dir = new Vector2(h, 0);
Vector2 moveMent = dir * moveSpeed * Time.deltaTime;
this.rBody2D.MovePosition(this.rBody2D.position + moveMent);
this.anim.SetFloat("Speed", Mathf.Abs(moveMent.x));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControll : MonoBehaviour
{
Animator anim;
private Rigidbody2D rBody2D;
public float moveForce = 1f;
public float moveSpeed = 1f;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animator>();
this.rBody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
if (h != 0)
{
this.transform.localScale = new Vector3(h, 1, 1);
}
this.rBody2D.velocity = new Vector2(moveSpeed*h, 0);
anim.SetFloat("Speed", Mathf.Abs(h));
}
}
'유니티 기초' 카테고리의 다른 글
유니티짱 마우스로 움직이기 (0) | 2023.08.04 |
---|---|
밤송이 던지기 (0) | 2023.08.04 |
유니티 애니메이션(점프) (0) | 2023.08.03 |
구름타는 고양이 (0) | 2023.08.02 |
수리검 던지기 (0) | 2023.08.01 |