Unity 3D Particle System Snow effect - c#

Within unity 3D, Im building a 2d runner within which I have created a particle system that emits snow like particles from a single location at the top right corner of the main camera off screen.
I attached the particle emitter to the main camera so theres always snow like particles on screen, however, as the speed of the main camera increases, so does the speed of the particle emitter, which eventually starts emitting particles off screen.
How can I change the settings of the emitter so that the speed of the emitter does not affect the direction of the particles. Lastly for more details, I have set the shape of the emitter to be a cone, and the simulation space to be world. Any help would be much appreciated

You can change the Simulation Space setting of your ParticleSystem component : Particle System main module.
EDIT
Since I didn't noticed at first you wanted the particles speed to increase with the Camera accelerating (my bad...) here's a way you could fake the effect :
public class TestScript : MonoBehaviour
{
#region Attributes
[SerializeField]
private float m_MaxTransformSpeed;
[SerializeField]
private float m_MinStartSpeed;
[SerializeField]
private float m_MaxStartSpeed;
private ParticleSystem m_Particles;
private Vector3 m_PreviousPosition;
#endregion
#region MonoBehaviour
protected void Start()
{
m_Particles = GetComponent<ParticleSystem>();
}
protected void Update()
{
Vector3 cameraSpeed = transform.position - m_PreviousPosition;
m_Particles.startSpeed = Mathf.Clamp(m_MinStartSpeed + (m_MaxStartSpeed - m_MinStartSpeed) * cameraSpeed.magnitude / m_MaxTransformSpeed, m_MinStartSpeed, m_MaxStartSpeed);
m_PreviousPosition = transform.position;
}
#endregion
}
Instead of being based on only the latest frame you can either save N older positions and get the average speed value of N deltas or directly send the script the amount of movement you apply to your Camera if you move it manually.

Related

Strange behaviour of the Cinemachine virtual camera

I am using the new input system in my unity project. I also use Cinemachine. I use Cinemachine Input Provider to change the input from the old to the new system.
When I change max speed to Input Value Gain in speed field of virtual camera's settings (I did it because it is the most comfortable way to control camera) I face a problem.
My problem: When my character moves after some time the camera speed changes. If I start to move in the opposite direction, the camera speed returns to normal.
This is independent of the other components in the scene. My scene has only plane, cube, camera and my character.
Here's my character control code (ignore the awful calculation of movement direction):
private Rigidbody _rb;
private Vector2 _moveDirection;
private float speed = 5f;
private void Awake()
{
_rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
}
public void OnMove(InputAction.CallbackContext context)
{
_moveDirection = context.ReadValue<Vector2>();
}
private void FixedUpdate()
{
Move(_moveDirection);
}
private void Move(Vector3 moveDirection)
{
float scaledMoveSpeed = speed * Time.deltaTime;
moveDirection = new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z).normalized * moveDirection.y + new Vector3(Camera.main.transform.right.x, 0, Camera.main.transform.right.z).normalized * moveDirection.x;
_rb.MovePosition(transform.position + moveDirection * scaledMoveSpeed);
}
Here's a screenshot of the camera settings and Cinemachine Input Provider:
And screenshots of the Input Actions settings:
I found a solution for those who will ever face this problem! On your main camera, in the CinemachineBrain component, change the Update Method from Smart Update to Late Update. That should help

How to make an enemy follow the player with momentum in Unity?

I am a beginner in Unity developing a 2D top-down mobile game. I am trying to create an enemy movement script that mimics the pattern of the leech enemy below:
This enemy is constantly trying to move towards the player but even though it can move quite quickly, due to its momentum, you are able to kite it as it cannot make a sharp turn without first taking some time to build speed in another direction.
I have created a script for enemies to constantly be targeting the player based on the player's current position but it is too difficult to dodge my enemies as they are able to turn instantly when the player does and maintain a constant speed. I would like to balance them to be more like this leech enemy so the player can dodge them by taking advantage of the enemy's current momentum with proper timing. How can I create this momentum effect for my enemies?
If you're using Unity's physics here's a way to do this nicely:
Walkable.cs
Create a modular component that all walkable game objects will use. The purpose of the component is to keep the object moving in the specified direction while stabilising the forces. It uses the values you configure in the inspector for speed and force. It does the movement inside of FixedUpdate as physics movement require it.
public class Walkable : MonoBehaviour {
private const float ForcePower = 10f;
public new Rigidbody2D rigidbody;
public float speed = 2f;
public float force = 2f;
private Vector2 direction;
public void MoveTo (Vector2 direction) {
this.direction = direction;
}
public void Stop() {
MoveTo(Vector2.zero);
}
private void FixedUpdate() {
var desiredVelocity = direction * speed;
var deltaVelocity = desiredVelocity - rigidbody.velocity;
Vector3 moveForce = deltaVelocity * (force * ForcePower * Time.fixedDeltaTime);
rigidbody.AddForce(moveForce);
}
}
Character.cs
This is a simple example of character that will follow a target. Notice how all it's doing is passing the direction to the walkable from inside an Update function.
public class Character : MonoBehaviour {
public Transform target;
public Walkable walkable;
private void Update() {
var directionTowardsTarget = (target.position - this.transform.position).normalized;
walkable.MoveTo(directionTowardsTarget);
}
}
Configurations
By configuring move and force variables you can get a variety of movement styles, some that can move fast but take a long time to ramp up, some that ramp up fast but move slowly overall.
You can also play around with with mass and linear drag on the Rigidbody2D to get even more control over the movement style.

