I have a basic script that allows me to move on the X and Y(jump). I want to run a check that the character is always on a certain Z axis. I have tried the transform.position.z = 20; but that gives me a “Cannot modify type value” error.
Here is the code in using (similar to CharacterController.Move sample code)
using UnityEngine;
using System.Collections;
public class characterController2D : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// If character is on the ground
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
//Jump from grounded position
if (Input.GetButtonDown("Fire1")){
moveDirection.y = jumpSpeed;
}
}
if(!controller.isGrounded){
moveDirection.x = Input.GetAxis ("Horizontal");
moveDirection.x *= speed;
}
moveDirection.y -= gravity * Time.deltaTime; //
print (moveDirection.z);
controller.Move(moveDirection * Time.deltaTime); //
}
}