Post by Legion on May 6, 2017 13:41:12 GMT
We were looking for how to make a bullet bounce on the walls, this is what we found on the internet:
And here our attempt to convert it to C#
The JS seems to work fine, just need to randomize the effect.
BUT, C# damages the player when shooting in a straight line, even to the air, with no obstacles in front, maybe because of having to activate the collider in the bullet gameobject? (Although in JS doesn't happen)
var velocity : Vector3; //Our movement velocity
var speed : float; //Speed of object
function Awake()
{
velocity = this.transform.forward; //Placeholder default velocity // Change to whatever you need
speed = 20;
}
function Update()
{
this.transform.position += velocity * Time.deltaTime * speed; //Update our position based on our new-found velocity
}
function OnCollisionEnter( info : Collision ) //When we run into something
{
if (info.collider.tag == "Dirt")
{
//'Bounce' off surface
for( var contact : ContactPoint in info.contacts ) //Find collision point
{
//Find the BOUNCE of the object
velocity = Quaternion.AngleAxis(180, contact.normal) * transform.forward * -1;
}
}
}
And here our attempt to convert it to C#
using UnityEngine;
using System.Collections;
public class Ricochet : MonoBehaviour
{
public float speed;
Vector3 velocity;
void Awake ()
{
velocity = this.transform.forward;
}
void Update ()
{
this.transform.position += velocity * Time.deltaTime * speed;
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Dirt") {
foreach( ContactPoint contact in collision.contacts )
{
velocity = Quaternion.AngleAxis(180, contact.normal) * transform.forward * -1;
}
}
}
}
The JS seems to work fine, just need to randomize the effect.
BUT, C# damages the player when shooting in a straight line, even to the air, with no obstacles in front, maybe because of having to activate the collider in the bullet gameobject? (Although in JS doesn't happen)