Currently working on a small prototype RPG that has a Top-Down, Point-and-click character control setup (diablo-like). What I’m trying to do is have a hotkey (fireball) that when pressed changes the cursor to a wand, then upon the the players left-mouse click a ray is cast whose hit point determines where the fireball will land. I don’t need info about changing the mouse cursor but instead what I’m really trying to understand is if their is any method to have a rigidbody (fireball) be sent to a specific point in 3d space (the ray’s hit point). Currently I have a prefab that has the fireball model, collider, and upon collision it detonates. My current take on achieving the spellcast part is that I will instantiate the fireball and give it velocity and do a bunch of math that gets the distance from player and raycast and throws the fireball to the ray’s hitpoint. I already have a script that gets the hit point of a ray cast onto the screen and moves the player there (standard left-click), so this is where im at:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
function Update() {
var destpoint : Vector3;
var hit : RaycastHit;
var movePos : Vector3;
var startPos : Vector3;
var therotation : Vector3;
if (Input.GetMouseButtonDown(0))
{
var ray = camera.main.ScreenPointToRay (Input.mousePosition); // mouse pointer to game world
if (Physics.Raycast (ray, hit, 500)) // See if I actually clicked on something nearby
{
destpoint = hit.point;
movePos = Vector3(destpoint.x, transform.position.y, destpoint.z);
startPos = Vector3(transform.position.x, 0, transform.position.z);
therotation = Vector3.Slerp(startPos, movePos, 1);
transform.LookAt(therotation);
StopCoroutine("MovePlayer2");
StartCoroutine("MovePlayer2", movePos);
}
}
}
function MovePlayer2(movePos : Vector3) {
var controller : CharacterController = GetComponent(CharacterController);
var dist = Vector3.Distance (transform.position, movePos);
var direction = transform.TransformDirection(Vector3.forward);
for (i = 0.0; i < 1.0; i += (speed * Time.deltaTime) / dist)
{
controller.SimpleMove(direction * speed);
yield;
}
}
@script RequireComponent(CharacterController)
*edit: should these input controls be in Update or LateUpdate()? I just tested both and LateUpdate appears to move the character a bit smoother on screen.