- Home /
not getting direction of view to push objects (AddForce)
Hi all, sorry for messy code, ... I´m new to this.
I´m using the FPSController, and I want to push Objects into the direction I´m lookin to. But my scripts/setups only keeps shooting/pushing them into one single direction.
I tried different versions but I cant manage to get it work. The scene Setup explanation: the "_ScriptStack", is a (empty) GameObject containing all the Scripts except "Raycaster"-Script. Raycaster-Script is on "FirstPersonCharacter".
(I wanted to have all the scripts on one spot(_ScriptStack). But I didn´t get the raycast to work as long it was not on the "FirstPersonCharacter".)
Please hint me to the right direction ;D And perhaps you could give some critics on how I have build this up or how one should build things up. Btw, I´m trying to do a FirstPerson meele-combat game, like: http://store.steampowered.com/app/219640/ (Chivalry).
Short: How to get looking/aiming Direction for AddForce?
Raycaster Script: using UnityEngine; using System.Collections;
public class Raycaster : MonoBehaviour {
public Weapon _weapon; //length of weapon&ray from Weapon.cs
public FightSystem _fightSys;
public RaycastHit rayInfo;
public Vector3 fwd;
public bool rayHitDetection;
public float rayDistance;
public string rayHitObjectName;
// Use this for initialization
void Start ()
{
//shortcut to the GameObject containing the Weapon.cs, not needed when on same GameObject
_weapon = GameObject.Find("_ScriptStack").GetComponent<Weapon>();
_fightSys = GameObject.Find("_ScriptStack").GetComponent<FightSystem>();
}
// Update is called once per frame
void Update ()
{
//declaring a forward looking vector(?)
fwd = transform.TransformDirection(Vector3.forward) * _weapon.wLength;
Debug.DrawRay(transform.position, fwd, Color.green);
//Shoots a ray, wether object is in range or not!
if (Input.GetMouseButtonDown(0) && (Physics.Raycast(transform.position, fwd, out rayInfo, _weapon.wLength)))
{
//set rayVariables to make calling them easier
//!!!bool to int!!!! rayInfo = rayHitDetection;
rayDistance = rayInfo.distance;
rayHitObjectName = rayInfo.collider.name;
Debug.Log("ObjName: " + rayHitObjectName);
Debug.Log("rayDistance: " + rayDistance);
_fightSys.Attack();
}
else
{
//MISS
if(Input.GetMouseButtonDown(0) && (!Physics.Raycast(transform.position, fwd, out rayInfo, _weapon.wLength)))
Debug.Log("nuttin in range");
}
}
}
FightSystem-Script:
using UnityEngine;
using System.Collections;
public class FightSystem : MonoBehaviour {
public Weapon _weapon;
public Raycaster _ray;
public Raycaster _fwd;
// Use this for initialization
void Start ()
{
_weapon = GameObject.Find("_ScriptStack").GetComponent<Weapon>();
_ray = GameObject.Find("FPSController").GetComponentInChildren<Raycaster>();
_fwd = GameObject.Find("FPSController").GetComponentInChildren<Raycaster>();
}
// Update is called once per frame
void Update ()
{
Debug.Log(_ray.transform.position);
}
public void Attack()
{
//Punch the Object, but only Objects which have a rigidbody.
if (_ray.rayInfo.rigidbody != null)
{
//_ray.rayInfo.rigidbody.AddForceAtPosition(transform.forward * _weapon.wDamage, _ray.rayInfo.point); //ADDFORCE change direction
_ray.rayInfo.rigidbody.AddForceAtPosition(_fwd.transform.position * _weapon.wDamage, _ray.rayInfo.point); //ADDFORCE change direction
}
}
//damage the enemy which is infront of you
//swing your weapon/play anim (perhaps do some refuel time)
//give points or shit
}
Thanks for letting me bother you wise-guys! :)
EDIT: I made it work, but I don´t know why. Really great -.- I had it tested with "Camera.main.transform.forward" and it didn´t work. But now it does! Really strange. For others, who may have the same problem, here my solution: using UnityEngine; using System.Collections;
public class Raycaster : MonoBehaviour {
public Weapon _weapon; //length of weapon&ray from Weapon.cs
public FightSystem _fightSys;
public RaycastHit rayInfo;
public bool rayHitDetection;
public float rayDistance;
public string rayHitObjectName;
// Use this for initialization
void Start ()
{
//shortcut to the GameObject containing the Weapon.cs, not needed when on same GameObject
_weapon = GameObject.Find("_ScriptStack").GetComponent<Weapon>();
_fightSys = GameObject.Find("_ScriptStack").GetComponent<FightSystem>();
}
// Update is called once per frame
void Update ()
{
//declaring a forward looking vector(?)
Vector3 fwd = transform.TransformDirection(Vector3.forward) * _weapon.wLength;
Debug.DrawRay(transform.position, fwd, Color.green);
//Shoots a ray, wether object is in range or not!
if (Input.GetMouseButtonDown(0) && (Physics.Raycast(transform.position, fwd, out rayInfo, _weapon.wLength)))
{
//set rayVariables to make calling them easier
//!!!bool to int!!!! rayInfo = rayHitDetection;
rayDistance = rayInfo.distance;
rayHitObjectName = rayInfo.collider.name;
Debug.Log("ObjName: " + rayHitObjectName);
Debug.Log("rayDistance: " + rayDistance);
_fightSys.Attack();
}
else
{
//MISS
if(Input.GetMouseButtonDown(0) && (!Physics.Raycast(transform.position, fwd, out rayInfo, _weapon.wLength)))
Debug.Log("nuttin in range");
}
}
}
using UnityEngine;
using System.Collections;
public class FightSystem : MonoBehaviour {
public Weapon _weapon;
public Raycaster _ray;
public Raycaster _fwd;
// Use this for initialization
void Start ()
{
_weapon = GameObject.Find("_ScriptStack").GetComponent<Weapon>();
_ray = GameObject.Find("FPSController").GetComponentInChildren<Raycaster>();
}
// Update is called once per frame
void Update ()
{
Debug.Log(_ray.transform.position);
}
public void Attack()
{
//Punch the Object, but only Objects which have a rigidbody.
if (_ray.rayInfo.rigidbody != null)
{
_ray.rayInfo.rigidbody.AddForceAtPosition(Camera.main.transform.forward * _weapon.wDamage, _ray.rayInfo.point);
}
}
//damage the enemy which is infront of you
//swing your weapon/play anim (perhaps do some refuel time)
//give points or shit
}
Your answer
Follow this Question
Related Questions
Raycast hitting objects to the left of my player 1 Answer
Player Attacking Enemy Issue 0 Answers
Trouble with rigidbody.AddForce 0 Answers
Raycast problems! 1 Answer