- Home /
Help getting the target and camera rotation to match
ok so i am like making a game camera like the one from wizard 101 and i got everything else done that i need but when i rotate the target(aka player) in place the camera does not rotate with it facing the same direction as the target(aka player) i have tried many tutorials but cant seem to get any to work with the way my setup is so if anyone can help that would be great below is my code for the camera follow(java script) and player movement(java script) please help me fix my problem been working on it for 3 days now and to no luck with it...and i have also tried making the target the cameras parent.
camera code:
 #pragma strict
 // setup variables
 var layermask: LayerMask;
 var target : Transform = null;;
 var height : float = 0.5;
 var distance : float = 4;
 
 var x:float = 0;
 var y:float = 4;
 var ymin:float = -50;
 var ymax:float = 60;
 var disMin:float = 1;
 var disMax:float = 8;
 var TurnSpeed : float = 4;
 
 // update the camera every screen refresh
 function Update () {
     // supose to lock on to target
     transform.LookAt(target.transform);
     // check if right mouse button is being pressed
     if(Input.GetMouseButton(1))
     {
         //updates camera's direction by mouse x and mouse y
         x += 10 * Input.GetAxis("Mouse X");
         y += 10 * Input.GetAxis("Mouse Y");
         // makes sure camera doesn't go pass its set y cords
         if(y > ymax)
         {
             y = ymax;
         }else if(y < ymin){
             y = ymin;
         }
     }
     // checks if mouse scroll is scrolling forward
     if(Input.GetAxis("Mouse ScrollWheel") > 0 && distance > disMin)
     {
         // zooms in foward and updates the height
         distance -= 0.2;
         if(height > 0.5)
         {
             height -= 0.05;
         }
         else if(height < 0.5)
         {
             height = 0.5;
         }
     }
     //checks if mouse scroll is scrolling backwards
     else if(Input.GetAxis("Mouse ScrollWheel") < 0 && distance < disMax)
     {
         // scrolls camera out and up at a slight tilt
         distance += 0.2;
         height += 0.05;
     }
     // check if scroll button is pressed
     if(Input.GetMouseButton(2))
     {
         // reesets camera to fixed point
         y = 4;
         height = 0.5;
         distance = 4;
     }
     
     // this block of code allows for the cam being pushed forward when againts terrain type stuff
     //{
     var rotation = Quaternion.Euler(y,x,0);
     var position = rotation * Vector3(0.0,height,-distance) + target.position;
     
     
     transform.rotation = rotation;
     transform.position = position;
     
     // check for collisions on layermask
     var hit : RaycastHit;
     if(Physics.Linecast(target.position,transform.position,hit,layermask))
     {
         var tempDistance = Vector3.Distance(target.position, hit.point);
         position = rotation * Vector3(0.0,height,-tempDistance) + target.position;
         transform.position = position;
     }
     //}
 }
 
target(aka player) movement:
 #pragma strict
 // setup variables
  var MoveSpeed : float = 0.2;
  var TurnSpeed : float = 4;
  
 function Update(){
     // moves this fowards
     if(Input.GetKey("w") || Input.GetKey(KeyCode.UpArrow))
     {
         this.transform.position += this.transform.forward * this.MoveSpeed;
     }
     // moves this backwards 
     if(Input.GetKey("s") || Input.GetKey(KeyCode.DownArrow))
     {
         this.transform.position -= this.transform.forward * this.MoveSpeed;
     }
     // rotates in place to the left
     if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
     {
         this.transform.Rotate(0,-this.TurnSpeed,0);
     }
     // rotates in place to the right
     if(Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
     {
         this.transform.Rotate(0,this.TurnSpeed,0);
     }
     // updates this to face camera's direction when right mouse button is down
     if(Input.GetMouseButton(1)) {
         this.transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
     }
 }
please help me and thanks in advanced for any and all help
So the camera doesn't move at all? Or does it respond to those controls in the camera follow script- mouse buttons/scroll and such?
the camera works it just when i rotate in place with my character the camera doesn't stay behind it and doesn't face it in the same direction
I think you need a better description of correct behavior. Right now you are using the mouse to rotate the camera, so adding an additional criteria of matching the rotation of the target will cause fighting. Currently your code looks at the character on line 19, but then you overwrite this rotation with one on line 66 that uses "$$anonymous$$ouse X" and "$$anonymous$$ouse Y". It also doesn't appear that you are causing the camera to follow the player, so matching the rotation doesn't make sense to me. $$anonymous$$aybe you need to start with a copy of the standard CameraFollow script and then put in your additions.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                