Question by
flyinfishtacos · Jun 08, 2016 at 03:51 PM ·
movementgravity
Character being pulled back when moving
I relaize I asked this before but I messed up sending my code and it was unreadable. My player is being pulled back for some reason after moving forward in 3d game. The drag is 0 and the mass is 5 for him and he has a capsule collider. I intially thought it was the collider being pushed by now not sure. He is moving on a flat surface so I can think it is gravity.
Code is below
using UnityEngine;
using System.Collections;
public class CharacterControl : MonoBehaviour {
public float inputDelay = 0.1f;
public float forwardVel =12;
public float rotateVel = 100;
Animator anim;
Quaternion targetRotation;
Rigidbody rBody;
public float forwardInput, turnInput;
public Quaternion TargetRotation
{
get { return targetRotation;}
}
void Start()
{
anim = GetComponent<Animator>();
targetRotation = transform.rotation;
if (GetComponent<Rigidbody>()) {
rBody = GetComponent<Rigidbody>();
}
else{
Debug.LogError("The character needs a rigid body");
}
forwardInput = turnInput = 0;
}
void GetInput(){
forwardInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void Update(){
GetInput();
Turn();
if(Input.GetButtonDown("Jump"))
{
anim.SetTrigger("isJumping");
}
if(forwardInput != 0){
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
if (Input.GetMouseButtonDown(0)) {
anim.SetTrigger("Spell");
}
}
void FixedUpdate(){
Run();
}
void Run(){
if(Mathf.Abs(forwardInput)> inputDelay){
rBody.velocity = transform.forward * forwardVel;
}
else{
Vector3 v = rBody.velocity;
v.x = v.z = 0;
rBody.velocity = v;
}
}
void Turn(){
if (Mathf.Abs(turnInput) > inputDelay) {
targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
Comment