Height Change

Ernanir

New member
Noob here.
I want to the following.

if (Input.GetKeyDown(KeyCode.X)) {
// Change the height of the FPS view point to 20.
}

How should I go about it?
 
You mean you want to move the Y position of the first-person camera, right? Something like this:

C#:
public CameraController cameraController; // assign in inspector, or get during runtime in Awake/Start

...

var viewType = cameraController.ActiveViewType;
if (viewType is FirstPerson) {
    var firstPerson = (FirstPerson)viewType;
    Vector3 lookOffset = firstPerson.LookOffset;
    lookOffset.y = 2;
    firstPerson.LookOffset = lookOffset;
}
 
Thanks for the quick reply, I think I'm missing something.

I think I did place the correct libraries. I'm getting this error:
Assets\Scripts\HeightChange.cs(30,32): error CS0246: The type or namespace name 'FirstPerson' could not be found (are you missing a using directive or an assembly reference?)

PS: How do place the C# format when posting here in the forum?

Code below:

using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Motion;
using Opsive.UltimateCharacterController.Utility;
using Opsive.Shared.Camera;
using Opsive.UltimateCharacterController.Camera;
using UnityEngine;


public class HeightChange : MonoBehaviour
{
public CameraController cameraController; // assign in inspector, or get during runtime in Awake/Start
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
var viewType = cameraController.ActiveViewType;
if (viewType is FirstPerson) {
var firstPerson = (FirstPerson)viewType;
Vector3 lookOffset = firstPerson.LookOffset;
lookOffset.y = 2;
firstPerson.LookOffset = lookOffset;
}
}
}
 
Noob here.
I want to the following.

if (Input.GetKeyDown(KeyCode.X)) {
// Change the height of the FPS view point to 20.
}

How should I go about it?
Andrew, please disregard the message above. I place the code under FirstPerson.cs and the error ceased to happen.
But I still had no luck with the height change.

I tried to change in the inspector and nothing. 400 did not changed the view point height.

1627926709925.png


I tried to place your code under an Update void and that did not work. Whole code here.

void Update()
{
var viewType = cameraController.ActiveViewType;
if (viewType is FirstPerson)
{
var firstPerson = (FirstPerson)viewType;
Vector3 lookOffset = firstPerson.LookOffset;
lookOffset.y = 200;
firstPerson.LookOffset = lookOffset;
}
}
 
You can't use an Update loop on a ViewType because it's not a MonoBehaviour. You'd need to use the Update loop on a custom script or another MonoBehaviour script you're using elsewhere.
 
You can't use an Update loop on a ViewType because it's not a MonoBehaviour. You'd need to use the Update loop on a custom script or another MonoBehaviour script you're using elsewhere
1 - On this custom script what libraries do I need to reference?
2 - How can I solve the issue I mentioned in the previous post? This should change the default height without the need for any code correct?
1628021327526.png
 
1. To find the right namespace to include in your scripts, find the class that is missing a reference an d check the namespace definition at the top of the script. In your case, find FirstPerson.cs and you'll see the namespace definition at the top of the script: namespace Opsive.UltimateCharacterController.FirstPersonController.Camera.ViewTypes. So in your own script, you'd need to include Opsive.UltimateCharacterController.FirstPersonController.Camera.ViewTypes;.

2. The CharacterIK component has nothing to do with the camera. The LookAtOffset property on the CharacterIK component will add an offset to where the character is looking (e.g. in third-person, you can adjust this to make the character look up/down etc). The LookAtOffset property you need to adjust the camera height is in the FirstPerson class, i.e. it's a property of any FirstPerson view type.
 
Thanks, Andrew.

2 - Got it, I was tweaking the wrongs section.

The LookAtOffset property you need to adjust the camera height is in the FirstPerson class, i.e. it's a property of any FirstPerson view type.

How can I make this variable public in the inspector?
 
It already is:

1628150227676.png

Look Offset is under the "Rendering" section of the first person view type, in the CameraController.
 
This worked out :D Thank you.
Now the offset is as intended.

Question: I'm still unsure would be the substitute to the Update loop?
What void should I use to place my condition and fire it? Like the one below?

if (Input.GetKeyDown(KeyCode.X)) {

}


1628188519464.png
 
You could just put it in Update with an Input.GetKeyDown check like you said. Or if you want it to happen at the same time as something else, e.g. when another ability starts or a state gets activated, then you could do it in other ways, e.g. by using the event system, or AbilityStarted, etc.
 
Top