Reputation:
im trying to make a first person character controller for a game im making, it wont need jumping nor fast speeds or any additional moves, practically just walking and a weapon swinging ill add down the line, right now though first person is out of my comfort zone, i made a third person character controller and it wasnt THIS difficult, im basically trying to change the forward vector of the player capsule into something more compatable with input.getaxis, i currently have a frankenstien mess that hardly functions, but the camera movement functions just fine, with my current setup i am using a rigidbody to control the player, and i idealy want to use velocity and not addforce or moveposition, velocity has given me the best results so far, how can i fix my movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class characterController : MonoBehaviour {
//variables and functions
public float moveSpeed = 0.1f;
public bool tweening = false;
public bool jumping = false;
public bool reteleporing = false;
bool walking = false;
public bool ragdolled = false;
public Vector3 smoothedvel;
public Vector3 smoothedrotationalvector;
public Vector3 cameraoffset;
void Start () {
cameraoffset = new Vector3(0,0,0);
smoothedvel = new Vector3(0,0,0);
moveSpeed = 5;
}
void Update (){
//movement
smoothedrotationalvector = Vector3.Lerp(smoothedrotationalvector, new Vector3(-Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal")), 0.2f);
if (new Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal")).magnitude != 0)
{
transform.LookAt(GameObject.Find("Main Camera").GetComponent<Camera>().transform.position, -Vector3.up);
transform.localEulerAngles = new Vector3(0,transform.localEulerAngles.y,0);
}
if (transform.position.y < -100)
{
reteleporing = true;
}
if (reteleporing == true)
{
transform.position = GameObject.Find("spawnpos").transform.position;
}
if (transform.position.y > 2.4)
{
reteleporing = false;
}
if (reteleporing == false)
{
if (new Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal")).magnitude != 0)
{
float axisX = Input.GetAxis("Horizontal") * moveSpeed;
float axisZ = Input.GetAxis("Vertical") * moveSpeed;
Vector3 moveDirection = transform.right * axisX + transform.forward * axisZ;
gameObject.GetComponent<Rigidbody>().velocity = moveDirection;
}
}
if (new Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal")).magnitude != 0 & walking == false)
{
walking = true;
}
if (new Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal")).magnitude == 0 & walking == true)
{
walking = false;
}
GameObject.Find("Main Camera").GetComponent<Camera>().transform.position = transform.position;
}
}
Upvotes: 0
Views: 548
Reputation:
i solved it, i used addforce instead of velocity and messed around with it from there, turns out velocity is really bad for things like this, it just downright didnt work because of velocity alone from what i can tell, another somewhat major thing was the switch from getaxis to getaxisraw, made it alot more snappy.
Upvotes: 0