- Home /
Question by
alexsteam4 · Sep 23, 2021 at 12:38 PM ·
animationmovement2d gameaccelerometeracceleration
Aceleration 2D
Hello, how can i add progressive acceleration for my player?
For the switch between the normal walk animation and run animation after i get pass a defined value it can be done from the animator tab or i need to also call it on the script?
This is my current script for movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement2d : MonoBehaviour
{
Rigidbody2D body;
float horizontal;
float vertical;
public float runSpeed = 10.0f;
public Animator animator;
// Start is called before the first frame update
void Start()
{
body = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
Vector3 CharacterScale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
CharacterScale.x = -5;
}
if (Input.GetAxis("Horizontal") > 0)
{
CharacterScale.x = 5;
}
transform.localScale = CharacterScale;
}
private void FixedUpdate()
{
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
Comment
Your answer
Follow this Question
Related Questions
Is it possible to get the forward and backward movement of android device 1 Answer
How to move player by accelerometer 0 Answers
Tilting ball with acceleration in 2D for android 0 Answers
Player not being rendered until input has been detected. 1 Answer
Why is root motion reduced once I apply the animation? 0 Answers