- Home /
WoW Movement and Freeze Cursor Location?
Hello, I need some help with this script. It's a wow-like movement script, and it's almost perfect! I can't seem to figure out how to make my character strafe when holding down the Right Mouse Button and Rotate keys (A and D... my strafe keys are Q and E). I want the rotate keys to act just the strafe keys when holding down the Right Mouse Button. Also, I don't know how to go about making my character move forward when both Left and Right Mouse Buttons are pressed. How would I go about doing these?
Also, at the bottom I added a FreezeCursor function. Basically what I want to do with that is to hide the cursor when pressing Right Mouse Button or Left Mouse Button or Both at the same time. But heres the catch - when I use Screen.showCursor and move the mouse, the cursor is in a different location. So I'm wondering if there is some way I can lock the cursor co-ordinates while Screen.showCursor = false; The reason I don't want to use Screen.lockCursor is because it goes back to the center of the screen every time I let go.
Here is the code:
@script RequireComponent(CharacterController)
private var moveDirection : Vector3 = Vector3.zero;
private var jumpTimer : int;
private var antiBunnyHopFactor = 1;
private var limitDiagonalSpeed = true;
var movementSpeed = 6.5f;
var jumpSpeed = 8.0f;
var rotateSpeed = 160.0f;
var gravity = 20.0f;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var rotate = Input.GetAxis("Rotate");
var rightMouse = Input.GetMouseButton(1);
var leftMouse = Input.GetMouseButton(0);
var jump = Input.GetButton("Jump");
var inputModifyFactor = (horizontal != 0.0 && vertical != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
if(controller.isGrounded)
{
if(rightMouse && rotate)
{
}
else if(rightMouse && leftMouse)
{
}
else
{
moveDirection = new Vector3(horizontal * inputModifyFactor, 0, vertical * inputModifyFactor);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= movementSpeed;
if (!jump)
jumpTimer++;
else if (jumpTimer >= antiBunnyHopFactor)
{
moveDirection.y = jumpSpeed;
jumpTimer = 0;
}
}
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(rightMouse)
{
Screen.showCursor = false;
transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
}
else
{
Screen.showCursor = true;
transform.Rotate(0, rotate * rotateSpeed * Time.deltaTime, 0);
}
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
controller.Move(moveDirection * Time.deltaTime);
}
function FreezeCursor ()
{
}
Answer by paulus51 · Jan 13, 2015 at 10:30 AM
i would love to know if you have succes with this movements script, i am searching the web for age to find a WOW like mouse / camera movement to ,
by combining som scripts together i manage the following :
right mouse click : rotate + up and down left mouse click : (same ) Rotate + up and down
moving forward just uses the w,a,s,d keys but wen i use the backwards key (s) the character turn arround facing the camera and walk backwards, but the camera and char moving forwards, verry strange ,
and wen i walk or run click spaca bar to jump, even wen i zoomed out the camera restarts at the head and then zoomed out if i use the code under this :
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
( if i let this option on the char shakes a bit wen i try to rotate thas the reason i comment this )
the character shakes wen i try to rotate so i have to put those // to comment this out of the code
for now i have these two codes :
movement ( add to charactercontroller :
private var jumpSpeed:float = 18.0;
private var gravity:float = 32.0;
private var runSpeed:float = 5.0;
private var walkSpeed:float = 0.9;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
static var dead : boolean = false;
function Update ()
{
if(dead == false) {
// Only allow movement and jumps while grounded
if(grounded)
{
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
//if(Input.GetButton("Jump"))
if (Input.GetKeyDown(KeyCode.Space))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
//if(Input.GetMouseButton(1)) {
// transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
//} else {
// transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
// Toggle walking/running with the numlock key
//if(Input.GetKeyDown(KeyCode.Numlock))
// isWalking = !Walking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
//Screen.lockCursor = true;
Screen.showCursor = false;
//var mouse1 = Input.mousePosition.y;
//var mouse2 = Input.mousePosition.x;
}
//Vector3 mousePos = Input.mousePosition;
else {
//Screen.lockCursor = false;
Screen.showCursor = false;
//Input.mousePosition.y = mouse1;
//Input.mousePosition.x = mouse2;
//Input.mousePosition = mousePos;
}
@script RequireComponent(CharacterController)
and camera : ( add to camera )
var target:Transform; // Target to follow
var targetHeight = 1.0; // Vertical offset adjustment
var distance = 8.0; // Default Distance
var offsetFromWall = 0.1; // Bring camera away from any colliding objects
var maxDistance = 20; // Maximum zoom Distance
var minDistance = 0.6; // Minimum zoom Distance
var xSpeed = 200.0; // Orbit speed (Left/Right)
var ySpeed = 200.0; // Orbit speed (Up/Down)
var yMinLimit = -80; // Looking up limit
var yMaxLimit = 80; // Looking down limit
var zoomRate = 40; // Zoom Speed
var rotationDampening = 3.0; // Auto Rotation speed (higher = faster)
var zoomDampening = 5.0; // Auto Zoom speed (Higher = faster)
var collisionLayers:LayerMask = -1; // What the camera will collide with
var lockToRearOfTarget = false; // Lock camera to rear of target
var allowMouseInputX = true; // Allow player to control camera angle on the X axis (Left/Right)
var allowMouseInputY = true; // Allow player to control camera angle on the Y axis (Up/Down)
private var xDeg = 0.0;
private var yDeg = 0.0;
private var currentDistance;
private var desiredDistance;
private var correctedDistance;
private var rotateBehind = false;
@script AddComponentMenu("Camera-Control/Third Person MMORPG Style")
function Start ()
{
var angles:Vector3 = transform.eulerAngles;
xDeg = angles.x;
yDeg = angles.y;
currentDistance = distance;
desiredDistance = distance;
correctedDistance = distance;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
if (lockToRearOfTarget)
rotateBehind = true;
}
//Only Move camera after everything else has been updated
function LateUpdate ()
{
// Don't do anything if target is not defined
if (!target)
return;
var vTargetOffset:Vector3;
// If either mouse buttons are down, let the mouse govern camera position
if (GUIUtility.hotControl == 0)
{
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
//Check to see if mouse input is allowed on the axis
if (allowMouseInputX)
xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02;
else
RotateBehindTarget();
if (allowMouseInputY)
yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02;
//Interrupt rotating behind if mouse wants to control rotation
if (!lockToRearOfTarget)
rotateBehind = false;
}
// otherwise, ease behind the target if any of the directional keys are pressed
else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || rotateBehind)
{
//RotateBehindTarget();
}
}
yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
// Set camera rotation
var rotation:Quaternion = Quaternion.Euler (yDeg, xDeg, 0);
// Calculate the desired distance
desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
correctedDistance = desiredDistance;
// Calculate desired camera position
vTargetOffset = Vector3 (0, -targetHeight, 0);
var position:Vector3 = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
// Check for collision using the true target's desired registration point as set by user using height
var collisionHit:RaycastHit;
var trueTargetPosition:Vector3 = Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
// If there was a collision, correct the camera position and calculate the corrected distance
var isCorrected = false;
if (Physics.Linecast (trueTargetPosition, position, collisionHit, collisionLayers))
{
// 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 limits
currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
// Recalculate position based on the new currentDistance
position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
//Finally Set rotation and position of camera
transform.rotation = rotation;
transform.position = position;
}
function RotateBehindTarget()
{
var targetRotationAngle:float = target.eulerAngles.y;
var currentRotationAngle:float = transform.eulerAngles.y;
xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
// Stop rotating behind if not completed
if (targetRotationAngle == currentRotationAngle)
{
if (!lockToRearOfTarget)
rotateBehind = false;
}
else
rotateBehind = true;
}
static function ClampAngle (angle : float, min : float, max : float)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
so i finely hope i l get my MMO like wow movement and camera scripts who can help me with this ?