- Home /
Question by
naser_1998 · May 08, 2016 at 06:36 AM ·
c#charactercharacter controllercharacter movement
Help please in regards to character controller
I am new to C# and I am having difficultly figuring my code out. I created this character controller and the character plays the animation when I press as well and his legs start to move. However, he doesn't physically move from his place. How do I fix this please... I've be on this for hours!
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public Rigidbody rbody;
private float inputH;
private float inputV;
void Start()
{
rbody = GetComponent<Rigidbody>();
}
void Update()
{
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
anime.SetFloat("inputH", inputH);
anime.SetFloat("inputV", inputV);
float moveX = inputH * 20f * Time.deltaTime;
float moveZ = inputV * 50f * Time.deltaTime;
rbody.velocity = new Vector3(moveX, moveZ);
}
}
Can anyone tell me what is wrong with my code please, I would be really grateful.
Comment
try using Debug.Log()
to display the values of moveX/Y
- they might be really small...
also, changing the velocity
isn't the best way to move rigidbodies... try applying a force ins$$anonymous$$d.
and include the 3rd parameter of the new Vector3()
Also you either use rigidbodys or the charactercontroller. Never ever mix them! If you want to use the charactercontroller, take a look at its move function.