I am rather new to c# and am making a sumo game with unity 3d. The sumo game isn’t normal sumo where you wrestle your opponent off the edge, in my version you shoot them with a gun off the edge. There are two scripts, the Target script (the script that goes on the Gameobject being shot at) and the gun script (the script that shoots the raycast). So, want to be able for the gameObject I shoot at to be moved in the opposite direction I shot it in (basically knockback). The problem is, if I shoot it from the side or back, it still moves in the same direction as if I were shooting the front. What is the easiest way I could do this?
My target script:
using UnityEngine;
public class Target : MonoBehaviour
{
//public float health = 50f;
public void TakeDamage (float amount)
{
transform.position += Vector3.forward * Time.deltaTime * 50f ;
}
void Die ()
{
Destroy(gameObject);
}
}
My gun script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float p1kb = 50;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
Shoot();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, range));
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
Debug.DrawRay(transform.position, transform.forward, Color.green);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(hit.normal * p1kb);
}
}
}
}
,I am making sort of like a sumo game, although instead of wrestling the other player of the edge, you shoot them off with a gun to win. There are two scripts, a target script (the script that goes on the object being shot) and a gun script (the script that shoots the recast from the actual gun).
So, I want to be able for the gameObject I shoot at to be moved in the opposite direction i shot it in (basically knockback). The problem is, if I shoot it from the side or back, it still moves in the same direction as if I were shooting the front.
My target script:
using UnityEngine;
public class Target : MonoBehaviour
{
//public float health = 50f;
public void TakeDamage (float amount)
{
transform.position += Vector3.forward * Time.deltaTime * 50f ;
}
void Die ()
{
Destroy(gameObject);
}
}
My gun script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float p1kb = 50;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
Shoot();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, range));
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
Debug.DrawRay(transform.position, transform.forward, Color.green);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(hit.normal * p1kb);
}
}
}
}