Keeping camera & mouse movement speeds the same on Drag in Unity3D

this question is a bit long but please bare with me.
I am working with Unity3D's newer input system, and I've been able to set up camera movement so when the user clicks the middle mouse button, they can drag the camera around along the X- & Y-axis. However, has a very parallax-y feel to it, which I am trying to avoid. From my understanding this is happening because the speed of the mouse movement & the camera movements are not the same (See, visual).
My InputController class:
public class InputController : MonoBehaviour
{
private InputControls inputControls;
[Header("Camera Movement")]
[SerializeField] CameraController cameraController;
private InputAction.CallbackContext context;
void Awake()
{
inputControls = new InputControls();
}
private void Start()
{
inputControls.Mouse.MiddleButton.started += MiddleButton_started;
}
public void MiddleButton_started(InputAction.CallbackContext ctx)
{
context = ctx;
}
private void Update()
{
bool mouseIsDown = context.performed;
if (mouseIsDown)
{
Vector2 delta = inputControls.Mouse.DeltaPosition.ReadValue<Vector2>();
cameraController.Move(delta, false);
}
}
}
My CameraController class:
public class CameraController : MonoBehaviour
{
[SerializeField] Vector2 minPosition;
[SerializeField] Vector2 maxPosition;
[SerializeField] float speed = 3;
[SerializeField] float zPosition = -10f;
private Camera mainCam;
private void Start()
{
mainCam = GetComponent<Camera>();
}
public void Move(Vector2 deltaPosition, bool convertToViewportPoint = true)
{
if (convertToViewportPoint) deltaPosition = mainCam.ScreenToViewportPoint(deltaPosition);
Vector3 moveTo = new Vector3(deltaPosition.x, deltaPosition.y, 0);
Vector3 target = transform.position - moveTo;
float clampedX = Mathf.Clamp(target.x, minPosition.x, maxPosition.x);
float clampedY = Mathf.Clamp(target.y, minPosition.y, maxPosition.y);
transform.position = Vector3.Lerp(transform.position, new Vector3(clampedX, clampedY, -10), speed * Time.deltaTime);
}
}
Now, in this version of the CameraController, the parallax-y feel might be coming from the fact that the Lerp speed is constant, whereas the speed of the mouse movement is not.
However, I've tested it with various magnitudes (ie., the magnitudes of the delta mouse position, the current mouse position, the current mouse position subtracted from the delta mouse position, etc...) with generally the same results -- either the camera moves too fast for the mouse or vice versa.
From my limited but growing understanding of trig, magnitudes represents the speed of a vector but I CANNOT get the speed of the camera movement and the speed of the mouse movement to match up. I would like for this to happen so that when the user clicks a certain point, that point generally stays under the cursor when in movement (See other visual).
Can someone help me understand how I might achieve this?
Thanks kindly.
What I did to solve this, in case anyone comes across this, is get the position of the cursor when the mouse is first clicked. We can call that dragOrigin. Then we can get the change in position as the mouse moves (polling the input through Unity's input system can be done through an Update function). Then we subtract the new mouse position from dragOrigin. Once we have that difference -- lets call it deltaPosition -- we just add it to the camera's current position.
To ensure that the speed matches that of the mouse, run the deltaPosition and the currentMousePosition through SmoothDamp to get the velocity of the change. Its magnitude will be the speed you want the camera to move.
So for example:
// Call this when the user first clicks the mouse.
// Lets assume this is part of a class that controls the camera.
public void SetDragOrigin(Vector2 mousePosition)
{
// could probably cache Camera.main for efficiency
dragOrigin = Camera.main.ScreenToWorldPoint(mousePosition);
}
// This is called in an update function of another class that deals with input.
// (Or perhaps you can simply pass the input controls to the class controlling the camera, but that's not the case here).
public void Move(Vector2 newMousePosition)
{
mousePosition = mainCam.ScreenToWorldPoint(mousePosition);
Vector2 deltaPosition = dragOrigin - mousePosition;
Vector3.SmoothDamp(deltaPosition, mousePosition, ref dragVelocity, 0.1f, 250f, Time.deltaTime);
cameraPosition += new Vector3(deltaPosition.x, deltaPosition.y, 0) * dragVelocity.magnitude * Time.deltaTime;
// anything else you want to do like clamping camera position
// ....
}
One thing to note here, is that the 5th argument of SmoothDamp is the maxSpeed. I hardcoded it in this example, but you might want to play around with it. When I did not set the maxSpeed, there was some wonky things happening to the dragVelocity over time, so it may be better to set the maxSpeed to what suits you best.

Camera Follow Causing Player To Stutter Unity 2D

I'm currently trying to make the camera follow the player smoothly. The script works fine but the problem is that this script is causing the player to stutter at a certain point. For example, if the player is at X:3, the player would stutter but then if the player was at X:-6, the player would stop stuttering. I'm 100% sure that this script is the problem because if I remove the script, the player stops stuttering.
Here is the camera follow script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowing : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 cameraOffset;
[SerializeField]
private float followSpeed = 10f;
[SerializeField]
private float xMin = 0f;
private Vector3 velocity = Vector3.zero;
private void FixedUpdate()
{
Vector3 targetPos = target.position + cameraOffset;
Vector3 clampedPos = new Vector3(Mathf.Clamp(targetPos.x, xMin, float.MaxValue), targetPos.y, targetPos.z);
Vector3 smoothPos = Vector3.SmoothDamp(transform.position, clampedPos, ref velocity, followSpeed * Time.fixedDeltaTime);
transform.position = smoothPos;
}
}
If you know the answer or possible causes please tell me, I'm trying to release, publish, etc, this game by the end of this year. Thanks! :D
By stutter I think you mean shake or tremble (not familiar with the stutter word).
I would try to adjust to the docs example. Use void Update() or LateUpdate() instead of FixedUpdate().
You may want to use void LateUpdate() if you want your game to be accurate. This method will be called after the input is detected, so it will react better. I believe that this is the better choice, because it will be more accurate than Update().
I would also keep track of where in the scene the shake starts if its from a determined point on, and check for some misplaced collider near the position where the undesired shake starts.

