- Home /
Can you pls help me fix this rigidbody collision problem
I am trying to make a cricket game. I want a projection of the ball trajectory on the x-z plane when the ball hits the bat and I'm trying to do this by the code given below :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallTrajectory : MonoBehaviour
{
Vector3 velocitVectorAtCollision;
Vector3 velocityVectorAlongXZ;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Bat")
{
ShowTrajectoryInXZPlane();
}
}
private void ShowTrajectoryInXZPlane()
{
//show trajectory on xz plane on collision with bat
velocitVectorAtCollision = GetComponent<Rigidbody>().velocity;
velocityVectorAlongXZ = new Vector3(velocitVectorAtCollision.x, 0, velocitVectorAtCollision.z);
//velocityVectorAlongXZ = Vector3.ProjectOnPlane(velocityVectorAlongXZ, Vector3.up);
Debug.DrawRay(transform.position, velocityVectorAlongXZ * 50f , Color.black, 8f);
}
}
but it is not giving the correct trajectory along the x-z plane. It gives some random line, sometimes it gives a line near to the actual trajectory but never the correct one. Can u pls help me figure out what's the actual problem going on. And I am a very beginner so pls ignore I if I've done anything(or everything) wrong. If you need anything else, pls ask for it. This problem is really frustrating.
I'm looking at it and I do have a couple questions, 1: Is this an parabola/ arc kind of trajectory, or just a straight line?
2: Is this 2d or 3d?
3: not really related, but any particular reason you call it "velocitVectorAtCollision" instead of "velocityVectorAtCollision"? I'm just mildly curious on why velocity is missing its "y" (velocitY)
Your answer
![](https://koobas.hobune.stream/wayback/20220613033114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I determine force and direction by holding mouse? 0 Answers
Pick Up Rigid Body 0 Answers
Rigid Body Velocity max 1 Answer
Physics goes crazy when rigidbody collides with moving character controller 0 Answers
Stopping gameobject with colliders 1 Answer