- Home /
Custom Cursor? How does it work?
I have used custom cursors in different games but they would always rotate the screen as i would turn. I want one that doesn't interfere with these two scripts.
moveCharacter.js
private var jumpSpeed:float = 8.0; private var gravity:float = 20.0; private var runSpeed:float = 5.0; private var walkSpeed:float = 1.0; 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";
function Update () { // 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"))
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);
}
if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
Screen.lockCursor = true;
else
Screen.lockCursor = false;
// Toggle walking/running with the T key
if(Input.GetKeyDown("t"))
isWalking = !isWalking;
//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;
}
@script RequireComponent(CharacterController)
and orbitCharacter.js
var target : Transform;
var targetHeight = 2.0; var distance = 5.0;
var maxDistance = 20; var minDistance = 2.5;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
var zoomRate = 20;
var rotationDampening = 3.0;
private var x = 0.0; private var y = 0.0;
@script AddComponentMenu("Camera-Control/WoW Camera")
function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }
function LateUpdate () { if(!target) return;
// If either mouse buttons are down, let them govern camera position
if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) { x += Input.GetAxis("Mouse X") xSpeed 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02;
// otherwise, ease behind the target if any of the directional keys are pressed
} else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) { var targetRotationAngle = target.eulerAngles.y; var currentRotationAngle = transform.eulerAngles.y; x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime); }
distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
distance = Mathf.Clamp(distance, minDistance, maxDistance);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation:Quaternion = Quaternion.Euler(y, x, 0);
var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
transform.rotation = rotation;
transform.position = position;
}
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); }
The main reason is because in these scripts it uses the mouse to change the angle you look at things... (kind of like World of Warcraft) If you need more detail on this just leave a comment and i will edit it.
Answer by The_r0nin · Dec 16, 2010 at 11:07 PM
var myCursor:Texture2D; var cursorSizeX: int = 32; // set to width of your cursor texture var cursorSizeY: int = 32; // set to height of your cursor texture
function Start(){ Screen.showCursor = false; }
function OnGUI(){ GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2, cursorSizeX, cursorSizeY),myCursor); }
That puts a custom cursor on the screen. Shouldn't affect any other script...
This makes the mouse move as if you are holding the mouse upside down
Oh, yeah, I forgot to add that. I'll edit the answer, but you simply subtract the y from Screen.height.
yeah, thanks it helped so much... i finally have a game with controls that don't get in my way. Thanks and merry christmas :D
Answer by oliver-jones · Dec 17, 2010 at 12:30 AM
To make the texture more alined to the mouse do this:
var myCursor:Texture2D; var cursorSizeX: int = 32; // set to width of your cursor texture var cursorSizeY: int = 32; // set to height of your cursor texture
function Start(){ Screen.showCursor = false; }
function OnGUI(){ GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2 + cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 + cursorSizeY/2, cursorSizeX, cursorSizeY),myCursor); }
This makes the top left of the texture alined to the top left of the mouse (where the click takes place)
Answer by uanmanarmy · Sep 15, 2014 at 08:53 PM
using UnityEngine;
using System.Collections;
public class CustomCursor : MonoBehaviour {
//The texture that's going to replace the current cursor
public Texture2D cursorTexture;
//This variable flags whether the custom cursor is active or not
public bool ccEnabled = false;
void Awake ()
{
//Call the 'SetCustomCursor' (see below) with a delay of 0 seconds (Start Imediatly as scene Starts).
Invoke("SetCustomCursor", 0.0f);
}
void OnDisable()
{
//Resets the cursor to the default
Cursor.SetCursor(null, Vector2.zero,CursorMode.Auto);
//Set the _ccEnabled variable to false
this.ccEnabled = false;
}
private void SetCustomCursor()
{
//Replace the 'cursorTexture' with the cursor
Cursor.SetCursor(this.cursorTexture, Vector2.zero, CursorMode.Auto);
//Set the ccEnabled variable to true
this.ccEnabled = true;
}
}