- Home /
need help fixing third person character controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class characterMovement : MonoBehaviour
{
//variables and functions
public float moveSpeed;
bool attackcooldown = true;
public float health = 100f;
float oldhealth = 100f;
public bool dead = false;
bool canheal = true;
Vector3 moveDirection;
void Start()
{
moveSpeed = 1f;
health = 100;
dead = false;
}
void FixedUpdate()
{
//movement
if (dead == false)
{
if (new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal")).magnitude != 0)
{
gameObject.GetComponent<Rigidbody>().AddForce(moveDirection * moveSpeed, ForceMode.Acceleration) ;
}
}
}
//update loop
void Update()
{
//moveDirection updating
moveDirection = (GameObject.Find("Camera Holder").transform.GetChild(0).transform.forward * Input.GetAxisRaw("Vertical") + GameObject.Find("Camera Holder").transform.GetChild(0).transform.right * Input.GetAxisRaw("Horizontal")).normalized;
moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
//death
if (health < 1)
{
if (dead == false)
{
dead = true;
}
}
//attacking
//if (Input.GetKeyDown(KeyCode.Mouse0) && attackcooldown && dead == false)
//{
//StartCoroutine(waiter());
//IEnumerator waiter()
//{
//GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).GetComponent<Animation>().Play();
//attackcooldown = false;
// yield return new WaitForSeconds(0.1f);
//GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<TrailRenderer>().emitting = true;
//yield return new WaitForSeconds(0.3f);
//RaycastHit hitinfo;
//if (Physics.Raycast(GameObject.Find("Main Camera").transform.position, GameObject.Find("Main Camera").transform.forward, out hitinfo, 2f))
//{
//GameObject clone = Instantiate(GameObject.Find("hiteffectsone"), hitinfo.point, Quaternion.Euler(0, 0, 0));
//GameObject clonetwo = Instantiate(GameObject.Find("hiteffectstwo"), hitinfo.point, Quaternion.Euler(0, 0, 0));
//clone.GetComponent<ParticleSystemRenderer>().material = hitinfo.transform.gameObject.GetComponent<Renderer>().material;
//clone.GetComponent<ParticleSystem>().Emit(15);
//clonetwo.GetComponent<ParticleSystem>().Emit(10);
//Destroy(clone, 10);
//Destroy(clonetwo, 10);
//}
//yield return new WaitForSeconds(0.1f);
//GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<TrailRenderer>().emitting = false;
//yield return new WaitForSeconds(GameObject.Find("Main Camera").transform.GetChild(0).transform.GetChild(0).GetComponent<Animation>().clip.length - 0.5f);
// attackcooldown = true;
// }
//}
if (Input.GetKeyDown(KeyCode.LeftShift) && dead == false)
{
moveSpeed = 45f;
}
if (Input.GetKeyUp(KeyCode.LeftShift) && dead == false)
{
moveSpeed = 30f;
}
if (new Vector3(Input.GetAxisRaw("Vertical"), 0, Input.GetAxisRaw("Horizontal")).magnitude != 0)
{
transform.GetChild(0).transform.LookAt(transform.GetChild(0).transform.position + moveDirection);
}
if (canheal == true && dead == false)
{
StartCoroutine(healing());
IEnumerator healing()
{
canheal = false;
if (health < 100)
{
health = health + 1;
}
yield return new WaitForSeconds(0.5f);
canheal = true;
}
}
}
}
i need help with the actual rigidbody movement of this script, note: PLEASE IGNORE ALL THE STUFF THAT IS NOT RELATED TO RIGIDBODY MOVEMENT, THAT IS ALL STUFF PLANNED FOR WHEN I GET THIS DONE
my issue here is that the rigidbody constantly moves left relative to the camera, various things ive tried are: -changing ways i calculate the movedirection variable -switching from velocity to addforce -messing around with physics material stats -changing my hierarchy from rotating the entire capsule to rotating just a mesh - and lastly, changing movespeed values and cutting out a camera bob system out of fear it would tamper with the movedirection variable (its all relative to the camera so if the camera is moving i figured it would get screwed up)
few things to note: the rotation of the mesh works fine, it turns left relative to the camera when i hit a, right when i hit d, forward when i hit w, and so on, so i believe its an issue with physics and not the movedirection variable
feel free to ask for any additional information, dont want to clutter my post too much though right off the bat
Answer by ChillCo · Jan 10 at 11:09 PM
I SOLVED IT, turns out it was a matter of rigidbody settings and using moveposition, so i was half right on that!
edit: nope, by setting rigidbody to be kinematic i could psuedo-solve the issue temporarily, but then i realized collision didnt work, when i turned it off it just crumbled again, please i am desperate to get this bare bones third person character controller working, please!
Your answer
Follow this Question
Related Questions
Non slippery movement 1 Answer
Camera smooth follow jitters 0 Answers
2D 360 degress platformer example needed 0 Answers
Manual Player Physics Help (C#) 0 Answers
Quake Movement in Unity 1 Answer