Help with Unity Please. Hello. I am learning Unity and I am doing my first game,
ID: 3888694 • Letter: H
Question
Help with Unity Please. Hello. I am learning Unity and I am doing my first game, but I have a problem. I cannot make my player move. I can make it change state like idle to running or running to idle but it always stay at the same place.
Pictures of the config of the player:
This is the code for the Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour {
[SerializeField]
public float rigidSpeedX;
public Animator anim;
public Rigidbody2D rd2d;
public SpriteRenderer sr;
// Use this for initialization
void Start ()
{
sr = GetComponent();
anim = GetComponent();
rd2d = GetComponent();
}
// Update is called once per frame
void Update ()
{
float moveHorizontal = Input.GetAxisRaw("Horizontal");
//playerSpeed += speedBoost;
if (moveHorizontal != 0)
{
MoveHorizontal(moveHorizontal);
}
else
{
StopMoving();
}
}
void MoveHorizontal(float moveHorizontal)
{
if (moveHorizontal < 0)
{
sr.flipX = true;
}
else if (moveHorizontal > 0)
{
sr.flipX = false;
}
// this.transform.Translate(moveHorizontal * 10f, 0, 0);
anim.SetInteger("State", 1);
rigidSpeedX = rd2d.GetComponent().velocity.x;
rd2d.GetComponent().velocity = new Vector2(moveHorizontal, rd2d.GetComponent().velocity.y);
}
void StopMoving()
{
rd2d.velocity = new Vector2(0, rd2d.velocity.y);
anim.SetInteger("State", 0);
}
//void JumpSpeed()
//{
// rd2d.AddForce(new Vector2(0, speedBoost));
//}
}
Explanation / Answer
Answer:
The below code will allow you to move the player and just put the classes according to what you have done. If you need any help just comment down below:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.