- Home /
Look at wont update?
so I got this script for my camera, it makes so that an object (target) rotates so that its looking to the same way i'm looking when I press one of the WASD keys, the problem I got is that it only does it once, help??
#pragma strict
var target : Transform;
var Distance = 10;
function Update(){
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
target.LookAt(Vector3(transform.position.x - 180,target.position.y,target.position.z));
}
transform.position = target.position - transform.forward * Distance;
}
Answer by Eno-Khaon · Apr 20, 2015 at 08:15 PM
Maybe I'm simply not understanding the intent, but why is the target looking 180 units away from the camera on the X axis, but towards itself on the Y and Z?
If the intent is to rotate the effective X axis by 180 degrees, you wouldn't want to do that with the target direction you're getting this object to face. If you want a simple "face the same way as the camera, but straight along the ground" sort of thing, try:
target.EulerAngles = Vector3(target.EulerAngles.x, transform.EulerAngles.y, target.EulerAngles.z);
With this, you could also change the Y axis to (transform.EulerAngles.y + 180) as desired.
Yeah I realized this my self later on, and your script line works like a charm!
what I had going on for me was
target.transform.rotation.y = transform.rotation.y;
but it sometimes bugged out when I did 180 turns, but yours doesn't, thanks!
Answer by Spinnernicholas · Apr 20, 2015 at 04:44 PM
You have to setup the "Vertical" and "Horizontal" axis in the Input Manager before you can use them.
Also, Input.GetAxis returns a float between -1 and 1, representing the axis position. It does not return a boolean for a logical condition statement. You should instead check if either of them is == to 0.
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){
//....
}
I have setup them, I tried the !=0 but it still only does it once and after that it wont do it..
If you want it to look at the point at "Distance" in front of this object, try this:
target.LookAt(transform.position + transform.forward * Distance);
You start with this object initial position, and then add it's forward vector times the distance.
I haven't done it myself, but it looks like using his Boolean logic would actually work, because if either of those values are non-zero, then it should cast to a bool and return true.
Yeah, it will work, but it is really bad code. It hinder's the "readability" of the code and hinders "reusability" because it is ambiguous. Stuff like that is a big reason coding is so difficult. Whoever wrote this probably did it to confuse beginning programmers. If you do want to use the shortcut, add a comment above that line explaining the purpose. eg:
//if joysticks are active
Your answer
Follow this Question
Related Questions
How Can I Stop Rotation From This Script? 0 Answers
Demons Souls Fall death Camera 1 Answer
I need help with camera movement and rotation. 1 Answer