Unity 3D – Architectural Walkthrough with interactive actions.
Start in Sketchup
Model house done by brunurb L. (https://3dwarehouse.sketchup.com/model/94b5b4e7f7f4243610582ac1f07d1ac/Mon-Oncle-house) from Tati’s “Mon Oncle” http://www.imdb.com/title/tt0050706.
Before exporting to Unity, beware of reverse faces.
Click and check the blue faces.
Right Mouse Click / reverse faces
The model is now clean.
Check scales before exporting.
Export in FBX with these options
Create a new Unity Project
Drag and drop the FBX file.
Prepare the model with checking the scale factor. Generate colliders.
Create a FPSController and test the visit
To interact with the environment, we need to free the cursor and make it independent from the mouse look.
The FPSController is structured as followed
And
Uncheck First Person Controler and Audio Source
You can create a new asset from the original one in a specific folder.
We will then add a first script to the FPSController
First Script CharacterMotor.JS
#pragma strict #pragma implicit #pragma downcast <em>// Does this script currently respond to input?</em> var canControl : boolean = true; var useFixedUpdate : boolean = true; <em>// For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.</em> <em>// Very handy for organization!</em> <em>// The current global direction we want the character to move in.</em> @System.NonSerialized var inputMoveDirection : Vector3 = Vector3.zero; <em>// Is the jump button held down? We use this interface instead of checking</em> <em>// for the jump button directly so this script can also be used by AIs.</em> @System.NonSerialized var inputJump : boolean = false; class CharacterMotorMovement { <em>// The maximum horizontal speed when moving</em> var maxForwardSpeed : float = 10.0; var maxSidewaysSpeed : float = 10.0; var maxBackwardsSpeed : float = 10.0; <em>// Curve for multiplying speed based on slope (negative = downwards)</em> var slopeSpeedMultiplier : AnimationCurve = AnimationCurve(Keyframe(-90, 1), Keyframe(0, 1), Keyframe(90, 0)); <em>// How fast does the character change speeds? Higher is faster.</em> var maxGroundAcceleration : float = 30.0; var maxAirAcceleration : float = 20.0; <em>// The gravity for the character</em> var gravity : float = 10.0; var maxFallSpeed : float = 20.0; <em>// For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.</em> <em>// Very handy for organization!</em> <em>// The last collision flags returned from controller.Move</em> @System.NonSerialized var collisionFlags : CollisionFlags; <em>// We will keep track of the character's current velocity,</em> @System.NonSerialized var velocity : Vector3; <em>// This keeps track of our current velocity while we're not grounded</em> @System.NonSerialized var frameVelocity : Vector3 = Vector3.zero; @System.NonSerialized var hitPoint : Vector3 = Vector3.zero; @System.NonSerialized var lastHitPoint : Vector3 = Vector3(Mathf.Infinity, 0, 0); } var movement : CharacterMotorMovement = CharacterMotorMovement(); enum MovementTransferOnJump { None, <em>// The jump is not affected by velocity of floor at all.</em> InitTransfer, <em>// Jump gets its initial velocity from the floor, then gradualy comes to a stop.</em> PermaTransfer, <em>// Jump gets its initial velocity from the floor, and keeps that velocity until landing.</em> PermaLocked <em>// Jump is relative to the movement of the last touched floor and will move together with that floor.</em> } <em>// We will contain all the jumping related variables in one helper class for clarity.</em> class CharacterMotorJumping { <em>// Can the character jump?</em> var enabled : boolean = true; <em>// How high do we jump when pressing jump and letting go immediately</em> var baseHeight : float = 1.0; <em>// We add extraHeight units (meters) on top when holding the button down longer while jumping</em> var extraHeight : float = 4.1; <em>// How much does the character jump out perpendicular to the surface on walkable surfaces?</em> <em>// 0 means a fully vertical jump and 1 means fully perpendicular.</em> var perpAmount : float = 0.0; <em>// How much does the character jump out perpendicular to the surface on too steep surfaces?</em> <em>// 0 means a fully vertical jump and 1 means fully perpendicular.</em> var steepPerpAmount : float = 0.5; <em>// For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.</em> <em>// Very handy for organization!</em> <em>// Are we jumping? (Initiated with jump button and not grounded yet)</em> <em>// To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.</em> @System.NonSerialized var jumping : boolean = false; @System.NonSerialized var holdingJumpButton : boolean = false; <em>// the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)</em> @System.NonSerialized var lastStartTime : float = 0.0; @System.NonSerialized var lastButtonDownTime : float = -100; @System.NonSerialized var jumpDir : Vector3 = Vector3.up; } var jumping : CharacterMotorJumping = CharacterMotorJumping(); class CharacterMotorMovingPlatform { var enabled : boolean = true; var movementTransfer : MovementTransferOnJump = MovementTransferOnJump.PermaTransfer; @System.NonSerialized var hitPlatform : Transform; @System.NonSerialized var activePlatform : Transform; @System.NonSerialized var activeLocalPoint : Vector3; @System.NonSerialized var activeGlobalPoint : Vector3; @System.NonSerialized var activeLocalRotation : Quaternion; @System.NonSerialized var activeGlobalRotation : Quaternion; @System.NonSerialized var lastMatrix : Matrix4x4; @System.NonSerialized var platformVelocity : Vector3; @System.NonSerialized var newPlatform : boolean; } var movingPlatform : CharacterMotorMovingPlatform = CharacterMotorMovingPlatform(); class CharacterMotorSliding { <em>// Does the character slide on too steep surfaces?</em> var enabled : boolean = true; <em>// How fast does the character slide on steep surfaces?</em> var slidingSpeed : float = 15; <em>// How much can the player control the sliding direction?</em> <em>// If the value is 0.5 the player can slide sideways with half the speed of the downwards sliding speed.</em> var sidewaysControl : float = 1.0; <em>// How much can the player influence the sliding speed?</em> <em>// If the value is 0.5 the player can speed the sliding up to 150% or slow it down to 50%.</em> var speedControl : float = 0.4; } var sliding : CharacterMotorSliding = CharacterMotorSliding(); @System.NonSerialized var grounded : boolean = true; @System.NonSerialized var groundNormal : Vector3 = Vector3.zero; private var lastGroundNormal : Vector3 = Vector3.zero; private var tr : Transform; private var controller : CharacterController; function Awake () { controller = GetComponent (CharacterController); tr = transform; } private function UpdateFunction () { <em>// We copy the actual velocity into a temporary variable that we can manipulate.</em> var velocity : Vector3 = movement.velocity; <em>// Update velocity based on input</em> velocity = ApplyInputVelocityChange(velocity); <em>// Apply gravity and jumping force</em> velocity = ApplyGravityAndJumping (velocity); <em>// Moving platform support</em> var moveDistance : Vector3 = Vector3.zero; if (MoveWithPlatform()) { var newGlobalPoint : Vector3 = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint); moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint); if (moveDistance != Vector3.zero) controller.Move(moveDistance); <em>// Support moving platform rotation as well:</em> var newGlobalRotation : Quaternion = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation; var rotationDiff : Quaternion = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation); var yRotation = rotationDiff.eulerAngles.y; if (yRotation != 0) { <em>// Prevent rotation of the local up vector</em> tr.Rotate(0, yRotation, 0); } } <em>// Save lastPosition for velocity calculation.</em> var lastPosition : Vector3 = tr.position; <em>// We always want the movement to be framerate independent. Multiplying by Time.deltaTime does this.</em> var currentMovementOffset : Vector3 = velocity * Time.deltaTime; <em>// Find out how much we need to push towards the ground to avoid loosing grouning</em> <em>// when walking down a step or over a sharp change in slope.</em> var pushDownOffset : float = Mathf.Max(controller.stepOffset, Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude); if (grounded) currentMovementOffset -= pushDownOffset * Vector3.up; <em>// Reset variables that will be set by collision function</em> movingPlatform.hitPlatform = null; groundNormal = Vector3.zero; <em>// Move our character!</em> movement.collisionFlags = controller.Move (currentMovementOffset); movement.lastHitPoint = movement.hitPoint; lastGroundNormal = groundNormal; if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform) { if (movingPlatform.hitPlatform != null) { movingPlatform.activePlatform = movingPlatform.hitPlatform; movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix; movingPlatform.newPlatform = true; } } <em>// Calculate the velocity based on the current and previous position. </em> <em>// This means our velocity will only be the amount the character actually moved as a result of collisions.</em> var oldHVelocity : Vector3 = new Vector3(velocity.x, 0, velocity.z); movement.velocity = (tr.position - lastPosition) / Time.deltaTime; var newHVelocity : Vector3 = new Vector3(movement.velocity.x, 0, movement.velocity.z); <em>// The CharacterController can be moved in unwanted directions when colliding with things.</em> <em>// We want to prevent this from influencing the recorded velocity.</em> if (oldHVelocity == Vector3.zero) { movement.velocity = new Vector3(0, movement.velocity.y, 0); } else { var projectedNewVelocity : float = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude; movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up; } if (movement.velocity.y < velocity.y - 0.001) { if (movement.velocity.y < 0) { <em>// Something is forcing the CharacterController down faster than it should.</em> <em>// Ignore this</em> movement.velocity.y = velocity.y; } else { <em>// The upwards movement of the CharacterController has been blocked.</em> <em>// This is treated like a ceiling collision - stop further jumping here.</em> jumping.holdingJumpButton = false; } } <em>// We were grounded but just loosed grounding</em> if (grounded && !IsGroundedTest()) { grounded = false; <em>// Apply inertia from platform</em> if (movingPlatform.enabled && (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) ) { movement.frameVelocity = movingPlatform.platformVelocity; movement.velocity += movingPlatform.platformVelocity; } SendMessage("OnFall", SendMessageOptions.DontRequireReceiver); <em>// We pushed the character down to ensure it would stay on the ground if there was any.</em> <em>// But there wasn't so now we cancel the downwards offset to make the fall smoother.</em> tr.position += pushDownOffset * Vector3.up; } <em>// We were not grounded but just landed on something</em> else if (!grounded && IsGroundedTest()) { grounded = true; jumping.jumping = false; SubtractNewPlatformVelocity(); SendMessage("OnLand", SendMessageOptions.DontRequireReceiver); } <em>// Moving platforms support</em> if (MoveWithPlatform()) { <em>// Use the center of the lower half sphere of the capsule as reference point.</em> <em>// This works best when the character is standing on moving tilting platforms. </em> movingPlatform.activeGlobalPoint = tr.position + Vector3.up * (controller.center.y - controller.height*0.5 + controller.radius); movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint); <em>// Support moving platform rotation as well:</em> movingPlatform.activeGlobalRotation = tr.rotation; movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation; } } function FixedUpdate () { if (movingPlatform.enabled) { if (movingPlatform.activePlatform != null) { if (!movingPlatform.newPlatform) { var lastVelocity : Vector3 = movingPlatform.platformVelocity; movingPlatform.platformVelocity = ( movingPlatform.activePlatform.localToWorldMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint) - movingPlatform.lastMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint) ) / Time.deltaTime; } movingPlatform.lastMatrix = movingPlatform.activePlatform.localToWorldMatrix; movingPlatform.newPlatform = false; } else { movingPlatform.platformVelocity = Vector3.zero; } } if (useFixedUpdate) UpdateFunction(); } function Update () { if (!useFixedUpdate) UpdateFunction(); } private function ApplyInputVelocityChange (velocity : Vector3) { if (!canControl) inputMoveDirection = Vector3.zero; <em>// Find desired velocity</em> var desiredVelocity : Vector3; if (grounded && TooSteep()) { <em>// The direction we're sliding in</em> desiredVelocity = Vector3(groundNormal.x, 0, groundNormal.z).normalized; <em>// Find the input movement direction projected onto the sliding direction</em> var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity); <em>// Add the sliding direction, the spped control, and the sideways control vectors</em> desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl; <em>// Multiply with the sliding speed</em> desiredVelocity *= sliding.slidingSpeed; } else desiredVelocity = GetDesiredHorizontalVelocity(); if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) { desiredVelocity += movement.frameVelocity; desiredVelocity.y = 0; } if (grounded) desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal); else velocity.y = 0; <em>// Enforce max velocity change</em> var maxVelocityChange : float = GetMaxAcceleration(grounded) * Time.deltaTime; var velocityChangeVector : Vector3 = (desiredVelocity - velocity); if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) { velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange; } <em>// If we're in the air and don't have control, don't apply any velocity change at all.</em> <em>// If we're on the ground and don't have control we do apply it - it will correspond to friction.</em> if (grounded || canControl) velocity += velocityChangeVector; if (grounded) { <em>// When going uphill, the CharacterController will automatically move up by the needed amount.</em> <em>// Not moving it upwards manually prevent risk of lifting off from the ground.</em> <em>// When going downhill, DO move down manually, as gravity is not enough on steep hills.</em> velocity.y = Mathf.Min(velocity.y, 0); } return velocity; } private function ApplyGravityAndJumping (velocity : Vector3) { if (!inputJump || !canControl) { jumping.holdingJumpButton = false; jumping.lastButtonDownTime = -100; } if (inputJump && jumping.lastButtonDownTime < 0 && canControl) jumping.lastButtonDownTime = Time.time; if (grounded) velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime; else { velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime; <em>// When jumping up we don't apply gravity for some time when the user is holding the jump button.</em> <em>// This gives more control over jump height by pressing the button longer.</em> if (jumping.jumping && jumping.holdingJumpButton) { <em>// Calculate the duration that the extra jump force should have effect.</em> <em>// If we're still less than that duration after the jumping time, apply the force.</em> if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) { <em>// Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.</em> velocity += jumping.jumpDir * movement.gravity * Time.deltaTime; } } <em>// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.</em> velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed); } if (grounded) { <em>// Jump only if the jump button was pressed down in the last 0.2 seconds.</em> <em>// We use this check instead of checking if it's pressed down right now</em> <em>// because players will often try to jump in the exact moment when hitting the ground after a jump</em> <em>// and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,</em> <em>// it's confusing and it feels like the game is buggy.</em> if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) { grounded = false; jumping.jumping = true; jumping.lastStartTime = Time.time; jumping.lastButtonDownTime = -100; jumping.holdingJumpButton = true; <em>// Calculate the jumping direction</em> if (TooSteep()) jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount); else jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount); <em>// Apply the jumping force to the velocity. Cancel any vertical velocity first.</em> velocity.y = 0; velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight); <em>// Apply inertia from platform</em> if (movingPlatform.enabled && (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) ) { movement.frameVelocity = movingPlatform.platformVelocity; velocity += movingPlatform.platformVelocity; } SendMessage("OnJump", SendMessageOptions.DontRequireReceiver); } else { jumping.holdingJumpButton = false; } } return velocity; } function OnControllerColliderHit (hit : ControllerColliderHit) { if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) { if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero) groundNormal = hit.normal; else groundNormal = lastGroundNormal; movingPlatform.hitPlatform = hit.collider.transform; movement.hitPoint = hit.point; movement.frameVelocity = Vector3.zero; } } private function SubtractNewPlatformVelocity () { <em>// When landing, subtract the velocity of the new ground from the character's velocity</em> <em>// since movement in ground is relative to the movement of the ground.</em> if (movingPlatform.enabled && (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer || movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) ) { <em>// If we landed on a new platform, we have to wait for two FixedUpdates</em> <em>// before we know the velocity of the platform under the character</em> if (movingPlatform.newPlatform) { var platform : Transform = movingPlatform.activePlatform; yield WaitForFixedUpdate(); yield WaitForFixedUpdate(); if (grounded && platform == movingPlatform.activePlatform) yield 1; } movement.velocity -= movingPlatform.platformVelocity; } } private function MoveWithPlatform () : boolean { return ( movingPlatform.enabled && (grounded || movingPlatform.movementTransfer == MovementTransferOnJump.PermaLocked) && movingPlatform.activePlatform != null ); } private function GetDesiredHorizontalVelocity () { <em>// Find desired velocity</em> var desiredLocalDirection : Vector3 = tr.InverseTransformDirection(inputMoveDirection); var maxSpeed : float = MaxSpeedInDirection(desiredLocalDirection); if (grounded) { <em>// Modify max speed on slopes based on slope speed multiplier curve</em> var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y) * Mathf.Rad2Deg; maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle); } return tr.TransformDirection(desiredLocalDirection * maxSpeed); } private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 { var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity); return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude; } private function IsGroundedTest () { return (groundNormal.y > 0.01); } function GetMaxAcceleration (grounded : boolean) : float { <em>// Maximum acceleration on ground and in air</em> if (grounded) return movement.maxGroundAcceleration; else return movement.maxAirAcceleration; } function CalculateJumpVerticalSpeed (targetJumpHeight : float) { <em>// From the jump height and gravity we deduce the upwards speed </em> <em>// for the character to reach at the apex.</em> return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity); } function IsJumping () { return jumping.jumping; } function IsSliding () { return (grounded && sliding.enabled && TooSteep()); } function IsTouchingCeiling () { return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0; } function IsGrounded () { return grounded; } function TooSteep () { return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad)); } function GetDirection () { return inputMoveDirection; } function SetControllable (controllable : boolean) { canControl = controllable; } <em>// Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.</em> <em>// The function returns the length of the resulting vector.</em> function MaxSpeedInDirection (desiredMovementDirection : Vector3) : float { if (desiredMovementDirection == Vector3.zero) return 0; else { var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed; var temp : Vector3 = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized; var length : float = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed; return length; } } function SetVelocity (velocity : Vector3) { grounded = false; movement.velocity = velocity; movement.frameVelocity = Vector3.zero; SendMessage("OnExternalVelocity"); } <em>// Require a character controller to be attached to the same game object</em> @script RequireComponent (CharacterController) @script AddComponentMenu ("Character/Character Motor") |
Second script The FPSInputController
private var motor : CharacterMotor; <em>// Use this for initialization</em> function Awake () { motor = GetComponent(CharacterMotor); } <em>// Update is called once per frame</em> function Update () { <em>// Get the input vector from kayboard or analog stick</em> var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); if (directionVector != Vector3.zero) { <em>// Get the length of the directon vector and then normalize it</em> <em>// Dividing by the length is cheaper than normalizing when we already have the length anyway</em> var directionLength = directionVector.magnitude; directionVector = directionVector / directionLength; <em>// Make sure the length is no bigger than 1</em> directionLength = Mathf.Min(1, directionLength); <em>// Make the input vector more sensitive towards the extremes and less sensitive in the middle</em> <em>// This makes it easier to control slow speeds when using analog sticks</em> directionLength = directionLength * directionLength; <em>// Multiply the normalized direction vector by the modified length</em> directionVector = directionVector * directionLength; } <em>// Apply the direction to the CharacterMotor</em> motor.inputMoveDirection = transform.rotation * directionVector; motor.inputJump = Input.GetButton("Jump"); } <em>// Require a character controller to be attached to the same game object</em> @script RequireComponent (CharacterMotor) @script AddComponentMenu ("Character/FPS Input Controller") |
The third one is a modification of the MouseLook. In the original MouseLook, it’s difficult to aim and click precisely a specific object or a menu. With this modification, the player presses on MiddleMouseButton (for instance) to look.
Comparison between the standard Mouse Look and the modified one.
The mofified MouseLook called MouseLookClick is as followed:
using UnityEngine; using System.Collections; <em>/// MouseLook rotates the transform based on the mouse delta.</em> <em>/// Minimum and Maximum values can be used to constrain the possible rotation</em> <em>/// To make an FPS style character:</em> <em>/// - Create a capsule.</em> <em>/// - Add the MouseLook script to the capsule.</em> <em>/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)</em> <em>/// - Add FPSInputController script to the capsule</em> <em>/// -> A CharacterMotor and a CharacterController component will be automatically added.</em> <em>/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.</em> <em>/// - Add a MouseLook script to the camera.</em> <em>/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)</em> [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLookClick : MonoBehaviour { public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F; public float minimumX = -360F; public float maximumX = 360F; public float minimumY = -60F; public float maximumY = 60F; float rotationY = 0F; void Update () { if (axes == RotationAxes.MouseXAndY) { if (Input.GetMouseButton (2)) { float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0); } } else if (axes == RotationAxes.MouseX) { if (Input.GetMouseButton (2)) { transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityX, 0); } } else { if (Input.GetMouseButton (2)) { rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0); } } } void Start () { <em>// Make the rigid body not change rotation</em> if (GetComponent<Rigidbody>()) GetComponent<Rigidbody>().freezeRotation = true; } } |
To choose the Mouse Button:
if (Input.GetMouseButton (2)) |
0 is Left Mouse Button, 1 is Right Mouse Button and 2 is Middle Mouse Button.
Drag and drop the scripts to the FPSController
Choose Mouse X for Axes
Drag and drop MouseLookClick to the FirstPersonCharacter
Choose Mouse Y for Axes
Test.
Now, we will change the wall aspect with menu
Create 4 Materials
Copy and convert your texture into Sprites
Add a Canvas and a first Button (WHITE is the button’s name)
Name the button, set a good position, then put one the sprite.
Set the button’s label
Let’s add a new script
var material : Material; var object : GameObject; function OnMouseDown(){ GetComponent.<Renderer>().material = material; } |
Apply the script to an object, a group a walls in our example.
It is not necessary to fill material and object items.
Go back to the button Script.
Add the walls as objects, select MeshRenderer / Material material and select the material
Final result.
Repeat for each button
Result
Now, we will decide that the menu appears only when stepping close to the façade.
Create a box.
The box is not rendered and is trigger
Create a java Script Load-Menu
var Menu : Canvas; function OnTriggerEnter () { Menu.enabled = true; } function OnTriggerExit () { Menu.enabled = false; } |
Set Canvas Off
Drag and drop the script to the trigger cube
Add Canvas in the var Zone
Test
Last step, create a menu to load or mask objects
Create a Java Script (not necessary in fact)
var Tree : GameObject; function Start () { Tree.enabled = true; } |
Create a new Canvas and a Toggle button, I named it Trees-On-Off
Add objects in the options as followed
Test
Hi
is perfect tutorial
thanks