so, i’ve been reworking the character movement script, adapted from the standard assets 3rd person controller… and i’m trying to get it so that when the mouse is held down, the turning towards what you’re clicking on takes precidence over the previous orientation… but, i’m lost as to where it is? i’m trying to figure it out but i’m having a sort of blonde moment here.
here’s what i’ve got so far for scripting.
var speed = 3.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var smoothSpeed = 10.0;
var smoothDirection = 10.0;
var canJump = true;
var target : Transform;
var reticle1 : Transform;
var reticle2 : Transform;
var turnSpeedClick = 15.0;
var playerInitialOrientation;
private var moveDirection = Vector3.zero;
private var verticalSpeed = 0.0;
private var moveSpeed = 0.0;
private var grounded : boolean = false;
private var jumping : boolean = false;
private var targetAngle = 0.0;
// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)
function Awake ()
{
moveDirection = transform.TransformDirection(Vector3.forward);
}
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
// 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);
// Target direction relative to the camera
var targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;
// 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)
{
moveDirection = Vector3.Lerp(moveDirection, targetDirection, smoothDirection * Time.deltaTime);
moveDirection = moveDirection.normalized;
}
// Smooth the speed based on the current target direction
var curSmooth = smoothSpeed * Time.deltaTime;
// When in air we accelerate slower
if (!grounded)
{
curSmooth *= 0.5;
}
moveSpeed = Mathf.Lerp(moveSpeed, targetDirection.magnitude * speed, curSmooth);
}
function Update() {
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
UpdateSmoothedMovementDirection();
if (grounded) {
verticalSpeed = 0.0;
// Jump
if (canJump Input.GetButton ("Jump")) {
verticalSpeed = jumpSpeed;
jumping = true;
SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
}
}
// Apply gravity
verticalSpeed -= gravity * Time.deltaTime;
var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0);
movement *= Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(movement);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
// Set rotation to the move direction
transform.rotation = Quaternion.LookRotation(moveDirection);
// We are in jump mode but just became grounded
if (grounded jumping)
{
jumping = false;
SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
}
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, hitdist))
{
// Get the point along the ray that hits the calculated distance.
var targetPoint = ray.GetPoint(hitdist);
//reticleCache = targetPoint;
targetCache = GameObject.FindWithTag("Player").transform;
//print(reticleCache);
//var isClickingLeft = false;
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// if Mouse click is held down, Smoothly rotate towards the target point.
if (Input.GetMouseButton(0))
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeedClick * Time.deltaTime);
reticle1.transform.position = targetPoint;
print(targetPoint);
}
if (Input.GetMouseButton(0)==false)
{
reticle1.transform.position = target.transform.position;
}
if (Input.GetMouseButton(1))
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeedClick * Time.deltaTime);
reticle2.transform.position = targetPoint;
print(targetPoint);
}
if (Input.GetMouseButton(1)==false)
{
reticle2.transform.position = target.transform.position;
}
}
}
function GetSpeed () {
return moveSpeed;
}
function IsJumping () {
return jumping;
}
function GetDirection () {
return moveDirection;
}
i’m not a good coder, my forte is in the graphics department… so please understand that i’m not all that great with coding.