- Home /
unity bounce without physics
Alright so I'm just completely stuck on how to do this I am using velocity to make my projectile rotate and move forward towards the player which is all working fine what I am stuck with is having a mechanic In my game that the player puts up a barrier to then bounce the projectile of it and give it a new force in direction depending on where it hit this barrier (pong) any help would be super amazing been on this one for few days now. so this is my basic bullet rotating towards player code since I'm sure my paragraph explained it terribly.
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
public float speed = 2.0f;
public float MoveSpeed = 30.0f;
void Start ()
{
target = GameObject.FindWithTag("Player");
}
void Update()
{
targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
targetRotation = Quaternion.LookRotation (targetPoint, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
transform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
In case you decide to use physics after all: Use FixedUpdate and either AddForce or change in velocity to achieve this. Rotation would be torque, but I think this would still be fine in your case.
OnCollisionEnter gives you relevant information when colliding. If you pick any of the returned contacts and raycast against it you'll receive a surface normal at that point. Using Vector3.Reflect would mirror reflect any vector, like the velocity, which could be the new direction. Or you just give the projectile a bouncy material, might result in an appropriate reflection too.
I answered similarly. Then I reread the title.
Answer by unit_nick · Sep 14, 2017 at 08:05 AM
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
public float speed = 2.0f;
public float MoveSpeed = 30.0f;
// added a barrier radius x. assumes barrier always x in front of target
public float barrierRadius = 1f;
void Start()
{
target = GameObject.FindWithTag("Player");
}
void Update()
{
targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
// calculate movement for easy access
Vector3 movement = transform.forward * MoveSpeed * Time.deltaTime;
// get distance to barrier
float distanceToBarrier = Vector3.Distance(transform.position, targetPoint) - barrierRadius;
// if movement will encroach barrier radius then...
if (movement.magnitude > distanceToBarrier)
{
// move the projectile to the barrier
transform.position += movement.normalized * distanceToBarrier;
// subtract the distance the projectile travelled to the barrier
movement -= movement.normalized * distanceToBarrier;
// then ricochet
// for this example i will simply reflect the projectile straight back
movement *= -1f;
transform.rotation = Quaternion.Inverse(transform.rotation);
// we can then leave this if clause and finish the movement
}
// move the transform
transform.position += movement;
}