Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

(Solved) Rotating Camera

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • (Solved) Rotating Camera

    Thought this would be handy for everyone and Ive finally finished it at 2.10am and am immensly chuffed with it so here you are Rotating camera when right mouse button is pushed. Basically holding right mouse button and spinning with the mouse spins the camera around the character rather than spinning the player itself. Im sure you get the point. Any questions feel free to ask ..

    CalcCamera Function in My Custom Pawn ...

    Code:
    simulated function bool CalcCamera(float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV)
    {
    	local vector start, end, hl, hn, hl2;
    	local actor a;
    	local Rotator rot2, SavedRot;
    
    	start = Location;
    	
    	if (Controller != none)
    	{
    		end = Location - (Vector(Controller.Rotation) * 100.f);
    	}
    	else
    	{
    		end = Location - (Vector(Rotation) * 100.f);
    	}
    	
    	a = Trace(hl, hn, end, start, false);
    	
    	if (a != none)
    	{
    		out_CamLoc = hl + vect(0,0,50);
    	}
    	else
    	{
    		if (bUsingFreeCam)
    		{
    			out_CamLoc.X = Location.X + Sin(Controller.Rotation.Yaw*0.0001) * 100.f;
    			out_CamLoc.Y = Location.Y + Cos(Controller.Rotation.Yaw*0.0001) * 100.f;
    		}
    		else
    		{
    			out_CamLoc = end + vect(0,0,50);
    		}
    	}
    	
    	GetActorEyesViewPoint(hl2, rot2);
    
    	if (bUsingFreeCam)
    	{
    		out_CamRot = Rotator(Location - out_CamLoc);
    	}
    	else
    	{
    		out_CamRot = rot2;
    	}
    
    	return true;
    }
    and then to make sure the pawn still doesnt rotate with the mouse like he normally would I just over rid the face rotation function in my custom pawn file too and changed the "IF" statement since I will never be using "FreeCam"

    Code:
    simulated function FaceRotation(rotator NewRotation, float DeltaTime)
    {
    	// Do not update Pawn's rotation depending on controller's ViewRotation if in FreeCam.
    	if (!bUsingFreeCam)
    	{
    		if ( Physics == PHYS_Ladder )
    		{
    			NewRotation = OnLadder.Walldir;
    		}
    		else if ( (Physics == PHYS_Walking) || (Physics == PHYS_Falling) )
    		{
    			NewRotation.Pitch = 0;
    		}
    
    		SetRotation(NewRotation);
    	}
    }
    but you could always just have ...

    if (!InFreeCam() || !bUsingFreeCam)

    Anyway I hope this helps someone

    Thanks to Solid_Snake for origional work on Third Person Code

  • #2
    nice work! thanks for the code =D

    Comment


    • #3
      Glad to finally put something back into the community

      Comment


      • #4
        Ok this code does work and it works perfectly BUT I was having trouble because if you pressed forward or back on the mouse it was moving closer to the character or further away which is something I didnt want happening. I would rather add this in later maybe using the scroll wheel. Anyway I looked over this many times and have eventually scrapped using the Cartesian way and went for something a lot simpler that has a very good effect. So heres how Ive done it ...

        The files being used are :

        CustomController.uc
        CustomPawn.uc

        In Custom Controller I have ...

        Code:
        // Function for free cam mode
        
        exec function FreeCamMode()
        {
        	GGPawn(Pawn).BeginFreeLook();
        }
        
        exec function NonFreeCamMode()
        {
        	GGPawn(Pawn).StopFreeLook();
        }
        This is a simple executed function which I have made on right click.

        in CustomPawn I have these code snippets ... (I wil ltry explain as best as possible)

        First declare a new boolean variable ...
        Code:
        var bool bFreeLookMode;
        Then over ride the CalcCamera Function from Actor...

        Code:
        simulated function bool CalcCamera( float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV )
        {
        	local Vector    HitLocation, HitNormal, TraceStart, TraceEnd, Height, ActorViewPointHL;
        	local Actor     HitActor;
        	local Rotator   ActorViewPointRot;
        	local float     CameraDistance;
        
        	Height = vect(0,0,80);
        	CameraDistance = 200.f;
        
        	TraceStart = Location;
        	TraceEnd = Location - Vector(Controller.Rotation) * CameraDistance;
        
        	HitActor = Trace(HitLocation, HitNormal, TraceEnd, TraceStart, false);
        
        	if (HitActor != none)
        	{
        		out_CamLoc = HitLocation + Height;
        	}
        	else
        	{
        		out_CamLoc = (Location - Vector(Controller.Rotation) * CameraDistance) + Height;
        	}
        
        	GetActorEyesViewPoint(ActorViewPointHL, ActorViewPointRot);
        
        	out_CamRot = ActorViewPointRot;
        
        	return true;
        }
        This is my over ridden Calc Camera Function. First of all ofcourse we have the height and cameradistance being set. These speak for themselves really.

        Secondly is this part ...

        TraceStart = Location;
        TraceEnd = Location - Vector(Controller.Rotation) * CameraDistance;

        HitActor = Trace(HitLocation, HitNormal, TraceEnd, TraceStart, false);

        This is taken from Solid_Snakes code from the hoursences tutorial, all it does is trace from the Location of the player(tracestart) to the location of the player in the angle of the "Controller.Rotation" (the mouse rotation in this case) as far as the camera distance(traceend). This is to see if the camera is blocked. So ...


        if (HitActor != none)
        {
        out_CamLoc = HitLocation + Height;
        }
        else
        {
        out_CamLoc = (Location - Vector(Controller.Rotation) * CameraDistance) + Height;
        }

        if HitActor comes back positive it means were blocked and so we place the camera at the HitActor + the height of the Camera added on. If Not then we place the camera with the same code used for the Trace + adding on the height.

        The next part is just because i wanted the camera to be up and over the shoulder of the Pawn but instead of Facing into the pawns back I want it looking forward to where the pawn is looking for aiming purposes. So to do this I have declared 2 new Variables ... vector ActorViewPointHL (hit location) and rotator ActorViewPointRot (rotation of pawns eyes)

        then to set these used the function ...

        GetActorEyesViewPoint(ActorViewPointHL, ActorViewPointRot);

        This returns both the hitLocation of the eyes and the Rotartion. Then use the Rotation for the cameras own rotation in the line ...

        out_CamRot = ActorViewPointRot;

        Easy Enough. Now for the Right Click function. I have these functions in my Pawn as I may want to do more with them later rather than just setting the booleans from my Controller ...

        Code:
        // Free Look Mode
        
        function BeginFreeLook()
        {
        	bFreeLookMode = true;
        }
        
        function StopFreeLook()
        {
        	Controller.SetRotation(Rotation);
        	bFreeLookMode = false;
        }
        The Begin FreeLook sets the boolean to true which is then used in this function ...

        Code:
        simulated function FaceRotation(rotator NewRotation, float DeltaTime)
        {
        	// Do not update Pawn's rotation depending on controller's ViewRotation if in FreeCam.
        	if (!bFreeLookMode)
        	{
        		if ( Physics == PHYS_Ladder )
        		{
        			NewRotation = OnLadder.Walldir;
        		}
        
        		//Update aimnode
        		if (NewRotation.Pitch>16384)
        			NewRotation.Pitch-=65536;
        		AimNode.Aim=vect2d(0.0,NewRotation.Pitch*0.00006103515625);
        
        		NewRotation.Pitch=0;
        		SetRotation(NewRotation);
        	}
        }
        The bit I have put in bold is something you should not worry about. The only reason I include it is because this is what brought me across this function. This is where I am setting my aiming node on my Pawn so that it pitches with the Mouse. If this gives you problems then leave it out.

        Anyway basically all this function does by default is to set the players rotation to your input but by default it has ... if (!CameraStyle='FreeCam') or something like that, (cant know for sure as not at a UDK computer) so I have changed this to ... if (!bFreeLookMode) which means if our boolean we made earlier isnt set to true then it updates the rotation, if it is set to true (on holding down right click) then it doesnt update the rotation.

        This all worked fine up until here but the problem I had was that when I let go off right click my Pawn would turn around and start running in the direction the camera was now in. if you want this then so be it but I wanted the camera to jump back to behind the pawn (just incase say your running from enemies) so in my StopFreeLook function I have this ...

        Controller.SetRotation(Rotation);
        bFreeLookMode = false;

        as shown before. What this means is that the Controllers Rotation (controlling the camera) will be set back to the rotation of the pawn RIGHT BEFORE our boolean is set to false which in turn means the pawns rotation is controlled by the controller. So basically its a little hacky and a bit of a cop out but it works well and Im happy with it.

        Anyway, any questions about this or the previous way to do it give me a mail or post here.

        P.s. the input command for the mouse is like this ...

        Code:
        ;-----------------------------------------------------------------------------------------
        ; Game Keyboard/Mouse Bindings
        ;-----------------------------------------------------------------------------------------
        
        ; Primary default bindings
        .Bindings=(Name="RightMouseButton",Command="FreeCamMode | OnRelease NonFreeCamMode")
        if you cant manage this part search the forums for "Player Input" but it should be easy, just add this line to your DefaultInput.ini in the section Ive shown and comment out the old right click using a ; semi colon. Delete your utinput.ini before doing so and then build all your files.

        Regards
        McTavish

        Comment


        • #5
          Thanks very much for posting this, I've been trying this out. I was hoping to achieve something similiar (eventually).

          I'm new to scripting, So it's a real headache ATM to solve even the simplest issues. Looking up every little thing....Sigh. But stuff like this is really helping.

          I got it working except for one small glitch. I was getting an error with the AIm node when extending off GamePawn.
          And resulting in (I think) the veiw snapping back when the right mouse button is released? Or is this intentional?
          I'm assuming my pawn class doesn't have the aim node or something? I had to paste in some defaults I grabbed from roychr's excellent isometroc camera tute to get the pawn to be visible. I'm guessing this is where my problem lies.

          Can I ask what classes you are extending from?
          Or if you could post the complete code for the Pawn? thad be great.


          This has been a huge help.

          Thanx.

          Comment


          • #6
            @McTavish: i have the same problem like Squiz theres no aim node... and do i have to replace a lot to get the camera to ratate by hitting a key?

            @Squiz: could you write the code you replaced?

            Comment


            • #7
              Hi Ghoul, I didn't actually replace anything, I just commented out the line....

              AimNode.Aim=vect2d(0.0,NewRotation.Pitch*0.0000610 3515625);

              So I'm not sure if I'm getting the intended result, or at least the complete effect.

              I was extending from GamePawn, and grabbed the default properties from roychr's tute.
              http://x9productions.com/blog/?page_id=357

              Otherwise he doesn't show up, I don't know if that is normal or not.
              Also the correction in defaults to place the pawn on the ground seems to interfere a little with the right mouse rotation.

              That's why I was hoping to get a peak at the full code for the Pawn. Pretty Please!!

              Cheers.

              Comment


              • #8
                Whats the problem ? the camera snaps back to behind the player ? that is ment, I would rather it did that anyway Ill just post my whole pawn guys with anything relating to this, there's a few things id rather leave out but hopefully this will help

                Code:
                class GGPawn extends GamePawn;
                
                /**
                 * Class Variables
                 */
                
                var bool bFreeLookMode;
                
                var	AnimNodeAimOffset		AimNode;
                
                /**
                 * Class Functions
                 */
                
                // Camera Calculation
                
                simulated function bool CalcCamera( float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV )
                {
                	local Vector    HitLocation, HitNormal, TraceStart, TraceEnd, Height, ActorViewPointHL;
                	local Actor     HitActor;
                	local Rotator   ActorViewPointRot;
                	local float     CameraDistance;
                
                	Height = vect(0,0,80);
                	CameraDistance = 200.f;
                
                	TraceStart = Location;
                	TraceEnd = Location - Vector(Controller.Rotation) * CameraDistance;
                
                	HitActor = Trace(HitLocation, HitNormal, TraceEnd, TraceStart, false);
                
                	if (HitActor != none)
                	{
                		out_CamLoc = HitLocation + Height;
                	}
                	else
                	{
                		out_CamLoc = (Location - Vector(Controller.Rotation) * CameraDistance) + Height;
                	}
                
                	GetActorEyesViewPoint(ActorViewPointHL, ActorViewPointRot);
                
                	out_CamRot = ActorViewPointRot;
                
                	return true;
                }
                
                // Free Look Mode
                
                function BeginFreeLook()
                {
                	bFreeLookMode = true;
                }
                
                function StopFreeLook()
                {
                	Controller.SetRotation(Rotation);
                	bFreeLookMode = false;
                }
                
                simulated function FaceRotation(rotator NewRotation, float DeltaTime)
                {
                	// Do not update Pawn's rotation depending on controller's ViewRotation if in FreeCam.
                	if (!bFreeLookMode)
                	{
                		if ( Physics == PHYS_Ladder )
                		{
                			NewRotation = OnLadder.Walldir;
                		}
                
                		//Update aimnode
                		if (NewRotation.Pitch>16384)
                			NewRotation.Pitch-=65536;
                		AimNode.Aim=vect2d(0.0,NewRotation.Pitch*0.00006103515625);
                
                		NewRotation.Pitch=0;
                		SetRotation(NewRotation);
                	}
                }
                
                /**
                 * Class Events
                 */
                
                simulated event PostInitAnimTree(SkeletalMeshComponent skelComp)
                {
                	super.PostInitAnimTree(skelComp);
                	if (skelComp == Mesh)
                	{
                		AimNode = AnimNodeAimOffset( mesh.FindAnimNode('AimNode') );
                		//AimNode.SetActiveProfileByName('SinglePistol');
                	}
                }
                
                /**
                 * Default properties Block
                 */
                
                DefaultProperties
                {
                
                    Begin Object Class=SkeletalMeshComponent Name=InitialSkeletalMesh
                   			CastShadow=true
                   			bCastDynamicShadow=true
                   			bOwnerNoSee=false
                			bUpdateKinematicBonesFromAnimation=true
                			BlockRigidBody=true
                			CollideActors=true
                			BlockZeroExtent=true
                    		AnimSets(0)=AnimSet'CH_AnimHuman.Anims.K_AnimHuman_BaseMale'
                		AnimSets(1)=AnimSet'CH_AnimHuman.Anims.K_AnimHuman_AimOffset'
                    		AnimTreeTemplate=AnimTree'CH_AnimHuman_Tree.AT_CH_Human'
                    		SkeletalMesh=SkeletalMesh'CH_IronGuard_Male.Mesh.SK_CH_IronGuard_MaleA'
                    	End Object
                    	Mesh=InitialSkeletalMesh;
                    	Components.Add(InitialSkeletalMesh);
                
                	Begin Object Name=CollisionCylinder
                		CollisionRadius=+0021.000000
                		CollisionHeight=+0048.000000
                	End Object
                
                	CylinderComponent=CollisionCylinder
                
                	bFreeLookMode = false
                }
                Ok thats my Pawn scaled down to only deal with the camera. Let me know if you have problems

                But as I say, I wanted the camera to snap back into place, if you dont want it to then remove the line ...

                Controller.SetRotation(Rotation);

                From the StopFreeLookMode() function

                regards

                Comment


                • #9
                  thanks, it works !

                  Comment


                  • #10
                    Good stuff both ways should work, the second is kind of cheating as the Pawns rotation is changing you just cant see it, but I think its fine

                    Comment


                    • #11
                      Yep, works great. Actually was working great already.
                      Didn't mean to say the camera snapping back was bad, I was just wondering what I was missing by having to comment out the aim node line and thought it might be related. After I posted last I realised that was logical.

                      Thanks for posting up the pawn, that cleared a lot of confusion for me.
                      I was mostly interested in the default settings for the pawn because what I was using seemed problematic.
                      Only been at this a couple of weeks so most things are confusing for me

                      Very much appreciated. Once again, a huge help for me.

                      Thanx.

                      Comment


                      • #12
                        No problem

                        Comment


                        • #13
                          Sorry I have problem

                          Code:
                          Log: I:\UDK\UDK-2010-01\Development\Src\UDKGame\Classes\DunDefPlayerController.uc(1864) : Error, Bad or missing expression after '!': 'bFreeLookMode'
                          Log: I:\UDK\UDK-2010-01\Development\Src\UDKGame\Classes\DunDefPlayerController.uc(1858) : Error, 'Controller': Bad command or expression
                          Log: I:\UDK\UDK-2010-01\Development\Src\UDKGame\Classes\DunDefPlayerController.uc(1853) : Error, 'bFreeLookMode': Bad command or expression
                          Log: I:\UDK\UDK-2010-01\Development\Src\UDKGame\Classes\DunDefPlayerController.uc(1847) : Error, 'GGPawn': Bad command or expression
                          Log: I:\UDK\UDK-2010-01\Development\Src\UDKGame\Classes\DunDefPlayerController.uc(1842) : Error, 'GGPawn': Bad command or expression
                          My version is January. Thanks a lot

                          Comment


                          • #14
                            OK, ok, it works very well, but camera only rotates when right_mouse is pressed, is not holded. How way to fix it ?? Thanks a lot!!!!

                            Comment


                            • #15
                              This is My Problem

                              This is your code
                              Code:
                              simulated function bool CalcCamera( float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV )
                              {
                              	local Vector    HitLocation, HitNormal, TraceStart, TraceEnd, Height, ActorViewPointHL;
                              	local Actor     HitActor;
                              	local Rotator   ActorViewPointRot;
                              	local float     CameraDistance;
                              
                              	Height = vect(0,0,80);
                              	CameraDistance = 200.f;
                              
                              	TraceStart = Location;
                              	TraceEnd = Location - Vector(Controller.Rotation) * CameraDistance;
                              
                              	HitActor = Trace(HitLocation, HitNormal, TraceEnd, TraceStart, false);
                              
                              	if (HitActor != none)
                              	{
                              		out_CamLoc = HitLocation + Height;
                              	}
                              	else
                              	{
                              		out_CamLoc = (Location - Vector(Controller.Rotation) * CameraDistance) + Height;
                              	}
                              
                              	GetActorEyesViewPoint(ActorViewPointHL, ActorViewPointRot);
                              
                              	out_CamRot = ActorViewPointRot;
                              
                              	return true;
                              
                              
                              }
                              
                              
                              // Free Look Mode
                              
                              function BeginFreeLook()
                              {
                              	//FaceRotation(Rotation,10.f);
                              	`Log("Press");
                              	bFreeLookMode = true;
                              }
                              
                              function StopFreeLook()
                              {
                              	`Log("Release");
                              	Controller.SetRotation(Rotation);
                              	bFreeLookMode = false;
                              }
                              simulated function FaceRotation(rotator NewRotation, float DeltaTime)
                              {	// Do not update Pawn's rotation depending on controller's ViewRotation if in FreeCam.
                              	if (bFreeLookMode==true)
                              	{
                              		`Log("Run");
                              		if ( Physics == PHYS_Ladder )
                              		{
                              			NewRotation = OnLadder.Walldir;
                              		}
                              
                              		//Update aimnode
                              		if (NewRotation.Pitch>16384)
                              			NewRotation.Pitch-=65536;
                              		AimNode.Aim=vect2d(0.0,NewRotation.Pitch*0.00006103515625);
                              
                              		NewRotation.Pitch=0;
                              		SetRotation(NewRotation);
                              	}
                              
                              }
                              In function BeginFreeLook, StopFreeLook,FaceRotation, I add into `Log("Press"); `Log("Release");`Log("Run"); to check and this is Result.

                              As you see, when I Press Right_click, Log appear "Press" but no "Run" and when I release Right_click, Log appear "Release"
                              You said "The Begin FreeLook sets the boolean to true which is then used in this function ... simulated function FaceRotation(rotator NewRotation, float DeltaTime)"
                              But sorry, function FaceRotation doesn't run.
                              Code:
                              function BeginFreeLook()
                              {
                              	bFreeLookMode = true;
                              }
                              
                              function StopFreeLook()
                              {
                              	
                              	Controller.SetRotation(Rotation);
                              	bFreeLookMode = false;
                              }
                              If I amnot wrong, Camera only is rotated by Hold Right_click when function FaceRotation runs ???? AnyIdea to fix it ??? Thanks a lot !!!

                              Comment

                              Working...
                              X