Hey Guys,
I’m currently developing a game where you click and the player moves to that location. I’ve previously used Rigidbody but wasn’t pleased with the outcome, so after researching I found out Character Controller is better for Third Person Games. Is there any other ways of moving the character with mouse click, but collision has to be detected as it’ll have a major role in the game.
Note: I’ve played around a lot with the script but the results weren’t great, I’ve also researched the forums, answers section for a solution.
This is the script I have so far, any help would be appreciated.
var MovementSpeed : float = 6.0;
var JumpSpeed : float = 8.0;
var Gravity : float = 20.0;
var RaycastDistance : float = 100;
@HideInInspector
var MovementEnabled : boolean;
var MoveDirection : Vector3;
function Update()
{
//Create Character Controller
var PlayerController : CharacterController = GetComponent(CharacterController);
//Create Raycast
var NewRay = Camera.main.ScreenPointToRay(Input.mousePosition);
//Store Location of RaycastHit Position
var RayHit : RaycastHit;
//Check if Character is Grounded
if (PlayerController.isGrounded)
{
if(Input.GetButtonDown("Fire1"))
{
//Detect if any collider is hit within the RaycastDistance
if (Physics.Raycast(NewRay, RayHit, RaycastDistance))
{
//Get RayHit Point
var MovePoint = RayHit.point;
Debug.Log(MovePoint);
}
}
if (Input.GetButton ("Jump"))
{
MoveDirection.y = JumpSpeed;
}
}
//Apply gravity
MoveDirection.y -= Gravity * Time.deltaTime;
//Move the controller
PlayerController.Move(MoveDirection * Time.deltaTime);
}