- Home /
IP Infringement
Object is the wrong way around
I have looked for ages to find a solution, but cant seem to figure out why the object that has the applied script is back to front. Here is the sample code :
#pragma strict
public var TargetLookAt : Transform;
public var Distance : float = 5.0;
public var DistanceMin : float = 3.0;
public var DistanceMax : float = 10.0;
private var mouseX : float = 0.0;
private var mouseY : float = 0.0;
private var startingDistance : float = 0.0;
private var desiredDistance : float = 0.0;
public var X_MouseSensitivity : float = 5.0;
public var Y_MouseSensitivity : float = 5.0;
public var MouseWheelSensitivity : float = 5.0;
public var Y_MinLimit : float = -40.0;
public var Y_MaxLimit : float = 80.0;
public var DistanceSmooth : float = 0.05;
private var velocityDistance : float = 0.0;
private var desiredPosition : Vector3 = Vector3.zero;
public var X_Smooth : float = 0.05;
public var Y_Smooth : float = 0.1;
private var velX : float = 0.0;
private var velY : float = 0.0;
private var velZ : float = 0.0;
private var position : Vector3 = Vector3.forward;
function Start()
{
Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
startingDistance = Distance;
Reset();
}
function LateUpdate()
{
if (TargetLookAt == null)
return;
HandlePlayerInput();
CalculateDesiredPosition();
UpdatePosition();
}
function HandlePlayerInput()
{
var deadZone = 0.01; // mousewheel deadZone
if (Input.GetMouseButton(0))
{
mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
}
// this is where the mouseY is limited - Helper script
mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit);
// get Mouse Wheel Input
if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
{
desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity),
DistanceMin, DistanceMax);
}
}
function CalculateDesiredPosition()
{
// Evaluate distance
Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth);
// Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
function CalculatePosition(rotationX : float, rotationY : float, distance : float)
{
var direction : Vector3 = Vector3(0, 0, -distance);
var rotation : Quaternion = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + (rotation * direction);
}
function UpdatePosition()
{
var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth);
var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth);
var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth);
position = Vector3(posX, posY, posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
function Reset()
{
mouseX = 0;
mouseY = 10;
Distance = startingDistance;
desiredDistance = Distance;
}
function ClampAngle(angle : float, min : float, max : float)
{
while (angle < -360 || angle > 360)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
If you are going to use one of my scripts, but not ask a question where you found it, you could at least give me credit or provide the link to where you found it for future readers :
robertblu is correct : check the orientation of your objects
Thanks Fattie, I appreciate the thought, but really my answer is an IP Infringement of 3D Buzz. This is more the point I was trying to make. From that answer :
EDIT 2 : I want to clarify this is from an excellent tutorial by 3D Buzz in C#, I have simply converted it to unityJavaScript. (and moved a function to make one script)
Am still a bit heated from this previous question (check the last comment where my temper is showing, but still not fully unleashed) : http://answers.unity3d.com/questions/422522/slender-type-game-need-help.html
I wish I had Doc's Delorean to go back 12 months and stop myself from writing that bloody guide !
@Fattie : I have noticed a problem since the start of the year (apart from all the scripts I have put out there! The one on this question was actually my own) : http://answers.unity3d.com/questions/422377/my-problem-is-this.html#comment-422412
I keep telling myself "I am going to stop, or only give API links", but then in the next spare moment I find myself co$$anonymous$$g back here ....
Answer by robertbu · Mar 23, 2013 at 06:31 AM
I tested the script above, and it worked just fine for me. The problem is most likely your model. In unity forward is the side of the model that is facing positive 'z' with (0,0,0) rotation. If the issue is your model, you might be able to fix it by using an empty game object as the parent (with this script attached), and then having the rotated model as a child. Or you can fix it in the modeling program.
Follow this Question
Related Questions
Slerp Problem?: Enemy in Constant Rotation! 1 Answer
using a custom rotation in "Instantiate" 4 Answers
Smooth rotation in 90° increments 0 Answers
How To Check If X Rotation Is Between Two Numbers?? 1 Answer
space flying system 3 Answers