Help with some C#


I found this piece of code recently that tracks the mouse. There are some piece on the code that I would I am not 100% on and I would appreciate it if someone could help me understand them.

  • Quanternion.LookRotation

  • Quanternion.Slerp

  • Plane

  • Ray/Raycast

I have looked at the Unity code database, but I am struggling to understand what they do. I know Raycast intersects a plane and a ray and a ray is an infinite line, but thats about it.


This function you supply a direction as a Vector3. This will rotate the object to “look” in the direction of the supplied vector.

This function you supply two rotations as Quaternions and a rate as a float. This will rotate the object from the first rotation towards the second rotation. The rate part is confusing to me the way it is written in the documentation. It’s easier if you think of it as a “Slider” that starts at zero and goes to one. as the number in the rate gets closer to one the rotation gets closer to the second rotation.
//The start Rotation
Quaternion from = transform.rotation;
//The end Rotation
Quaternion to = Quaternion.Euler(transform.forward);

		Quaternion.Slerp (from,to,0.0f);// 0.0% complete or Start rotation
		Quaternion.Slerp (from,to,0.1f);// 10.0% complete
		Quaternion.Slerp (from,to,0.9f);// 90.0% complete
		Quaternion.Slerp (from,to,1.0f);// 100.0% complete or End rotation
		//So in an update loop if you pass Time.time or some other counter
		Quaternion.Slerp (from,to,Time.time);//This will move from start to end as time passes.

A plane is a plane. Not sure if this is what you were talking about.

A ray is an infinite line from a point in a direction. As an example if you use as your origin and Vector3.forward as your direction you will get a ray from zero to forward infinity.

This is used to take the information from what a raycast collided with.

A raycast is the operation of actually performing a cast of a ray you made.

A layer mask is a nifty tool when used in combination with Raycasting. This allows you to set specific layers for your raycast to check against. For example if you want your ray to ignore things in the “Player” layer you would uncheck “Player” in your layermask variable.


		public LayerMask myLayerMask;
		void MyRayFunction(){
			Ray myRay = new Ray(,Vector3.forward);
			RaycastHit rayHit;
			if(Physics.Raycast(myRay,out rayHit,100.0f,myLayerMask)){
				//Do stuff if ray hit something