By using a simple trigger collider. I've made an automatic door that moves out of the way of the player when they come into contact with it.
void Update()
{
if (!_isClosing && !_isOpening)
{
return;
}
if (_isOpening)
{
_targetPos = _openPosition;
}
else if (_isClosing)
{
_targetPos = _closedPosition;
}
_doorPivot.transform.localPosition = Vector3.MoveTowards(current: _doorPivot.transform.localPosition,
target: _targetPos, maxDistanceDelta: _speed * Time.deltaTime);
if (Vector3.Distance(a: _doorPivot.transform.localPosition, b: _targetPos) <= 0.001f)
{
_isClosing = false;
_isOpening = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
_isOpening = true;
_isClosing = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
_isOpening = false;
_isClosing = true;
}
}
}
Using Unity's particle system, I was able to create an effect that was exactly what I was looking for. A spinning dispersal of purple green and yellow stars for both pads.
Using Unity's Input Actions, I've made a firing mode meant to replicate the feeling of using the coconut gun from donkey kong 64, to capture the feeling, I included the same object limit as well as the same de-spawn timing.
Balloons
The Balloon objects that are shot at are programmed to only pop when shot with the coconuts, give 10 bananas, and make the UI appear.
These balloons also use the same code as the moving platform.
void Update()
{
bool isCrouching = playerMovement.crouchAction.action.IsPressed();
bool leftClick = primaryFireAction.action.IsPressed();
bool rightClick = alternateFireAction.action.IsPressed();
// Toggling Shootmode Logic
if (rightClick && !prevRightClick && isCrouching)
{
ToggleShootMode();
}
// Shooting Logic
if (isInShootMode && leftClick && !prevLeftClick)
{
Shoot();
}
prevRightClick = rightClick;
prevLeftClick = leftClick;
}
private void ToggleShootMode()
{
isInShootMode = !isInShootMode;
if (crosshair != null)
crosshair.enabled = isInShootMode;
}
private void Shoot()
{
if (coconutPrefab == null || cameraTransform == null || Coconut.activeCoconuts >= Coconut.maxCoconuts)
return;
Vector3 spawnPos = cameraTransform.position + cameraTransform.forward * spawnDistance;
GameObject coconut = Instantiate(coconutPrefab, spawnPos, Quaternion.identity);
Rigidbody rb = coconut.GetComponent<Rigidbody>();
if (rb != null)
{
rb.linearVelocity = cameraTransform.forward * shootForce;
}
}
using UnityEngine;
public class SinPlatform : MonoBehaviour
{
public enum Axis
{
X,
Y,
Z
}
[SerializeField] private Axis axisDirection = Axis.Z;
[SerializeField] private float amplitude = 2f;
[SerializeField] private float frequency = 1f;
[SerializeField] private float offset = 0f;
private Vector3 startPos;
public Vector3 movementPerFrame;
public Vector3 movementPerSecond;
private Vector3 lastPos;
void Start()
{
startPos = transform.position;
lastPos = startPos;
}
void Update()
{
float sinMovement = Mathf.Sin(Time.time * frequency) * amplitude;
Vector3 newPos = startPos;
switch (axisDirection)
{
case Axis.X:
newPos.x = startPos.x + offset + sinMovement;
break;
case Axis.Y:
newPos.y = startPos.y + offset + sinMovement;
break;
case Axis.Z:
newPos.z = startPos.z + offset + sinMovement;
break;
}
transform.position = newPos;
}
void LateUpdate()
{
movementPerFrame = transform.position - lastPos;
movementPerSecond = movementPerFrame / Time.deltaTime;
lastPos = transform.position;
}
}
This Character Controller has the ability to jump, crouch run and mentioned above, fire projectiles.
Translating mouse movement to affect the first person camera
using UnityEngine;
public class PlayerCamera : MonoBehaviour
{
public Transform player;
public float sensitivity = 200f;
private float minPitch = -89f;
private float maxPitch = 89f;
private float pitch = 0f;
public void Start()
{
Cursor.visible = false;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
// rotate player horizontally
transform.parent.Rotate(Vector3.up, mouseX);
// rotate camera vertically
pitch -= mouseY;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
}
}