Como hacer un efecto de lanzar granadas en mano con Unity.

 



Como hacer un efecto de lanzar granadas en mano con Unity.

He puesto como hijo del player el protagonista del juego un lanzador de proyectiles, el que idee que va girando y tocando otro objeto realizando a así los disparos...https://youtu.be/cTSwWrXgVow

….

Luego e puesto una nueva animación con el movimiento del bracear, lanzando la granada de mano y la e ampliado en el script de movimientos de mi personaje….

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CONTROLREPARTIDOR : MonoBehaviour {

public Animator Anim;
public float WalkSpeed;

void Update () {

if (Input.GetKey (KeyCode.E)) {
Anim.SetBool ("CORRER", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("CORRER", false);

if (Input.GetKey (KeyCode.W)) {
Anim.SetBool ("ANDA", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("ANDA", false);

if (Input.GetKey (KeyCode.S)) {
Anim.SetBool ("ATRAS", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("ATRAS", false);

if (Input.GetKey (KeyCode.A)) {

Anim.SetBool ("CORRER", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("CORRER", false);

if (Input.GetKey (KeyCode.P)) {

Anim.SetBool ("BOMBA", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("BOMBA", false);


if (Input.GetKey (KeyCode.O)) {

Anim.SetBool ("BOMBA", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("BOMBA", false);

if (Input.GetKey (KeyCode.D)) {

Anim.SetBool ("CORRER", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("CORRER", false);


if (Input.GetButton ("Fire1")) {

Anim.SetBool ("DISPARANDO", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("DISPARANDO", false);


if (Input.GetKey (KeyCode.M)) {
Anim.SetBool ("LANZAR", true);
transform.Translate (Vector3.forward * WalkSpeed * Time.deltaTime);
} else {
Anim.SetBool ("LANZAR", false);



}
}
}
}
}
}
}
}
}
}

}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Las granadas no hacen nada pero al contacto con los cubos que ban a simular municion o explosibos o combustible dan efecto de explosion gracias al script de ActivateTrigger



using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace UnityStandardAssets.Utility
{
public class ActivateTrigger : MonoBehaviour
{
// A multi-purpose script which causes an action to occur when
// a trigger collider is entered.
public enum Mode
{
Trigger = 0, // Just broadcast the action on to the target
Replace = 1, // replace target with source
Activate = 2, // Activate the target GameObject
Enable = 3, // Enable a component
Animate = 4, // Start animation on target
Deactivate = 5 // Decativate target GameObject
}

public Mode action = Mode.Activate; // The action to accomplish
public Object target; // The game object to affect. If none, the trigger work on this game object
public GameObject source;
public int triggerCount = 1;
public bool repeatTrigger = false;


private void DoActivateTrigger()
{
triggerCount--;

if (triggerCount == 0 || repeatTrigger)
{
Object currentTarget = target ?? gameObject;
Behaviour targetBehaviour = currentTarget as Behaviour;
GameObject targetGameObject = currentTarget as GameObject;
if (targetBehaviour != null)
{
targetGameObject = targetBehaviour.gameObject;
}

switch (action)
{
case Mode.Trigger:
if (targetGameObject != null)
{
targetGameObject.BroadcastMessage("DoActivateTrigger");
}
break;
case Mode.Replace:
if (source != null)
{
if (targetGameObject != null)
{
Instantiate(source, targetGameObject.transform.position,
targetGameObject.transform.rotation);
DestroyObject(targetGameObject);
}
}
break;
case Mode.Activate:
if (targetGameObject != null)
{
targetGameObject.SetActive(true);
}
break;
case Mode.Enable:
if (targetBehaviour != null)
{
targetBehaviour.enabled = true;
}
break;
case Mode.Animate:
if (targetGameObject != null)
{
targetGameObject.GetComponent<Animation>().Play();
}
break;
case Mode.Deactivate:
if (targetGameObject != null)
{
targetGameObject.SetActive(false);
}
break;
}
}
}


private void OnTriggerEnter(Collider other)
{
DoActivateTrigger();
}
}
}

---------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------

que sustituye un gameobject por otro en este caso por explosiones prefabricados descargados del asset gratuito de unity del principal de ellos el que viene por defecto el mas recurrido……


Es cierto que nada tiene que ver la salida de la granada con el movimiento del brazo simplemente ago coincidir la tecla “m” con la acción de disparar con un script aparte para que baya el disparador automático funcionando y la misma letra “m” para activar la animación de personaje con el otro script diferente...

https://youtu.be/cTSwWrXgVow

Añado un script mas que facilita la inclinación con el mouse del lanzador de granadas para lanzar mas lejos o mas cerca.



using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Utility
{
public class SimpleMouseRotator : MonoBehaviour
{
// A mouselook behaviour with constraints which operate relative to
// this gameobject's initial rotation.
// Only rotates around local X and Y.
// Works in local coordinates, so if this object is parented
// to another moving gameobject, its local constraints will
// operate correctly
// (Think: looking out the side window of a car, or a gun turret
// on a moving spaceship with a limited angular range)
// to have no constraints on an axis, set the rotationRange to 360 or greater.
public Vector2 rotationRange = new Vector3(70, 70);
public float rotationSpeed = 10;
public float dampingTime = 0.2f;
public bool autoZeroVerticalOnMobile = true;
public bool autoZeroHorizontalOnMobile = false;
public bool relative = true;


private Vector3 m_TargetAngles;
private Vector3 m_FollowAngles;
private Vector3 m_FollowVelocity;
private Quaternion m_OriginalRotation;


private void Start()
{
m_OriginalRotation = transform.localRotation;
}


private void Update()
{
// we make initial calculations from the original local rotation
transform.localRotation = m_OriginalRotation;

// read input from mouse or mobile controls
float inputH;
float inputV;
if (relative)
{
inputH = CrossPlatformInputManager.GetAxis("Mouse X");
inputV = CrossPlatformInputManager.GetAxis("Mouse Y");

// wrap values to avoid springing quickly the wrong way from positive to negative
if (m_TargetAngles.y > 180)
{
m_TargetAngles.y -= 360;
m_FollowAngles.y -= 360;
}
if (m_TargetAngles.x > 180)
{
m_TargetAngles.x -= 360;
m_FollowAngles.x -= 360;
}
if (m_TargetAngles.y < -180)
{
m_TargetAngles.y += 360;
m_FollowAngles.y += 360;
}
if (m_TargetAngles.x < -180)
{
m_TargetAngles.x += 360;
m_FollowAngles.x += 360;
}

#if MOBILE_INPUT
// on mobile, sometimes we want input mapped directly to tilt value,
// so it springs back automatically when the look input is released.
if (autoZeroHorizontalOnMobile) {
m_TargetAngles.y = Mathf.Lerp (-rotationRange.y * 0.5f, rotationRange.y * 0.5f, inputH * .5f + .5f);
} else {
m_TargetAngles.y += inputH * rotationSpeed;
}
if (autoZeroVerticalOnMobile) {
m_TargetAngles.x = Mathf.Lerp (-rotationRange.x * 0.5f, rotationRange.x * 0.5f, inputV * .5f + .5f);
} else {
m_TargetAngles.x += inputV * rotationSpeed;
}
#else
// with mouse input, we have direct control with no springback required.
m_TargetAngles.y += inputH*rotationSpeed;
m_TargetAngles.x += inputV*rotationSpeed;
#endif

// clamp values to allowed range
m_TargetAngles.y = Mathf.Clamp(m_TargetAngles.y, -rotationRange.y*0.5f, rotationRange.y*0.5f);
m_TargetAngles.x = Mathf.Clamp(m_TargetAngles.x, -rotationRange.x*0.5f, rotationRange.x*0.5f);
}
else
{
inputH = Input.mousePosition.x;
inputV = Input.mousePosition.y;

// set values to allowed range
m_TargetAngles.y = Mathf.Lerp(-rotationRange.y*0.5f, rotationRange.y*0.5f, inputH/Screen.width);
m_TargetAngles.x = Mathf.Lerp(-rotationRange.x*0.5f, rotationRange.x*0.5f, inputV/Screen.height);
}

// smoothly interpolate current values to target angles
m_FollowAngles = Vector3.SmoothDamp(m_FollowAngles, m_TargetAngles, ref m_FollowVelocity, dampingTime);

// update the actual gameobject's rotation
transform.localRotation = m_OriginalRotation*Quaternion.Euler(-m_FollowAngles.x, m_FollowAngles.y, 0);
}
}
}




No hay comentarios:

Publicar un comentario