Okay, I need to set up a ray so that when it is cast from the camera it will hit/destroy what is currently in the center of the screen, and nothing else along the way. Currently, I can cast a ray from the camera, and it will destroy what it hits, but it doesn't hit what's at the center of the screen, and instead hits a bunch of other stuff. My code goes as follows:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotSpeed : float = 5.0;
var latTarget : Transform;
var latRotSpeed : float = 5.0;
public var reach : float = 10;
var currentTarget :Transform;
var cameraPos : Transform;
var target : Transform;
private var moveDirection : Vector3 = Vector3.zero;
function Start() {
}
function Update() {
var hit : RaycastHit;
var fwd : Vector3 = cameraPos.TransformDirection(Vector3.forward);
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var controller : CharacterController = GetComponent.();
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X")*rotSpeed);
transform.RotateAround(latTarget.position, Vector3.left, Input.GetAxis("Mouse Y")*(latRotSpeed));
//if (transform.eulerAngles.
transform.eulerAngles.z = 0;
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
if (Physics.Raycast(ray, hit, reach))
{
if(Input.GetMouseButton(1)){
Destroy(hit.transform.gameObject);
Debug.Log("I'm hitting something");
}
}
}
Thank you for your help, in advance.
↧