I am creating a game where a bullet is a cylinder and the player is a cube (for now).
Whenever I create a new bullet like this:
Instantiate(Bullet, transform.position, transform.rotation);
The “player”(cube) that shoots it recoils backwards.
I need to turn off the recoil when SHOOTING a bullet only. I would like to keep the physics that when a bullet hits another player (or cube) it topples it over. What could be done please? Thanks?
EDIT:
@Lo0NuhtiK
I had already tried this code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float PlayerSpeed;
public float PlayerRotationSpeed;
public GameObject BulletPrefab;
// Use this for initialization, not needed yet
void Start ()
{
}
// Update is called once per frame
void Update ()
{
// Converts Y axis (Vertical) into Z.
// Move the player along with the X and Z axis multiplied the playerspeed and real time (instead of fps).
float xAxis = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
float zAxis = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;
transform.Translate(xAxis, 0, zAxis);
// Create a plane that transmits upwards on the transform's position.
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Generate a ray from the cursor position
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
// This will be the point that the object must look towards to be looking at the mouse.
// Transmitting to a Plane object only gives us a distance, so we'll have to take the distance,
// then find the point along that ray that meets that distance. This will be the point
// to look at.
float hitDistance = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, out hitDistance))
{
// Get the point along the ray that hits the calculated distance.
Vector3 targetPoint = ray.GetPoint(hitDistance);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, PlayerRotationSpeed * Time.deltaTime);
}
// If statement which takes left-click mouse input to create an instance of the BulletPrefab (clone).
if (Input.GetMouseButtonDown(0))
{
shootBullet();
}
}
void shootBullet()
{
Transform bullet = Instantiate(BulletPrefab, transform.position, transform.rotation) as Transform;
Physics.IgnoreCollision(bullet.collider, collider); //Ignore collision from bullet to shooter.
}
}
I just get this error:
NullReferenceException: Object
reference not set to an instance of an
object
The bullet actually shoots fine and everything, it just gives me that error.