Ball object slows down without gravity

I'm making a 2D Block breaker game; the issue is that my ball will slow down after a while, yet there isn't any gravity and the bounciness is at 100% so it should keep all its kinetic energy. I just need the ball to stay at a constant speed.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public Paddle paddle;
private Vector3 paddleToBallVector;
private bool Started = false;
void Start () {
paddleToBallVector = this.transform.position
paddle.transform.position;
print(paddleToBallVector);
//this.GetComponent<Rigidbody2D>().drag = 0f;
}
void FixedUpdate () {
if (!Started) {
this.transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0))
{
Debug.Log("mouse clicked, Started = " + Started);
Started = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
}
}
The issue isn't in the code - it's in the game Gravity settings. You could solve it in one of two ways:
1) change the gravity scale of the ball - go to the ball game object or Prefab, and look at the RigidBody2D component. In there, you have a field called "Gravity Scale", usually set to 1. Changing it to 0.1 will make your ball light and quick. However, this setup isn't ideal for a game with multiple levels.
2) Change the global gravity in the project settings. Go in the Edit Menu to the Project Settings and in the menu choose Physics2D:
In the panel that is opened you usually have a realistic gravity scale of -9.81. Change it to something like -1.
This will make all the objects in your game light, and lessen the hold of gravity for all levels. In a brick-breaker type game when only the ball is loose and thrown around, it makes the most sense.
If you want the ball to have constant velocity, you could set it each frame in Update
myRigidbody.velocity = speed * (myRigidbody.velocity.normalized);
Where myRigidBody is a private RigidBody declared somewhere in your script, and set in Start() like this
myRigidbody = this.GetComponent<Rigidbody>();
Because you dont want to have to call GetComponent every single frame.
Create a physics material 2D. Keep the bounciness at 1 but reduce the friction to zero. Now attach that material to your Ball Collider. Your ball won't lose any speed when colliding now

Categories

Resources