Updating Rotations

Hi; I’m using the MouseLook script provided with the FPS tutorial, and I have this piece of code embedded to serve as a ‘lock-on’ function:

if (Input.GetButton("Lockon")  target != null) {						
			Quaternion newRotation = Quaternion.LookRotation(target.position - transform.position);
			transform.rotation = originalRotation * Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 5);
		}

My issue is that the RotationX and RotationY variables are not updated with it, and I do not know how to extrapolate it from a Quaternion. As a result, when I let go of the Lock-On button, the camera snaps back to the ‘last’ position, which is not the behavior I desire.

Does anyone know how to do this?

Do you get the information you want from the quaternion’s Euler angles?

I was able to acquire the coordinates by acquiring the Euler Angles, yes, although for some reason x had become y, and y had become z.

Thank you for the reply, regardless.

I wish to utilize a function called ‘FindClosestEnemy’, but my MouseLook script is a .cs file and I get the following error when I attempt to declare:

FindClosestEnemy fce = GetComponent();
target = fce.FindClosestEnemy();

It says the type or namespace could not be found. How would I go about exposing this script to be used with MouseLook in C#?

Can you post the exact error? (The problem may be that you have a function other than a constructor with the same name as the class to which it belongs.)

Hi there, sorry for the late reply.

The exact error is:

Assets/Standard Assets/Camera Scripts/MouseLook.cs(46,33): error CS0246: The type or namespace name `FindClosestEnemy’ could not be found. Are you missing a using directive or an assembly reference?

On my First Person Controller GameObject, I have two scripts: MouseLook.cs, and FindClosestEnemy.js. I wish to use FindClosestEnemy in MouseLook, hence the line:

FindClosestEnemy fce = GetComponent();
target = fce.FindClosestEnemy();

Sorry, nevermind.

I decided to just re-write the script in .cs and add it as a method to the MouseLook script.

I need to be able to ‘draw’ lines across the screen and translate it to the world space. Currently I have such code:

var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}

Which was taken from the FPS tutorial’s Machine Gun example. What I would like to be able to do is to make the raycast point at wherever the mouse is pointing rather than Vector3.forward. I’ve taken a look at ScreenPointToRay but I cannot seem to get it to work, because I do not have a camera object…[/code]

There’s an example of how to use a raycast from the camera here. If you can see anything in the scene, then you must have a camera somewhere. It is probably the main camera that is in the scene by default. You can access it from a script using Camera.main.

Thank you very much for that, that worked wonderfully.

The next step I am uncertain about is how to create a ‘beam’; I wish to create a ‘melee’-effect with the Particle Emitter, similar to how a laser sword or some other type of drawn line would behave.

Does anyone have any suggestions?

Using the particle system is fairly straightforward, but you will probably have to experiment a bit to get the exact effect you want. Try using the Particles/Additive shader for the particle material. You can make them emit from a single line using an ellipsoid emitter, but with the ellipsoid set so that two of the axes are set to zero. (For example, set the ellipsoid’s X and Z components to zero to emit along the Y axis.) You will probably want to use a high emission rate and low energy (probably less than 0.5) to leave a slight trail without having a cloud of particles hanging around.

I am having trouble getting a grasp of when to enable my LineRenderer and when to turn it off.

I would like to have the effect appear when the user presses the Fire1 button, and make it go away when he releases it. Where would I put the linerender.enabled = true and false?

Alright; so I have a script that essentially is a melee weapon – it uses a LineRenderer object that is set to enabled = true when the player presses the Fire1 button.

I’ve tried several things, but I am not able to consistently turn it off when it’s done – I’ve tried putting it in LateUpdate and Update, but it isn’t guaranteed that the renderer will be disabled every time I release the button.

How may I fix this?