- Home /
script failing to move player character
I'm trying to create a character controller that allows me to move a character in first person, with the right/left keys rotating 90 degrees, the back key rotating 180, and the forward key moving the character a set distance. Most of this is working, but when I try to actually move the character forward, I run into some issues. If I use the player.Move method, the character moves forward some variable amount between 0.5 and 0.9, with none of the involved variables seeming to affect the distance. I can't use this because the number is inconsistent - if it were the same each time, I could work with it. If I use AddForce, the character doesn't move at all. I would like to have either the AddForce version moving correctly, or the player.Move version moving consistently. Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerRedo : MonoBehaviour
{
public float moveDistance=50;
public float playerX;
public float playerZ;
public float turn;
public bool isMoving = false;
public Vector3 movement;
private CharacterController player;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
player = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
Debug.Log(transform.position);
}
public void Move()
{
if (Input.GetButtonDown("Horizontal"))
{
if (Input.GetKey(KeyCode.LeftArrow))
{
turn = -90;
}
if (Input.GetKey(KeyCode.RightArrow))
{
turn = 90;
}
}
if (Input.GetButtonDown("Vertical"))
{
if (Input.GetKey(KeyCode.DownArrow))
{
turn = 180;
}
if (Input.GetKey(KeyCode.UpArrow))
{
turn = 0;
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 5) && hit.collider.tag != "Enemy")
{
return;
}
else
{
float zAxis = Input.GetAxis("Vertical") * moveDistance;
Vector3 movement = new Vector3(0, 0, zAxis);
movement = transform.rotation * movement;
//just comment out whichever of the two aren't needed to test
//rb.AddForce(transform.forward * moveDistance); //doesn't move at all
player.Move(movement); //moves inconsistently
Debug.Log(transform.position);
}
}
}
transform.Rotate(0, turn, 0);
turn = 0;
}
// Update is called once per frame
void Update()
{
Move();
//check collision for enemies
}
}
Horizontal and Vertical are axis, not buttons. You're requesting if a single input is down, when an axis consists of multiple inputs. What you need is something like...
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h != 0)
{
//rotate object using h as the modifier, if h == 1, it's right, if h == -1 its left
transform.Rotate(0, h * rotationSpeed * Time.deltaTime, 0);
}
if (v != 0)
{
//move object using v as the modifier, if v == 1 its forward if v == -1 its backward
player.$$anonymous$$ove(transform.forward * moveDistance * v * Time.deltaTime);
}
Yeah sorry, plus I assumed he's on a PC since he's calling out to $$anonymous$$eys and Axis' only result in being -1, 0, or 1, on a PC or whatever the $$anonymous$$/max values are set to in inspector.
Your answer
Follow this Question
Related Questions
Character Controller "walking on air" off cliffs/slopes 1 Answer
Start Button 2 Answers
Move player via button press with including acceleration / decelertion? 0 Answers
Sliding after moving 0 Answers
Player keeps flying up 0 Answers