Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nynex71 · Dec 16, 2010 at 10:14 PM · cursorcursor-customization

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nynex71 · Dec 16, 2010 at 10:45 PM 0
Share

feel free to use these scripts in your games :D

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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...

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Dec 16, 2010 at 11:44 PM 0
Share

This makes the mouse move as if you are holding the mouse upside down

avatar image Nynex71 · Dec 16, 2010 at 11:57 PM 0
Share

yeah how do u fix that upside down problem?

avatar image The_r0nin · Dec 17, 2010 at 12:00 AM 0
Share

Oh, yeah, I forgot to add that. I'll edit the answer, but you simply subtract the y from Screen.height.

avatar image Nynex71 · Dec 17, 2010 at 12:05 AM 0
Share

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

avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Keep Cursor at center and project interactions to cursor position on mobile touch devices (IOS) 0 Answers

How do I change the cursor without a separate Texture2D? 0 Answers

Multiple Cursors - WebGL Unity 5.6 2 Answers

Cursor / MouseOver Glitch 0 Answers

Changing Color of Curser 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges