When I instantiate a bullet decal texture to the hit point of a raycast it goes sideways for some reason, no online resources have proved to be any help because it works for them.
The texture for the decal is on a plane and it is laying flat above the ground (height at which raycast hit.)
Here's the raycast shooting code:
var TheDammage = 100;
var BulletHole : Transform;
var canShoot : boolean;
var NoShootZoneTrigger : Transform;
//var AmmoText : GUIText;
var Ammo : float;
var MaxDistanceInMetersMax35 = 25;
var GunFirePistol : AudioClip;
var DryGunFirePistol : AudioClip;
function Start() {
canShoot=true;
}
function Update () {
if (MaxDistanceInMetersMax35>25){
MaxDistanceInMetersMax35 = 35;
}
var hit : RaycastHit;
var ray : Ray =
Camera.main.ScreenPointToRay (Vector3(Screen.width*0.5, Screen.height*0.5,0));
//AmmoText.text = "Bullets Remaining: "+Ammo;
if (Input.GetMouseButtonDown(0)&& canShoot==true){
if (Physics.Raycast(ray, hit, 100)){
if (Ammo >0){
Ammo--;
audio.PlayOneShot(GunFirePistol);
}
{
Debug.Log(hit.distance);
if (hit.distance<MaxDistanceInMetersMax35){
var particleClone = Instantiate(Effect, hit.point,
Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 15);
hit.transform.SendMessage("ApplyDammage",
TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
if (Input.GetMouseButtonDown(0)&& canShoot==false){
Debug.Log("No Shooting Zone!");
}
}
if (Input.GetKeyDown("w")){
canShoot = false;
}
if (Input.GetKeyUp("w")){
//animation.Play("GunWalkDone");
canShoot=true;
}
}
if (Ammo<1){
canShoot=false;
if (Input.GetMouseButtonDown(0)&& canShoot==false){
//AmmoText.text="";
audio.PlayOneShot(DryGunFirePistol);
}
}
if (Ammo<18) {
if (Input.GetKeyDown("r")){
animation.Play("Reload");
}
}
}
Related
This code shoots at the mouse pointer but goes directly, not by projection. I need it to be like the picture below.
var prefab : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
var go = Instantiate(prefab, transform.position, q);
go.rigidbody2D.AddForce(go.transform.up * 20000.0);
}
}
I created car game. I have 3 checkpoint. Car refresh when I press R key
but car wrong position.
My Code;
Checkpoint.js;
#pragma strict
var SpawnPoint : Transform;
function OnTriggerEnter(col: Collider)
{
if(col.tag =="Player")
{
SpawnPoint.position = Vector3(transform.position.x, transform.position.y, transform.position.z);
}
}
ReSpawn.js;
#pragma strict
var SpawnPoint : Transform;
var player : GameObject;
var target : Transform;
function Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
player.transform.position = SpawnPoint.position;
}
}
How to force correct position?
you should use getcomponent to get a var in another script assign your checkpoint script to var script via inspector.
#pragma strict
var script : Checkpoint;
var player : GameObject;
var target : Transform;
function Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
player.transform.position = script.GetComponent(Checkpoint).SpawnPoint.position;
}
}
I have a player who can carry objects with its gun in a "2.5D Platform Shooter" game but the problem I'm experiencing at the moment is that every time I make the player hold the object(it has a rigid body) and pressed against the wall, the object goes through the wall. Please help unity fans.
This the code I'm using below to carry the object.
#pragma strict
var catchRange = 30.0;
var holdDistance = 4.0;
var minForce = 1000;
var maxForce = 10000;
var forceChargePerSec = 3000;
var layerMask : LayerMask = -1;
#HideInInspector
var anim : Animator;
enum GravityGunState { Free, Catch, Occupied, Charge, Release};
private var gravityGunState : GravityGunState = 0;
private var rigid : Rigidbody = null;
private var currentForce = minForce;
function FixedUpdate()
{
if(gravityGunState == GravityGunState.Free)
{
if(Input.GetButton("Fire1"))
{ anim.SetBool("Shoot", true);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask))
{
if(hit.rigidbody)
{
rigid = hit.rigidbody;
gravityGunState = GravityGunState.Catch;
}
}
}else if(!hit.rigidbody){anim.SetBool("Shoot", false);}
}
else if(gravityGunState == GravityGunState.Catch)
{
rigid.useGravity = false;
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(!Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Occupied;
}
else if(gravityGunState == GravityGunState.Occupied)
{
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)){
Debug.DrawLine (transform.position, hit.point, Color.red);
}
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Charge;
}
else if(gravityGunState == GravityGunState.Charge)
{
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(currentForce < maxForce)
{
currentForce += forceChargePerSec * Time.deltaTime;
}
else
{
currentForce = maxForce;
}
if(!Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Release;
rigid.useGravity = true;
}
else if(gravityGunState == GravityGunState.Release)
{
rigid.AddForce(transform.forward * currentForce);
currentForce = minForce;
gravityGunState = GravityGunState.Free;
}
}
#script ExecuteInEditMode()
adding collider to gameObjects will prevent object from moving through each other and if it already has a collider and it is passing through check your settings as follows
Rigidbody Is Kinamatic : False, Is Triggger:false
set the Collision Detection of rigidbody to Continuous or Continuous Dynamic
sometimes for fast moving game objects the collision wont be detected in these sceneraios you can use Raycasting for collisin detection here is DontGoThrough Script that use raycasting for collision detection
using UnityEngine;
using System.Collections;
public class DontGoThroughThings : MonoBehaviour
{
public LayerMask layerMask; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private Rigidbody myRigidbody;
//initialize values
void Awake()
{
myRigidbody = rigidbody;
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = myRigidbody.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
}
previousPosition = myRigidbody.position;
}
}
I'm trying to spawn a prefab after I destroy the object and only when the object is destroyed.
#pragma strict
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "destroyable") {
Destroy(hit.collider.gameObject);
}
}
}
}
You could put the Prefab in a variable to set on unity editor:
var objectToSpawn : GameObject;
Before the Spawn code you can use:
yield WaitForSeconds (5);
Your code would look something like this:
Javascript
#pragma strict
var objectToSpawn : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "destroyable") {
var oldTransform = hit.collider.gameObject.transform;
Destroy(hit.collider.gameObject);
StartCoroutine(SpawnAfter5Seconds(oldTransform));
}
}
}
}
function SpawnAfter5Seconds(oldTransform:Transform)
{
yield WaitForSeconds (5);
var newObject = Instantiate (objectToSpawn , oldTransform.position, oldTransform.rotation);
}
This is the code for my player's movement:
var animator : Animator;
function Start () {
animator = GetComponent("Animator");
}
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var jumpheight : int = 2;
var speed : int = 2;
var isGrounded : int = 0;
// Checks if player is in air or ground
var Jumptest : int = 0;
// Checks if player has jumped
function Update () {
animator.SetInteger("Direction", 4);
if (Input.GetKey(moveUp) && isGrounded == 0)
{
rigidbody2D.velocity.y = jumpheight;
isGrounded = 1;
animator.SetInteger("Direction", 2);
}
else if (Input.GetKey(moveLeft))
{
transform.position += Vector3.left * speed * Time.deltaTime;
animator.SetInteger("Direction", 3);
}
else if (Input.GetKey(moveRight))
{
transform.position += Vector3.right * speed * Time.deltaTime;
animator.SetInteger("Direction", 1);
}
}
and this is the code for the projectiles movement:
#pragma strict
var speed = 8.0;
var time = 0.0;
function Start () {
}
var moveRight : KeyCode;
var moveLeft : KeyCode;
function Update () {
time += Time.deltaTime;
if(time > 3){
Destroy(gameObject);
}
transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
}
At the moment, the projectiles shoot to the right. I want to be able to shoot them in the direction the player is facing.
I tried if key down a and if key down d but then the projectiles stop when the player is not moving and move relative to the player (if I shoot one going left, then move my player to the right, the projectile reverses direction).
Anybody know?
When checking for player movement , whether the left/right key is down, you create a simple integer which keeps track of direction
...
var isRight: int=1 ;//assuming player starts with facing in the right direction
...
and then, when checking for key press set the value of this variable appropriately
...
else if (Input.GetKey(moveLeft)&&!Input.GetKey(moveRight))
{
isRight = -1;
...
}
else if (Input.GetKey(moveRight)&&!Input.GetKey(moveLeft))
{
isRight = 1;
...
}
Finally when setting translation for the projectile simply multiply the speed with this new variable
transform.Translate(Vector3.right * speed * isRight * Time.deltaTime, Space.Self);