Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sethixias · Apr 26, 2014 at 08:48 PM · raycastmultiplayervector3third-personaim

Networked 3rd person aiming

Hello! I am working on a multiplayer TPS and can't seem to figure out an issue I'm having with aiming online.

I'm sending a ray from the viewpoint forward and getting the distance between the weapon and the hitinfo to create a float and using that float to determine how far the weapon should point. It looks like this-

 using UnityEngine;
 using System.Collections;
 
 public class UseAbilities : MonoBehaviour {
 
     private PlayerCharacter playerChar;
 
 
     public GameObject bullet;
 
     private Transform myTransform;
 
     private Transform leftHandTransform;
 
     private Transform rightHandTransform;
 
     private Vector3 launchPos = new Vector3();
 
     private float fireRate = 0.1f;
 
     private float nextFire;
 
     private Quaternion launchRot = new Quaternion();
 
     private Vector3 crossHairVec = new Vector3();
     private RaycastHit rayHit;
     private float distance;
 
     private RaycastHit raycastHit;
 
     private bool hit;
 
     // Use this for initialization
     void Start () {
 
         playerChar = transform.GetComponent<PlayerCharacter>();
 
         myTransform = transform;
 
         leftHandTransform = playerChar.weaponMountL.transform;
 
         rightHandTransform = playerChar.weaponMountR.transform;
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Physics.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0)),out rayHit,1000f)){
             if(rayHit.transform.tag == "ProjectileObstruction" /*||rayHit.transform.tag == "Player"*/){
                 distance = Vector3.Distance(Camera.main.transform.position, rayHit.transform.position);
             }
         }
         
         if(distance == null){
             distance = 1000f;
         }
         
         if(distance < 1){
             distance = 1000f;
         }
 
         crossHairVec = Camera.main.ViewportToWorldPoint(new Vector3(0.5f,0.5f,distance));
 
         if(Input.GetButton("Fire1") && Time.time > nextFire && Network.peerType == NetworkPeerType.Disconnected && Screen.lockCursor == true){
             
             launchPos = leftHandTransform.TransformPoint(0,-0.1f,0);
 
             launchRot = Quaternion.LookRotation(crossHairVec);
 
             nextFire = Time.time + fireRate;
 
             Instantiate(bullet,launchPos, launchRot);
 
 
         }
         if(Input.GetButton("Fire1") && Time.time > nextFire && Network.peerType != NetworkPeerType.Disconnected && Screen.lockCursor == true){
 
             launchPos = leftHandTransform.TransformPoint(0,-0.1f,0);
 
             launchRot = Quaternion.LookRotation(crossHairVec);
             
             nextFire = Time.time + fireRate;
             
             networkView.RPC("ProjectileRPC",RPCMode.All,launchPos,launchRot);
             
         }
 
     }
 
     [RPC]
     void ProjectileRPC(Vector3 lp, Quaternion lr){
         Instantiate(bullet,lp,lr);
         }
 }


It seems to work offline, but online it shoots at very odd angles, almost as if it's using the server cam to aim but I know that's not the case. The camera can zoom in and out by the way, which is one reason I'm doing it like this. Any ideas?

P.S. Ignore the fact player tag is commented out, just doing some testing to make sure it wasn't hitting something on my model.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i don't understand Vector3.forward correctly on camera 1 Answer

Problem with LineCast Bullet 1 Answer

Null reference exception on Screenpointtoray (Multiplayer) 1 Answer

Aim-Assist with raycast 1 Answer

Not getting right value; ScreenPointToRay 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