Keys "A" and "D" to TURN and not TURN and RUN
I searched in google, but i cant found the aswer.
Let me explain
I need to move my char, like wow, move like keys "A" and "D" only TURN the char, TURN not turn and run. I only need to turn and run if, and only if, the "W" or "S" keys are pressed too.
I am using the thirdperson scripts and movement from standard unity, and the script below (from another person in this forun, i think), for the camera like wow too, but only the right button do what i need, the left buton not orbit the char like wow, i cant do this. But my urgent problem now is, keys "A" and "D" only turn the char, not turn and run.
PS: I am developer c#, then, i not have problem with scripts.
using UnityEngine;
using System.Collections;
public class WowCamera : MonoBehaviour
{
public Transform target;
public float targetHeight = 1.7f;
public float distance = 5.0f;
public float offsetFromWall = 0.1f;
public float maxDistance = 20;
public float minDistance = .6f;
public float speedDistance = 5;
public float xSpeed = 200.0f;
public float ySpeed = 200.0f;
public int yMinLimit = -40;
public int yMaxLimit = 80;
public int zoomRate = 40;
public float rotationDampening = 3.0f;
public float zoomDampening = 5.0f;
public LayerMask collisionLayers = -1;
private float xDeg = 0.0f;
private float yDeg = 0.0f;
private float currentDistance;
private float desiredDistance;
private float correctedDistance;
private float xDeg2 = 0.0f;
private float yDeg2 = 0.0f;
private float currentDistance2;
private float desiredDistance2;
private float correctedDistance2;
private bool leftButtonPressed;
void Start()
{
Vector3 angles = transform.eulerAngles;
xDeg = angles.x;
yDeg = angles.y;
xDeg2 = angles.x;
yDeg2 = angles.y;
currentDistance = distance;
desiredDistance = distance;
correctedDistance = distance;
// Make the rigid body not change rotation
if (this.gameObject.GetComponent<Rigidbody>())
this.gameObject.GetComponent<Rigidbody>().freezeRotation = true;
}
/**
* Camera logic on LateUpdate to only update after all character movement logic has been handled.
*/
void LateUpdate()
{
Vector3 vTargetOffset;
// Don't do anything if target is not defined
if (!target)
return;
// If either mouse buttons are down, let the mouse govern camera position
leftButtonPressed = false;
if (GUIUtility.hotControl == 0)
{
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
if (Cursor.visible)
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
// otherwise, ease behind the target if any of the directional keys are pressed
else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
float targetRotationAngle = target.eulerAngles.y;
float currentRotationAngle = transform.eulerAngles.y;
xDeg = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
}
if (!Input.GetMouseButton(0) && !Input.GetMouseButton(1) && !Cursor.visible)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
// calculate the desired distance
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance) * speedDistance;
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
// set camera rotation
Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);
correctedDistance = desiredDistance;
// calculate desired camera position
vTargetOffset = new Vector3(0, -targetHeight, 0);
Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
// check for collision using the true target's desired registration point as set by user using height
RaycastHit collisionHit;
Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y, target.position.z) - vTargetOffset;
// if there was a collision, correct the camera position and calculate the corrected distance
bool isCorrected = false;
if (Physics.Linecast(trueTargetPosition, position, out collisionHit, collisionLayers.value))
{
// calculate the distance from the original estimated position to the collision location,
// subtracting out a safety "offset" distance from the object we hit. The offset will help
// keep the camera from being right on top of the surface we hit, which usually shows up as
// the surface geometry getting partially clipped by the camera's front clipping plane.
correctedDistance = Vector3.Distance(trueTargetPosition, collisionHit.point) - offsetFromWall;
isCorrected = true;
}
// For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
// keep within legal limits
currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
// recalculate position based on the new currentDistance
position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
transform.position = position;
if (!Input.GetMouseButton(0))
transform.rotation = rotation;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
Thanks a lot Moacir Jr.
Comment