- Home /
Speed after Vector3.Reflect Decreases,Speed of ball after Vector3.Reflect decreases
I have a Ball and A Surfboard. When the ball collides with the Surfboard, the Speed of the ball decreases. The decrease in speed is evident by seeing it in game mode.
I use rigidbody velocity in start() multiplied by the speed of ball; I change the rigidbody velocity in another function in the script attached to ball after it is called from the script of Surfboard after collision.
Here's the code:
Surfboard script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SurfBoard : MonoBehaviour
{
[SerializeField]
private float _speed = 3.5f;
private SurfBall _surfBall;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0,0,0);
_surfBall = GameObject.Find("Surf Ball").GetComponent<SurfBall>();
if(_surfBall == null){
Debug.LogError("The Surf Ball in NULL");
}
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(0,1,0) * horizontalInput * _speed * Time.deltaTime);
transform.Translate(new Vector3(1,0,0) * verticalInput * _speed * Time.deltaTime);
}
private void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Surf Ball"){
Debug.Log("collision Surf Ball");
Vector3 SurfBoardNormal = other.GetContact(0).normal;
_surfBall.BallSpeed(SurfBoardNormal);
}
}
}
Ball script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SurfBall : MonoBehaviour
{
[SerializeField]
private float _ballSpeed = 1f;
[SerializeField]
private Rigidbody _rb;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(3,4,0);
_rb = GetComponent<Rigidbody>();
_rb.velocity = new Vector3(-1,-1,0) * _ballSpeed;
}
// Update is called once per frame
void Update()
{
}
public void BallSpeed(Vector3 Normal)
{
_rb.velocity = Vector3.Reflect(_rb.velocity,Normal);
}
}
What Should I Do?
,
Comment