im in the process of making a game that is a blend of RPG and RTS, so the player runs around, mouse clicking an enemy, and then rotate’s towards the enemy, so the line of gun-fire is clear, so i have a script on the camera, that selects the enemy, and set’s the variables, and then i use a modifyed version of Unity’s ThirdPersonController. neat little script ![]()
AttackMode1.js:
var SelectedEnemy: GameObject;
var SelectionMarker: Transform;
var ThePlayer: Transform;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100) Input.GetMouseButton(0)) {
SelectedEnemy = gameObject.Find(hit.transform.name);
if (SelectedEnemy.tag == "Enemy") {
// make it a child of the current object
SelectionMarker.parent = SelectedEnemy.transform;
SelectionMarker.transform.localPosition = Vector3(0, 0.001, 0);
}
}
}
And then we have the ThirdPersonController
Part of ThirdPersonController.js:
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
var grounded = IsGrounded();
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
// Are we moving backwards or looking backwards
if (v < -0.2)
movingBack = true;
else
movingBack = false;
var wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
// Target direction relative to the camera
var targetDirection = h * right + v * forward;
// Grounded controls
if (grounded)
{
if (Input.GetButton ("Fire2")) {
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast(ShootingPoint.position, fwd, hit, 1000)) {
if (hit.transform.tag == "Enemy") {
//animation.CrossFade("shoot");
if (Time.time > lastPunchTime + 0.3) {
// deal damage
var target = GameObject.Find(hit.transform.name);
target.SendMessage("ApplyDamage2", damage);
lastPunchTime = Time.time;
}
}
}
}
// Lock camera for short period when transitioning moving standing still
lockCameraTimer += Time.deltaTime;
if (isMoving != wasMoving)
lockCameraTimer = 0.0;
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero)
{
// If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9 grounded)
{
moveDirection = targetDirection.normalized;
}
// Otherwise smoothly turn towards it
else
{
var TheSelector : AttackMode1 = gameObject.Find("MainCamera").GetComponent(AttackMode1);
if (TheSelector.SelectedEnemy) {
moveDirection = Vector3.RotateTowards(moveDirection, TheSelector.SelectedEnemy.transform.position, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
} else {
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
}
// Smooth the speed based on the current target direction
var curSmooth = speedSmoothing * Time.deltaTime;
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
// Pick speed modifier
if (Input.GetButton ("Fire3"))
{
targetSpeed *= runSpeed;
}
else if (Time.time - trotAfterSeconds > walkTimeStart)
{
targetSpeed *= trotSpeed;
}
else
{
targetSpeed *= walkSpeed;
}
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Reset walk time start when we slow down
if (moveSpeed < walkSpeed * 0.3)
walkTimeStart = Time.time;
}
// In air controls
else
{
// Lock camera while in air
if (jumping)
lockCameraTimer = 0.0;
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
}
}
So what i am trying to do, is when i select a enemy, the player rotate towards the enemy that is selected, so his line of fire is clear, but the player will still move in an good order so to speak, so he can run around the enemy while shooting, so i guess at a later point, i will set it like the camera will get both player and enemy in the same view ( no matter what angle), but back the the idea, HOW DO I get the player to run around the selected enemy while hit focus or direction/rotation is on the enemy?