I just finished a BreakOut style game but there is a bug where sometimes the ball gets stuck to the edges of the map with no direction or speed as shown in the screenshot
What I see is that it happens when the ball completely loses trajectory or speed, but could not solve the error
enter image description here
my code
public class Ball : MonoBehaviour
{
[SerializeField] Rigidbody2D rigidbody2D;
Vector2 moveDirection;
Vector2 currentVelocity;
float velocity=10;
//GameManager gameManager;
Transform paddle;
[SerializeField] AudioController audioController;
[SerializeField] AudioClip bounceSfx;
[SerializeField] AudioClip dieSfx;
public bool superBall;
[SerializeField] float superBallTime=10;
[SerializeField]float yMinSpeed = 10;
[SerializeField]TrailRenderer trailRenderer;
public bool SuperBall
{
get=> superBall;
set{
superBall=value;
if(superBall)
StartCoroutine(ResetSuperBall());
}
}
// Start is called before the first frame update
void Start()
{
//rigidbody2D = GetComponent<Rigidbody2D>();
//rigidbody2D.velocity = Vector2.up*velocity*Time.deltaTime;
GameManager.Instance = FindObjectOfType<GameManager>();
paddle = transform.parent;
}
// Update is called once per frame
void Update()
{
currentVelocity = rigidbody2D.velocity;
if (Mathf.Abs(currentVelocity.y) < 3 && Mathf.Abs(currentVelocity.y) < 3 && GameManager.Instance.ballOnGame)
{
velocity = 10;
rigidbody2D.velocity = Vector2.up * velocity ;
}
if (Mathf.Abs(currentVelocity.y) + Mathf.Abs(currentVelocity.y) < 10 && GameManager.Instance.ballOnGame)
{
velocity = 10;
rigidbody2D.velocity = Vector2.up * velocity ;
}
if (velocity <10 && GameManager.Instance.ballOnGame)
{
velocity = 10;
rigidbody2D.velocity = Vector2.up * velocity ;
}
if ((Input.GetKey(KeyCode.W) && GameManager.Instance.ballOnGame == false)||(Input.GetKey(KeyCode.Space) && GameManager.Instance.ballOnGame == false))
{
rigidbody2D.velocity = Vector2.up * velocity ;
transform.parent = null;
GameManager.Instance.ballOnGame = true;
rigidbody2D.isKinematic = false;
rigidbody2D.AddForce(new Vector3(velocity, velocity, 0));
if (!GameManager.Instance.GameStarted)
{
GameManager.Instance.GameStarted = true;
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.CompareTag("Brick") && superBall)
{
rigidbody2D.velocity = currentVelocity;
return;
}
moveDirection=Vector2.Reflect(currentVelocity,collision.GetContact(0).normal);
if (Mathf.Abs(moveDirection.y) < yMinSpeed)
{
//permitir velocidad minima
moveDirection.y = yMinSpeed*Mathf.Sign(moveDirection.y);
}
rigidbody2D.velocity=moveDirection;
audioController.PlaySfx(bounceSfx);
if (collision.transform.CompareTag("BottomLimit"))
{
if(GameManager.Instance != null)
{
GameManager.Instance.PlayerLives--;
audioController.PlayDie(dieSfx);
if (GameManager.Instance.PlayerLives > 0)
{
rigidbody2D.velocity = Vector2.zero;
transform.SetParent(paddle);
transform.localPosition = new Vector2(0, 0.65f);
GameManager.Instance.ballOnGame = false;
}
}
}
}
IEnumerator ResetSuperBall()
{
trailRenderer.enabled = true;
yield return new WaitForSeconds(superBallTime);
trailRenderer.enabled = false;
GameManager.Instance.powerIsActive = false;
superBall = false;
}
}
This is a common issue with Rigidbodies at low velocities. The physics engine implements a Physics2D.velocityThreshold that is designed to dampen slow bounces and calm down a pile of Rigidbodies.
The default value for velocityThreshold is 1, meaning that the x or y component of the velocity that is slower will be floored to 0.
To resolve this issue you can simply lower the value to 0.001. You can do it in the Physcs2d tab located at Edit->ProjectSettings->Physics2d
Related
I was following a tutorial but I'm having trouble with the CheckCollisionOverlap() function. It's meant to check for a collision over the player's head when the player is crouching, but for some reason he won't stand back up (or stop walking in place).
If I comment out PhysicsUpdate() and CheckCollisionOverlap(), he will crouch, walk crouch, and stand up from crouch, so I know the problem has to exist within that logic.
In the tutorial comments section, someone mentioned that they changed the value of the layermask. I named a layermask Player and assigned it to layer 8 but that still didn't do anything.
Tutorial Link https://www.youtube.com/watch?v=QVxvwRAYeHU
using UnityEngine;
public class CrouchingState : State
{
float playerSpeed;
bool belowCeiling;
bool crouchHeld;
bool grounded;
float gravityValue;
Vector3 currentVelocity;
public CrouchingState(Character _character, StateMachine _stateMachine):base(_character, _stateMachine)
{
character = _character;
stateMachine = _stateMachine;
}
public override void Enter()
{
base.Enter();
character.animator.SetTrigger("crouch");
belowCeiling = false;
crouchHeld = false;
gravityVelocity.y = 0;
playerSpeed = character.crouchSpeed;
character.controller.height = character.crouchColliderHeight;
character.controller.center = new Vector3(0f, character.crouchColliderHeight / 2f, 0f);
grounded = character.controller.isGrounded;
gravityValue = character.gravityValue;
}
public override void Exit()
{
base.Exit();
character.controller.height = character.normalColliderHeight;
character.controller.center = new Vector3(0f, character.normalColliderHeight / 2f, 0f);
gravityVelocity.y = 0f;
character.playerVelocity = new Vector3(input.x, 0, input.y);
character.animator.SetTrigger("move");
}
public override void HandleInput()
{
base.HandleInput();
if (crouchAction.triggered && !belowCeiling)
{
crouchHeld = true;
}
input = moveAction.ReadValue<Vector2>();
velocity = new Vector3(input.x, 0, input.y);
velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
velocity.y = 0f;
}
public override void LogicUpdate()
{
base.LogicUpdate();
character.animator.SetFloat("speed", input.magnitude, character.speedDampTime, Time.deltaTime);
if (crouchHeld)
{
stateMachine.ChangeState(character.standing);
}
}
public override void PhysicsUpdate()
{
base.PhysicsUpdate();
belowCeiling = CheckCollisionOverlap(character.transform.position + Vector3.up * character.normalColliderHeight);
gravityVelocity.y += gravityValue * Time.deltaTime;
grounded = character.controller.isGrounded;
if (grounded && gravityVelocity.y < 0)
{
gravityVelocity.y = 0f;
}
currentVelocity = Vector3.Lerp(currentVelocity, velocity, character.velocityDampTime);
character.controller.Move(currentVelocity * Time.deltaTime * playerSpeed + gravityVelocity * Time.deltaTime);
if (velocity.magnitude > 0)
{
character.transform.rotation = Quaternion.Slerp(character.transform.rotation, Quaternion.LookRotation(velocity), character.rotationDampTime);
}
}
public bool CheckCollisionOverlap(Vector3 targetPositon)
{
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit hit;
Vector3 direction = targetPositon - character.transform.position;
if (Physics.Raycast(character.transform.position, direction, out hit, character.normalColliderHeight, layerMask))
{
Debug.DrawRay(character.transform.position, direction * hit.distance, Color.yellow);
return true;
}
else
{
Debug.DrawRay(character.transform.position, direction * character.normalColliderHeight, Color.white);
return false;
}
}
}
I had to change all the layers of the children in the player gameobject as well. Not really sure why that worked, but its responding.
I've been asking so many Unity questions in such little time and I really appreciate all those who answer. I don't know why I always face such issues for which any of the answers I see on the net aren't able to help me out. Okay so here's my problem:
if (Input.GetKey(KeyCode.Q) && (rightHit.collider != null || leftHit.collider != null) && groundHit.collider == null)
This line of code here in the Update function checks every frame if I'm holding down the Q button on any wall. If the condition is satisfied, then
void Grapple()
{
if (wallGrapple)
{
x = 0f;
rb.gravityScale = 0;
rb.velocity = new Vector2(speed * x, speed * y);
if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
}
}
this function is called which allows the player to stick and slide on the wall based on Vertical axis input(which feeds the y variable). Now the problem is that if I only press Q once on a wall then it stays on the wall even though its supposed to be held down for Grapple() to be called [I've used Input.GetKey instead of Input.GetKeyDown] and I am also able to slide above the wall (like there's some sorta invisible wall) even though in the if line, the condition for detecting a wall is being checked.
I've been at it for around 30-45 minutes and I'm unable to crack it. Another workaround I thought of implementing was using a coroutine but I don't know how to use as I haven't found any basic explanation of how it works and how its supposed to be used. Here's the full code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;
public class Move : MonoBehaviour
{
[SerializeField] private LayerMask groundMask;
[SerializeField] private LayerMask Wall;
[Header("Horizontal")]
public float x;
float dirX;
public float speed;
float initialSpeed;
[SerializeField] [Range(0, 1)] float LerpConstant;
public float dashForce = 100f;
public bool canDash;
public bool dashRequest;
[Header("Vertical")]
public float y;
public bool canJump;
public bool jumpRequest;
public float initialJumpForce;
public float jumpForce;
public bool canWallJump;
public float modifiedJumpForce;
public float sideJumpForce;
public bool wallGrapple;
[Header("Other")]
public Rigidbody2D rb;
public float gScale = 10f;
BoxCollider2D boxcollider;
RaycastHit2D groundHit;
RaycastHit2D leftHit;
RaycastHit2D rightHit;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
boxcollider = gameObject.GetComponent<BoxCollider2D>();
jumpForce = initialJumpForce;
initialSpeed = speed;
rb.gravityScale = gScale;
}
// Update is called once per frame
void Update()
{
//Instant 1, 0, -1 inputs
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
if(x < 0)
{ dirX = -1; }
else if(x > 0)
{ dirX = 1; }
if (Input.GetKeyDown(KeyCode.Space) && (canJump || canWallJump)) //for jumps
{ jumpRequest = true; }
if (Input.GetKey(KeyCode.Q) && (rightHit.collider != null || leftHit.collider != null) && groundHit.collider == null) //for wall grappling
{ wallGrapple = true; }
if (Input.GetKeyDown(KeyCode.LeftShift) && (canDash)) //for dashing
{ dashRequest = true; }
Detection();
JumpDash();
Grapple();
}
void FixedUpdate()
{
Movement();
}
void Detection()
{
groundHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.down, 0.1f, groundMask);
if (groundHit.collider != null) //If player is on ground
{
canJump = true;
canWallJump = false;
canDash = false;
jumpForce = initialJumpForce;
sideJumpForce = 0;
LerpConstant = 0.25f;
}
else if (groundHit.collider == null)//not on ground
{
LerpConstant = 0.12f;
canJump = false;
}
//Wall detection
//Left wall
leftHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.left, 0.1f, Wall);
if (leftHit.collider != null && groundHit.collider == null) //if player on left wall and not on ground
{
canWallJump = true;
jumpForce = modifiedJumpForce;
sideJumpForce = jumpForce;
}
//Right wall
rightHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.right, 0.1f, Wall);
if (rightHit.collider != null && groundHit.collider == null) //if player on right wall and not on ground
{
canWallJump = true;
jumpForce = modifiedJumpForce;
sideJumpForce = -jumpForce; // negative of jump force to jump in left direction
}
if (rightHit.collider == null && leftHit.collider == null) //if both walls are not detected
{
canWallJump = false;
}
}
void Movement()
{
Vector2 move = new Vector2(x * speed, rb.velocity.y);
rb.velocity = Vector2.Lerp(rb.velocity, move, LerpConstant);
}
void JumpDash()
{
//Jump
if (jumpRequest)
{
wallGrapple = false;
rb.gravityScale = gScale;
rb.AddForce(new Vector2(sideJumpForce * 2.1f, jumpForce), ForceMode2D.Impulse);
speed = initialSpeed;
jumpRequest = false; //setting jumpRequest to false after jumping to prevent unlimited jumps
canDash = true;
}
/*if (dashRequest)
{
rb.velocity = Vector2.zero;
rb.AddForce((dashForce, 0f), ForceMode2D.Impulse);
}*/ //remember to put this code in later
}
void Grapple()
{
if (wallGrapple)
{
x = 0f;
rb.gravityScale = 0;
rb.velocity = new Vector2(speed * x, speed * y);
if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
}
}
}
P.S. I originally wanted to use Space itself instead of Q for the stick and slide but more errors pop up like inconsistent wall jumping so that's why I used Q.
https://streamable.com/3v6qrq <- Video showing the problem.
It seems you haven't set
wallGrapple = false
anywhere in your code except in the jump request. Therefore, as it never gets set to false your player will continuously grapple.
Maybe you can set it to false in the Update function first and do the key detection.
I'm showing you a coroutine example code snippet.
You can understand it from the comment lines and apply it to your own code.
void Start()
{
StartCoroutine("coroutineFunc"); // calling for example
}
and coroutine func like this:
IEnumerator coroutineFunc()
{
// before waiting... if u wanna try something
yield return new WaitForSeconds(1); // waiting..
// after waiting something what do you want
}
Trying to run a raycast from my camera to Z = 0 that will hit objects on the TransparentFX layer and temporarily make them transparent as they are likely blocking the view of the player. But the raycast never hits anything.
Camera
https://imgur.com/lyTo8OZ
Tree
https://imgur.com/bgNiMWR
ClearSight.cs
[RequireComponent(typeof(Camera))]
public class ClearSight : MonoBehaviour
{
[SerializeField]
private LayerMask raycastLayers;
// Update is called once per frame
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, transform.TransformDirection(Vector3.forward), 10f, raycastLayers);
if(hits != null && hits.Length > 0)
{
Debug.Log("Found objects blocking player!");
foreach(RaycastHit2D hit in hits)
{
Debug.Log("Making " + hit.transform.gameObject.name + " transparent!");
AutoTransparent at = hit.transform.GetComponent<AutoTransparent>();
if(at == null)
{
at = hit.transform.gameObject.AddComponent<AutoTransparent>();
}
at.MakeTransparent();
}
}
}
}
AutoTransparent.cs
[RequireComponent(typeof(SpriteRenderer))]
public class AutoTransparent : MonoBehaviour
{
[SerializeField]
private SpriteRenderer[] renderTargets;
[SerializeField]
private float transparentRecoveryTime = 0.1f;
private bool isTransparent = false;
private float transparencyTimer = 0;
private void Update()
{
if (isTransparent)
{
UpdateTransparencyTimer();
}
}
private void UpdateTransparencyTimer()
{
transparencyTimer += Time.deltaTime / transparentRecoveryTime;
if(transparencyTimer >= 1)
{
MakeOpaque();
}
}
public void MakeTransparent()
{
transparencyTimer = 0;
if (!isTransparent)
{
isTransparent = true;
foreach (SpriteRenderer renderer in renderTargets)
{
Color c = renderer.color;
renderer.color = new Color(c.r, c.g, c.b, 0.3f);
}
}
}
public void MakeOpaque()
{
isTransparent = false;
foreach(SpriteRenderer renderer in renderTargets)
{
Color c = renderer.color;
renderer.color = new Color(c.r, c.g, c.b, 1);
}
}
}
Figured it out. I was using RaycastHit2D and Physics2D.RaycastAll, which uses Vector2 parameters so the Z forward variable was being taken out of the equation. Switched up to Box Collider and Physics.RaycastAll and it works great.
After I click play the camera stays as it should be, but when I move my character the camera zooms in for no reason
The properties of the camera do not change at all, zoom and everything stays the same. Tryed changing from orthographic to perspective no change, move z axis no change, change scale no change, change resolution and no change, making the camera not a parent and no change it behaves the same as parent and as child
before character walks
after character walks
I dont think that there is something to do with the code but here is the code attached to my character, the camera behaves the same as child and as parent
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public float speed = 5f;
public float jumpSpeed = 8f;
private float movementX = 0f;
private Rigidbody2D rigidBody;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask groundLayer;
public bool isTouchingGround;
public SpriteRenderer box;
private bool canSpawn = true;
private bool canAnimateWalk = true;
private bool canAnimateIdle = true;
private bool canAnimateJump = true;
private bool stopJump = true;
private int spawningSpeed = 1000;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isTouchingGround = Physics2D.OverlapBox(groundCheckPoint.position,new Vector2(0.9f,0.1f),0f, groundLayer);
movementX = Input.GetAxis("Horizontal");
if (movementX > 0f)
{
if(canAnimateWalk==true && isTouchingGround)
{
canAnimateWalk = false;
StartCoroutine(AnimateWalk());
}
GetComponent<SpriteRenderer>().transform.localScale = new Vector3(2, 2, 1);
rigidBody.velocity = new Vector2(movementX * speed, rigidBody.velocity.y);
}
else if (movementX < 0f)
{
if (canAnimateWalk == true && isTouchingGround)
{
canAnimateWalk = false;
StartCoroutine(AnimateWalk());
}
GetComponent<SpriteRenderer>().transform.localScale = new Vector3(-2, 2, 1);
rigidBody.velocity = new Vector2(movementX * speed, rigidBody.velocity.y);
}
else
{
if(isTouchingGround)
{
StopCoroutine(AnimateWalk());
if(canAnimateIdle==true)
{
canAnimateIdle = false;
StartCoroutine(AnimateIdle());
}
}
rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
}
if (Input.GetButtonDown("Jump") && isTouchingGround)
{
canAnimateJump = false;
rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
StartCoroutine(AnimateJump());
}
else if(!isTouchingGround)
{
StopCoroutine(AnimateWalk());
}
}
IEnumerator AnimateJump()
{
Debug.Log("Animating Jump");
int counter = 0;
while (counter < 10)
{
counter++;
GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/j" + counter);
yield return new WaitForSeconds(0.1f);
if(isTouchingGround==true)
{
break;
}
}
while(!isTouchingGround)
{
GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/j10");
yield return new WaitForSeconds(0.1f);
}
GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/i1");
canAnimateWalk = true;
canAnimateJump = true;
}
IEnumerator AnimateIdle()
{
int counter = 0;
while(Input.GetAxis("Horizontal")==0 && counter <10 && rigidBody.velocity.y==0)
{
counter++;
GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/i"+counter);
yield return new WaitForSeconds(0.2f);
}
canAnimateIdle = true;
}
IEnumerator AnimateWalk()
{
int counter = 0;
while (Input.GetAxis("Horizontal")!=0 && counter < 8 && rigidBody.velocity.y==0)
{
counter++;
GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("img/g" + counter);
yield return new WaitForSeconds(0.08f);
}
canAnimateWalk = true;
}
}
What could it be? I tried everything I think
GetComponent<SpriteRenderer>().transform.localScale = new Vector3(-2, 2, 1);
If your movement script is attached to your "guy" gameobject, then you are changing the (local) scale of it. All children will also scale accordingly.
Since your camera is a child of guy, it will scale and produce the result you see.
Try unparenting the Camera from your guy and create a seperate script that follows your guy and attach that to your Camera.
I solved my problem
The issue was in the character scaling. The camera did not change but the size of the character did making me believe that there was a zoom in.
My character x and y scale is 1 and 1 but I used 2 and 2 scale on move
The scale was used to rotate my character when it moves left and right
I was trying to achieve this kind of player ball movement:
Catch Up (Ketchapp)
From my side I have tried and record a video of my current implementation:
CatchUpBallMovementDemo
Two kinds of problem, I was facing:
ball making so much jerk while moving on the plain track that I hope you have clearly noticed in my recorded video
when ball reach left or right edge and you try to swipe its making jerk again rather than remain restricted because clamping related code already added
I have just created a demo project so here I am providing the link for it so personally you can check and provide me a suggestion for making ball movement perfect.
Demo Project Source Link: CatchUpBallDemo
Demo Project SIZE 20MB
What at present making jerk in ball movement that I can't able to decide, whether its following camera jerk, whether ball not moving properly though I have created a plain track for checking purpose.
Ball Inspector Detail:
Complete code added within the working demo project. Share your suggestions with me to solve this.
Code Scripts:
BallController
[RequireComponent (typeof(Rigidbody))]
public class BallController : MonoBehaviour
{
//
private Rigidbody myRigidBody;
private bool isJumper;
private bool allowSpeedIncrease;
private BallInputHandler ballInputHandler;
private float speed;
private float speedMilestone;
private float jumpCounter;
private float scoreElapsedTime;
[SerializeField]
private bool isGrounded;
//
public float ballHorzRange;
public float ballStartSpeed;
public float ballTopSpeed;
public float smoothnessValue;
public float smoothnessX;
private void Awake ()
{
DoOnAwake ();
}
private void DoOnAwake ()
{
ballInputHandler = GetComponent<BallInputHandler> ();
myRigidBody = GetComponent<Rigidbody> ();
speed = ballStartSpeed;
speedMilestone = ballStartSpeed;
}
public void Start ()
{
DoOnStart ();
}
private void DoOnStart ()
{
// assinging player transform to camera to follow
Camera.main.GetComponent<CameraFollow> ().FollowPlayer (transform);
}
void Update ()
{
// slowly increase ball moving speed
if (allowSpeedIncrease) {
speed += Time.deltaTime;
if (speed >= speedMilestone) {
allowSpeedIncrease = false;
speed = speedMilestone;
}
}
}
void FixedUpdate ()
{
// do jumping
if (isJumper) {
jumpCounter++;
if (jumpCounter >= 3) {
isJumper = false;
jumpCounter = 0;
}
myRigidBody.AddForce (Vector3.up * 700f);
}
// applying continuous forward velocity
Vector3 nextVelocity = myRigidBody.velocity;
nextVelocity.x = ballInputHandler.horizontalInput * smoothnessX;
nextVelocity.z = speed;
if (isGrounded) {
nextVelocity.y = 0;
} else if (!isJumper) {
nextVelocity.y -= speed * 0.1f;
}
myRigidBody.velocity = nextVelocity.normalized * speed;
ClampingBallMovement ();
}
// ball horizontal movement limitation
private void ClampingBallMovement ()
{
Vector3 currRigidbodyPos = myRigidBody.position;
if (currRigidbodyPos.x <= -ballHorzRange || currRigidbodyPos.x >= ballHorzRange) {
currRigidbodyPos.x = Mathf.Clamp (currRigidbodyPos.x, -ballHorzRange, ballHorzRange);
myRigidBody.position = currRigidbodyPos;
}
}
void OnTriggerEnter (Collider other)
{
if (other.CompareTag (GameConstants.TAG_TRACK_SPAWNER)) {
GameController.Instance.SpawnPlateform ();
} else if (other.CompareTag (GameConstants.TAG_TRACK_DESTROYER)) {
Destroy (other.transform.parent.gameObject);
}
}
}
BallMeshRolling
public class BallMeshRolling : MonoBehaviour
{
private Vector3 ballLastPosition;
void Start ()
{
ballLastPosition = transform.parent.position;
}
void Update ()
{
// implementation-1
float speed = Vector3.Distance (transform.parent.position, ballLastPosition) * 30f;
transform.RotateAround (transform.position, Vector3.right, speed);
// float dragDifference = (transform.position.x - ballLastPosition.x) * 30f;
// transform.RotateAround (transform.position, Vector3.forward, dragDifference);
ballLastPosition = transform.parent.position;
}
}
CameraFollow
public class CameraFollow : MonoBehaviour
{
//
private Vector3 newPos;
private Vector3 initialPosition;
//
public Transform player;
public Vector3 offSet;
void Awake ()
{
initialPosition = transform.position;
}
void LateUpdate ()
{
if (!player)
return;
newPos = player.position + offSet;
newPos.x = ReMap (newPos.x);
newPos.y = Mathf.Clamp (newPos.y, initialPosition.y, initialPosition.y + 1f);
// transform.position = newPos;
transform.position = Vector3.Lerp (transform.position, newPos, 10f * Time.deltaTime);
}
public void FollowPlayer (Transform target)
{
player = target;
ResetCamera ();
}
public float ReMap (float value, float from1 = -4f, float to1 = 4f, float from2 = -2.5f, float to2 = 2.5f)
{
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
public void ResetCamera ()
{
transform.position = initialPosition;
}
}
I could solve the second problem by simply adding this to BallController.ClampingBallMovement():
private void ClampingBallMovement ()
{
Vector3 currRigidbodyPos = myRigidBody.position;
if (currRigidbodyPos.x <= -ballHorzRange || currRigidbodyPos.x >= ballHorzRange) {
currRigidbodyPos.x = Mathf.Clamp (currRigidbodyPos.x, -ballHorzRange, ballHorzRange);
myRigidBody.position = currRigidbodyPos;
}
// I ADDED THIS
// Clamp the velocity as well
if (currRigidbodyPos.x <= -ballHorzRange && myRigidBody.velocity.x < 0 || currRigidbodyPos.x >= ballHorzRange && myRigidBody.velocity.x > 0)
{
myRigidBody.velocity = new Vector3(0, myRigidBody.velocity.y, myRigidBody.velocity.z);
}
}
you clamped the position but did not clamp the velocity as well.
I could not reproduce the first jerking arround on my PC.