Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

script failing to move player character

$
0
0
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(); rb = GetComponent(); 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 } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>