Question by
jbMcFly · May 02, 2017 at 12:51 AM ·
charactercontrollermovement script
[solved] Playermovement script
Hello i wrote this script to have my character walking around and allow it to use my animator for that character. It works great except the character moves quite slow anyone get any tips or pointers for me thanks for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
void Start ()
{
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
void Update ()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
anim.SetBool("iswalking", true);
anim.SetFloat("inputx", movement_vector.x);
anim.SetFloat("inputy", movement_vector.y);
}
else
{
anim.SetBool("iswalking", false);
}
rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);
}
}
Comment
Best Answer
Answer by jbMcFly · May 02, 2017 at 01:33 AM
Hello thanks if anyone took a look but I found a fix.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
public float speed = 5;
void Start ()
{
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
void Update ()
{
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
anim.SetBool("iswalking", true);
anim.SetFloat("inputx", movement_vector.x);
anim.SetFloat("inputy", movement_vector.y);
}
else
{
anim.SetBool("iswalking", false);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Player still move left&right in MenuUI 0 Answers
Most suitable way to make player move for an open world environment? 0 Answers
Movement When Character is in air. 0 Answers
,Please help me with CharacterController.isGrounded 0 Answers
How do I transfer the momentum from my speed to the player? 0 Answers