I have been working on trying to smooth out my camera motion for my game for days and I cannot get it to a reasonable level of smoothness. Are there any corrections to this code that I can make that will improve the smoothness? I have tried the moveTowards() function as well and it didn't help.
/**************************** CAMERA MOVEMENT *********************************/
void FixedUpdate () {
//Do we need to spawn new platforms yet?
Vector2 playerHeight = playerTransform.position;
if (playerHeight.y > nextPlatformCheck) {
PlatformMaintenaince (); //Spawn new platforms
}
//Update camera position if the player has climbed
Vector2 currentCameraHeight = transform.position;
if (playerTransform.position.y > currentCameraHeight.y)
{
transform.position = Vector2.Lerp(new Vector2(transform.position.x,0.0f), new Vector2(0.0f, playerHeight.y), smooth * Time.deltaTime);
}
else {
// If player is too low, gameover.
if ((playerHeight.y) < (currentCameraHeight.y - 5.5)) {
GameOver();
}
}
}
Use the Update function instead of the FixedUpdate. FixedUpdate only gets called after a certain number od frames.
All the camera movement calculations should be carried out in the LateUpdate (), Let Update() get the player position and then let camera use that position after that Update calculations.
Related
I'm currently making a 2D game as a beginner and I made a spinning platform. But when it's rotating the player's rotation (z-axis) also changes because it's a child of the platform. I need this when I use moving platforms. Now I want to lock the z-axis of the rotation of the player. I already tried it in 3 different ways, but none of them seems to be working. Does anybody know how to do this?
These are the three ways I tried:
// 1
PlayerTrans.transform.Rotate(
PlayerTrans.transform.rotation.x,
PlayerTrans.transform.rotation.y,
0);
// 2
PlayerTrans.transform.Rotate(
PlayerTrans.transform.rotation.x,
PlayerTrans.transform.rotation.y,
0,
Space.Self);
// 3
PlayerTrans.transform.localRotation = Quaternion.Euler(new Vector3(
PlayerTrans.transform.localEulerAngles.x,
PlayerTrans.transform.localEulerAngles.y,
0f));
and this is, what my code looks like for staying on the moving platforms. I used raycasting for this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycasting : MonoBehaviour
{
// Start is called before the first frame update
Transform PlayerTrans;
public float RayCastRange = 3;
void Start()
{
PlayerTrans = transform.parent;
}
// Update is called once per frame
void Update()
{
RaycastHit2D PlattformCheck = Physics2D.Raycast(transform.position, -Vector2.up, RayCastRange);
if (PlattformCheck.collider != null)
{
if (PlattformCheck.collider.gameObject.tag == "Platform")
{
PlayerTrans.transform.SetParent(PlattformCheck.collider.gameObject.transform);
}
else
{
PlayerTrans.transform.SetParent(null);
}
}
else
{
PlayerTrans.transform.SetParent(null);
}
}
}
There are 2 ways that might help you:
Just freeze the rotation from the inspector:
you can use some LookAt function (there is one for 3D but you can look and find ones for 2D) and just look at the camera.
(if you cant find it let me know and I will add it)
You should raycast directly down and then apply velocities to both objects (un-child the player from the platforms). You could do something like this for the player:
public LayerMask mask; //set ‘mask’ to the mask of the
//platform in the Unity Editor.
public Vector3 velocity;
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit, 0.1f, mask))
//0.1f is the distance to the platform to be able to be moved by the platform.
{
velocity = hit.collider.gameObject.GetComponent<Platform>().velocity;
}
float h = Input.GetAxis(“Horizontal”);
//this one is for CharacterController:
cc.Move(velocity);
//this one is for rigidbody:
rb.velocity = velocity;
velocity = 0;
}
It takes the velocity from the ‘Platform’ script and moves the player based on it. Now we should add the platform script. Call it ‘Platform’.
public Vector3 velocity;
public Vector3 a; //a and b are the two points you want the platform to go between.
public Vector3 b;
public float period = 2f; //the amount of seconds per cycle.
float cycles;
void Update()
{
cycles = Time.time / period;
float amplitude = Mathf.Sin(Mathf.PI * 2f * cycles);
Vector3 location = Vector3.Lerp(a, b, amplitude);
velocity = transform.position - location;
transform.position = velocity;
}
This script interpolates between the point a and b, then it finds the velocity and applies it. The player takes this velocity and it moves the player. Comment if you found an error.
I am trying to make a 2d plat-former where you see the player from the side. I want him to be continuously moving and you have to press space at the right time so he doesn't fall. Right now everything works but he doesn't collide with the ground. I want it to be like he's running behind a wall so I want to ignore a certain layer I have made and collide with the boxes below that. So far I have tried ray casting, watched multiple tutorials, and did box collisions. Box collisions worked but to get all the platforms counted as solid I'd need like 50 box colliders. Here is my current code:
public int playerSpeed = 10;
public int playerJumpPower = 1250;
public float moveX;
public float playerYSize = 2;
public LayerMask mainGround;
public float playerFallSpeed = 5;
void Awake(){
}
// Update is called once per frame
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(10, 0));
if(hit.distance < 0.7f){
print("hi");
}
Vector3 characterTargetPosition = new Vector3(transform.position.x + playerSpeed, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, characterTargetPosition, playerSpeed * Time.deltaTime);
if(Input.GetKeyDown("space")){
// float playerTargetPosY = transform.position.y + playerJumpPower;
// Vector3 characterTargetPosition = new Vector3(transform.position.x, playerTargetPosY, transform.position.z);
// transform.position = Vector3.Lerp(transform.position, characterTargetPosition, playerJumpPower * Time.deltaTime);
gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
}
//PlayerMove();
}
I have a rigidBody2D on my player so right now he just falls through the ground but the jump does work. If there is any easy way to do this. Like some script, a tutorial, or website I'm open for it. Please help.
Do you have a Rigidbody2D in your player? Things that will move usually have to have a RigidBody
(sorry for posting this as an answer. Cant comment yet)
EDIT:
try this:
Rigidbody2D rb;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
//Physics usually are done in FixedUpdate to be more constant
public void FixedUpdate(){
if (Input.GetKeyDown("space"))
{
if(!rb.simulated)
//player can fall
rb.simulated = true;
rb.AddForce(Vector2.up * playerJumpPower);
}
else
{
//third argument is the distance from the center of the object where it will collide
//therefore you want the distance from the center to the bottom of the sprite
//which is half of the player height if the center is actually in the center of the sprite
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, playerYSize / 2);
if (hit.collider)
{
//make player stop falling
rb.simulated = false;
}
}
}
If the player is the only thing that will collide with something you can just take out the colliders from the object that the player will not collide with.
Else you can check for the layer of the collided object with hit.collider.gameObject.layer and decide if the player will collide with that layer or not
(note that you have to compare with the index of the layer. If you want to get the index by its name you can use LayerMask.NameToLayer(/*layer name*/))
you will have to do rb.simulated = true everytime you want to do something with the RigidBody (like AddForce())
hope it helped :)
First of all I'm new to Unity and Game programming. I'm trying to add a cricket bowling like animation. But when the ball touches the ground It doesn't bounce at all and it shows an weird rolling animation.
Here is the GIF,
So I just added the velocity in code,
public class Ball : MonoBehaviour {
public float EndPointX ;
public float EndPointY ;
bool ForceAdded = true;
private Rigidbody2D rigidBody;
void Start () {
rigidBody = GetComponent<Rigidbody2D> ();
}
void Update () {
rigidBody.velocity = new Vector3(EndPointX, EndPointY, 0)*2;
}
}
My Bounce 2d material file,
Ball Properties,
It bounces perfectly without any velocity. I mean when it falls in a straight angle.
Thanx For The Help!!
Since Update() runs every frame, you are continuously setting the velocity, and immediately overwriting the bounce materials attempts to change its direction of movement. If you move the velocity to your Start() method, the velocity will only be set once and the bounciness will be able to influence your object properly.
void Start () {
rigidBody = GetComponent<Rigidbody2D> ();
rigidBody.velocity = new Vector3(EndPointX, EndPointY, 0)*2;
}
I have a blockbreaker type game coded using C# that mainly works fine but upon play testing i have found the ball will slow right down in certain situations! i.e if it wedges between to game object bricks the force of the ball rapidly slows down! 8 times out of ten this will not happen but other times it does and im unsure why! i will post the Ball script i think you will need to help solve this but should you need anymore information then please ask.
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
void Start () {
paddle = GameObject.FindObjectOfType<Paddle> ();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if (!hasStarted) {
//lock ball relative to the paddle
this.transform.position = paddle.transform.position + paddleToBallVector;
//wait for mouse press to start
if (Input.GetMouseButtonDown (0)) {
//if (Input.GetTouch(0).phase == TouchPhase.Ended){
hasStarted = true;
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D collision){
Vector2 tweak = new Vector2 (Random.Range(0f,0.2f),Random.Range(0f,0.2f));
if (hasStarted) {
GetComponent<AudioSource> ().Play ();
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
You are adding directly to the velocity of the ball. The velocity variable defines a direction and speed, not just a speed as you are thinking here.
So, when the ball collides with a block it has a velocity of (+x, +y). After the collision and bounce is has a velocity of (+x, -y). So by adding a random positive value to the velocity when it has the -y velocity means that it will slow down the ball.
This does not happen every time because you are moving the ball in your Update() method. Change that to 'FixedUpdated()'. Fixed Update handles all physics calculations.
Also, I would recommend having a RigidBody2D on your Ball object. Moving physics objects should always have RigidBodies. With this, you can then use AddForce in the forward direction of your ball and that should solve your problem.
EDIT: I see you have a RigidBody2D already. I missed that the first time. Instead of having GetComponent<RigidBody2D>().velocity try GetComponent<RigidBody2D>().AddForce( transform.forward * impulseAmount, ForceMode.Impluse );
I have a question about making a particle for jumping, like a dust cloud when the player jumps, here is my player script:
public class PlayerMovement : MonoBehaviour {
public float speed = 5f;
public Transform groundCheck;
public LayerMask groundLayer;
bool grounded = false;
Animator anim;
Rigidbody2D rgbd;
void Start () {
anim = GetComponent<Animator> ();
rgbd = GetComponent<Rigidbody2D> ();
}
void Update () {
}
void FixedUpdate (){
grounded = Physics2D.OverlapCircle (groundCheck.position, 0.2f, groundLayer);
float movex = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (movex * speed, rigidbody2D.velocity.y);
if (movex > 0){
transform.localScale = new Vector2(1,transform.localScale.y);
} else if (movex < 0){
transform.localScale = new Vector2(-1,transform.localScale.y);
}
if (Input.GetKey (KeyCode.UpArrow)){
if (grounded == true){
rgbd.AddForce (new Vector2(0f, 4f),ForceMode2D.Impulse);
} else {
grounded = false;
}
}
anim.SetFloat ("speed", Mathf.Abs (movex));
anim.SetBool ("grounded", grounded);
}
}
I want him to activate the particle system only once while in midair. I've tried a few things but when the player is in the air the particle system never stopped.
What most people do is create a new copy of the particle system on every need and destroy it later.
So what you would need is a brand new particle system. Expand the Emission tab. In there set the Rate to 0 (0 particles per second). Below Rate there should be an empty list called Bursts. Add one burst. Set Time to 0.0 (should be set by default) and number of particles to whatever you need. That will shoot 1 burst of particles whenever the particle system runs. Note that if Looping is ON than the burst will happen on beginning of every loop.
So far so good. Now make a prefab from it (watch a tutorial if you need). Then, in your code declare a Game Object variable that will serve you as a particle system:
public GameObject jumpParticles;
back to Unity, feed your prefab into the Jump Particles slot in inspector. Now it's all ready to be copied and pasted wherever you need it. So create a method for this:
void SpawnJumpParticles(Vector3 pos){
GameObject tmpParticles = (GameObject)Instantiate(jumpParticles, pos, Quaternion.identity); //look up how to use Instantiate, you'll need it a lot
Destroy(tmpParticles, 3f);
}
this code will spawn particles and auto-destory them in 3 seconds. The pos argument in the function is where the particles will get created. All that's left is to call it from your code where you start the jump. I'll leave that to you :)) good luck.