Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Sefcio · Jul 07, 2015 at 03:57 PM · raycasttransformdirectionlinerenderercrosshair

Raycast LineRenderer, how to set end direction of Ray by transform to the center of the screen or crosshair?

I try to do a simple fps, and I just can't figure how I can do this. I need to change lane 82 of my script to be center of my screen or crosshair, any guidance?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerShooting : MonoBehaviour
 {
     public int minDmg = 15;
     public int maxDmg = 25;
     public float timeBetweenBullets = 0.15f;
     public float range = 50;
     
     public Texture crossHair;
     public Rect middle;
 
     Ray shootRay;
     RaycastHit shootHit;
     ParticleSystem gunParticles;
     LineRenderer gunLine;
     AudioSource gunAudio;
     Light gunLight;
     int shootableMask;
     float effectsDisplayTime = 0.2f;
     float timer;
 
 
     void OnGUI() 
     {
         GUI.DrawTexture (middle, crossHair);
     }
     
     
     void Awake ()
     {
         shootableMask = LayerMask.GetMask ("Shootable");
         gunParticles = GetComponent<ParticleSystem> ();
         gunLine = GetComponent <LineRenderer> ();
         gunAudio = GetComponent<AudioSource> ();
         gunLight = GetComponent<Light> ();
     }
 
 
     void Update ()
     {
         timer += Time.deltaTime;
 
         middle = new Rect((Screen.width - crossHair.width/4) / 2, (Screen.height - crossHair.height/4) /2, crossHair.width /4, crossHair.height/4);
         
         if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
         {
             Shoot ();
         }
 
         if(timer >= timeBetweenBullets * effectsDisplayTime)
         {
             DisableEffects ();
         }
     }
 
 
     public void DisableEffects ()
     {
         gunLine.enabled = false;
         gunLight.enabled = false;
     }
 
 
     void Shoot ()
     {
         timer = 0f;
 
         gunAudio.Play ();
          
         gunLight.enabled = true;
 
         gunParticles.Stop ();
         gunParticles.Play ();
 
         gunLine.enabled = true;
         gunLine.SetPosition (0, transform.position);
 
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;     //I NEED THIS TO BE THE CENTER OF MY SCREEN OR CENTER OF MY CROSSHAIR
 
         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             if(enemyHealth != null)
             {
                 enemyHealth.TakeDamage (Random.Range(minDmg, maxDmg), shootHit.point);
             }
             gunLine.SetPosition (1, shootHit.point);
         }
         else
         {
             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }
     }
 }


Comment
Add comment · Show 2
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
avatar image maccabbe · Jul 09, 2015 at 04:06 PM 0
Share

http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

avatar image Sefcio · Jul 09, 2015 at 07:08 PM 0
Share

I'm not asking because I ain't tried this, I'm asking because I tried and i cant figure it out, and need help...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mesuttasci · Oct 18, 2018 at 02:46 PM

i was looking for a solution to my problem, i found your question. Let me answer, maybe someone will have a problem like yours;)

Line 79 : "gunLine.SetPosition (0, transform.position);" is the starting point of Line. You can change it like gunLine.SetPosition(0, new Vector3(0,0,0));

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast between TransformDirection and Vector3.Forward? 1 Answer

In my gun script the bullets aren't hitting the correct area. 0 Answers

Third Person Cross Hair Question 1 Answer

JS Dynamic Height Camera vibrating =( 1 Answer

Push object in direction of camera 0 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