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 DevmobiStudios · Nov 18, 2017 at 02:40 PM · rotationraycastfps tutorial

[FPSGame] gun's muzzlePoint rotation Update Problem

my biggest problem is :

I am making a commercial FPS Game and i am stuck on MuzzlePoint's Rotation. i want my Bullet Tracer go from the muzzlePoint's current location to center of the screen (crosshair's center).

my Incomplete Script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AssaultRifle : MonoBehaviour {
 
     public float currentBullets;
     public float bulletsPerMag;
     public float fireRate;
 
     public GameObject muzzlePoint;
     public Camera FPSCamera;
 
     float firetimer;
 
     bool isReloading;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.Mouse0) && currentBullets < 0) {
             Fire ();
         }
     }
 
     void Fire()
     {
         if (isReloading)
             return;
 
         RaycastHit hit;
         if (Physics.Raycast (FPSCamera.transform.position, FPSCamera.transform.forward)) {
             // What to do here ????
             // muzzlePoint.transform.localRotation = hit.point;  Failed!!!   :(
         }
 
         if (firetimer < fireRate)
             firetimer = Time.deltaTime;
     }
 }
 

all i want to do is keep the rotation of MuzzlePoint to the raycasthit hit's hit Location..

plzz helpp !!! :(

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SonicScrew12 · Nov 18, 2017 at 09:23 PM

Firstly, hit.point is of type Vector3, meaning it returns a single point in 3D space. You're trying to apply a rotation (which is of type Quaternion) from a Vector3 so you're getting a type mismatch. You can't directly cast a quaternion to a Vector3, you need to multiply it by a vector.

Now, if all you want to do is make the bullet tracer go from the muzzle point to the point where the raycast hit, you could just do this (assuming you're rotating the camera and the player independently):

 GameObject bulletTracer = Instantiate(bulletTracerPrefab, muzzlePoint.transform.position, Quaternion.Euler(FPSCamera.transform.rotation.eulerAngles.x, player.transform.rotation.eulerAngles.y, 0f) );

Also assuming you have a GameObject called 'player' so it can get the euler angle from that object. I've just tried this and it works fine.

I hope I was able to solve your issue. Good luck, and happy coding.

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
avatar image
0

Answer by DevmobiStudios · Nov 19, 2017 at 07:28 PM

@SonicScrew12 I Didn't mean that !!

I Mean i want to instantiate the tracer at the gun's end and move it to the center of the screen and at the same time, making it charge forward...

for example IN SHORT - - when u see bullet tracers in Call of Duty - Modern Warfare 3

Comment
Add comment · Show 1 · 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
avatar image SonicScrew12 · Nov 19, 2017 at 11:19 PM 0
Share

Do not place responses in your answers. Just use my method and Translate() the bullet trail forward, since during instantiation it will be facing the center of the screen anyways.

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

125 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 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 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 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 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 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

transform.right not working correctly? 0 Answers

How to get Raycast surface normal to align particle system? 1 Answer

Offset raycast by specific amount 1 Answer

Detect if on ground? 1 Answer

RayCast offset rotation 20 degrees 1 Answer


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