My animation is working but the character won't move.
Why won't my character move?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f )
{
transform.Translate (new Vector3 (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f )
{
transform.Translate (new Vector3 (0f, Input.GetAxisRaw ("Jump") * moveSpeed * Time.deltaTime, 0f));
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
}
}
Answer by VindicareTheKiller · Aug 11, 2020 at 10:17 AM
Try This Code but make shure you Character has a 2DRigidbody and a 2DBoxcollider and watch outthat the script has the name "Character2DController "
also check out my Youtube there i make Tutorials too name:"MasterGreenStudios"
i hope it will help you ;))
using UnityEngine;
public class Character2DController : MonoBehaviour { public float MovementSpeed = 1; public float JumpForce = 1; public Animator animator; private bool facingRight;
private Rigidbody2D _rigidbody;
void Start()
{
facingRight = true;
_rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetAxis("Horizontal") < 0)
{
transform.localScale = new Vector2 (-1.5f, transform.localScale.y);
}
if (Input.GetAxis("Horizontal") > 0)
{
transform.localScale = new Vector2(1.5f, transform.localScale.y);
}
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
animator.SetFloat("Movement Speed", Mathf.Abs(movement));
{
}
if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) || Input.GetButtonDown("Jump")) && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}
Answer by Yoshinator2 · Nov 27, 2016 at 07:21 PM
To me, your logic seems wrong. Instead of having the getAxisRaw inputs in the vector3, you would need to have a RigidBody(Or RigidBody2D if your game is 2D) and modify the velocity from there. To do this, define the RigidBody(Or RigidBody2D) at the top as a variable. This would look something like:
private RigidBody myRigidBody;
or
private RigidBody2D myRigidBody;
Then, you would need to get the component in the start function. This would look like:
myRigidBody = GetComponent<RigidBody>();
or
myRigidBody = GetComponent<RigidBody2D>();
From there, you would need to completely rephrase your code in your if statements, as there is a much simpler and cleaner way to move a player.
To me, your transform.Translate and all of the code after it make no sense. Instead, you can just change the velocity of the player or object through the rigid body. This is done by calling your rigid body variable's velocity, and creating a new vector 3, with only the X velocity being modified. As shown:
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f )
{
myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, myRigidBody.velocity.z);
}
Or, if you have a 2D game:
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f )
{
myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
}
And for the jump, you would do the same. This time, you should have a new variable defined for the jumpSpeed. Do this at the top by adding:
public float jumpSpeed;
Then define the speed in the inspector. Instead of modifying the x velocity, you would modify the Y velocity. As shown:
if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f )
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, myRigidBody.velocity.z);
}
Or if its 2D:
if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f )
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, 0f);
}
In case you're having trouble finding where any of this goes, here is how the end code should look:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private RigidBody myRigidBody;
public float moveSpeed;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidBody = GetComponent<RigidBody>();
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f )
{
myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, myRigidBody.velocity.z);
}
if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f )
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, myRigidBody.velocity.z);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
}
}
Or if its a 2D game:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private RigidBody2D myRigidBody;
public float moveSpeed;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidBody = GetComponent<RigidBody2D>();
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f )
{
myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
}
if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f )
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
}
}
Then, for the animator variables, just have them send the myRigidBody.velocity.x to the animator, and have it run the walking animation if the velocity is greater than zero. But because you can move to the left as well, which would be a negative velocity, you would need to use the Mathf.Abs function. As shown:
anim.SetFloat("MoveX", Mathf.Abs(myRigidBody.velocity.x));
anim.SetFloat("MoveUp", Mathf.Abs(myRigidBody.velocity.y));
This should work beautifully for you! Please accept this answer if it helped, I spent a good 25 minutes typing this for you. If you have any further questions just ask! Thanks for reading
Your answer
Follow this Question
Related Questions
Animation/Movement Error 1 Answer
Animations get screwed up when I add OnAnimatorMove function 0 Answers
My movement gets blocked, when I activate the animator 1 Answer
How to make orb walking? 0 Answers