- Home /
Constrain object rotation set by mouse?
i have a character, and attached to him is a little flamethrower particle effect. ive been using this script to point the particle effect in the direction the mouse is. the particle effect is childed to the character so it doesnt move unless he does. however it goes around 360 degrees around the player. i have played around with this script for quite a while now and i just cant figure it out. how can i constrain the rotation of the particle effect to say, 60 or 90 degrees infront of the player?
var speed = 4.0;
function Update () {
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
Answer by Tocaro · Feb 12, 2013 at 04:10 AM
I suppose the simplest way would be to just check if the targetRotation exceeds your max rotation before setting it.
so something like this would go right after you set your targetRotation value.
//I'm just assuming 2D so I'm checking the z value
if(targetRotation.transform.eulerAngles.z > 90)
targetRotation.transform.eulerAngles.z = 90;
//Then the same sort of thing if less then 0
if(targetRotation.transform.eulerAngles.z < 0)
targetRotation.transform.eulerAngles.z = 0;
//And if your character flips directions you'll have to handle that too. otherwise now you just set it.
this doesnt quite work as it is telling me transform is not a member of Quaternion.
Your answer
Follow this Question
Related Questions
Snap the rotation 0 Answers
How to Constrain Quaternion Rotations Over Time? 1 Answer
Problem with normal snapping and rotations. 1 Answer
Matching 2 axes rotation only of 2 gameobjects 1 Answer
Rotation - Simple Question 0 Answers