Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by berskr · Jul 12, 2016 at 08:18 AM · raycasthitcamera-lookaddforceatposition

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).

alt text

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
 }
 

screencap.png (192.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Vector3.Distance between camera and raycasthit 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges