Hello everyone. I have been trying to emulate the movement that many action / rpg’s use, such as diablo 2 , TQ, etc. There is many free scripts around the internet that offer click and move ability , but they all seem to a similiar problem that i have noticed. Click and moving across a planar platform ( a plane) , but all these scripts i have seen seem to have one common problem. They dont support moving off the planar position that the object (player) is on. In other words, when moving across terrain with heights, the object just ignores the height completely.
I did manage to find this script (as posted below) and it does almost everything i want it to, except… it ignores (technicly, does not apply) gravity when using the Move() function. So what happens is. now i can move the object around and it will climb hills properly, but it will not fall back down to the ground ( or stay grounded) when descending the hill.
I have tried to mix and merge a few scripts, write my own additions to make this work, but so far i have had zero luck. I am fairly new to this whole thing, so just wondering if anyone out there can show me , or give me a tip, so i could get this all working correctly. As i said, the following script does everything i want it to now, exceppt… theres no gravity, so it will not stay grounded after ascending the terrain.
var dir : Vector3;
var hit : RaycastHit ;
var rot = 0;
var k : Vector3;
var speed : float = 1.0;
var gravity = 1.0;
private var yourCharacterController : CharacterController;
function Start() {
yourCharacterController = GetComponent(CharacterController);
yourCharacterController.isTrigger = true;
}
function FixedUpdate()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(Camera.main.transform.position,ray.direction,hit,1000);
dir = hit.point - transform.position;
dir[1] = 0;
var angle = Vector3.Angle(dir, transform.forward);
var k = Vector3.Cross(transform.forward, dir);
k.Normalize();
rot = k[1]*(angle*2.5);
if (Input.GetMouseButton(0))
{
transform.Rotate(Vector3.up*rot*Time.deltaTime);
yourCharacterController.Move(dir.normalized * Time.deltaTime * speed);
}
}
function Update()
{
/// here i have added a cheesy *wants to be gravity** but object just keeps falling through terrain and falling forever.
transform.position.y -= gravity * Time.deltaTime;
}
I am wondering if possibly i can use the gravity as i have it, and use a trigger of some sort to detect when the object contacts the terrain when it descends? But i have been awake now for about 22 hours and am beginning to become very unfocused, and over stuffed in the head, hehe.
Thanks for any help guy , greatly appreciate it.
EDIT I have tried to add this collision event so i can create a boolean case of grounded or not, but when testing this simple script, it seems to never trigger/ This is attached to the terain, and i than applied a rigid body to my gameObject, but nothing happens. It still falls through the terrain, and no msg sems to be sent.
function OnCollisionEnter(object : Collision)
{
print("Contact made");
}