• 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 /
This question was closed Aug 11, 2020 at 07:51 PM by Syntax_3rr0r for the following reason:

Found out it was becouse of the animation moving the object instead of the mesh

avatar image
0
Question by Syntax_3rr0r · Aug 11, 2020 at 10:14 AM · vector3transform.positionglitch

Object not moving with code

So I thought I had it working, but now if I aim and switch off the gun then switch back it stays in the aiming position permanently. I have no idea why. here's the code that makes you aim

         if (Input.GetButtonDown("Fire2") && IsReloadingScript.IsReloading == false)
         {
             Crosshair.SetActive(false);
             transform.localPosition = AimDownSight;
             fpsCamP1.fieldOfView = AimZoom;
             
         }
 
         if (Input.GetButtonUp("Fire2"))
         {
             Crosshair.SetActive(true);
             transform.localPosition = HipFire;
             fpsCamP1.fieldOfView = 66;
         }

 

Heres the rest of the code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Gun : MonoBehaviour {
 
   
    
 
     public float damage = 10f;
     public float range = 100f;
     public float fireRate = 15f;
     public float impactForce = 30f;
     public float Ammo = 48f;
     public float clip = 6f;
     public float ReloadAmount = 6f;
     public float ReloadingTime = 1f;
     public float MovmentSlowWhileAiming;
     public float AimZoom = 60f;
     public string ReloadAnimationName;
 
     public AudioSource audioSource;
     public AudioClip RevolverShoot;
     public float volume = 0.5f;
 
     public Camera fpsCamP1;
     public ParticleSystem muzzleFlash;
     public GameObject impactEffectStone;
     public GameObject impactEffectDirt;
     public GameObject impactEffectWood;
     public GameObject impactEffectBlood;
     public GameObject DamagePopUp;
     public Text AmmoText;
     public Text CylinderText;
 
     private float nextTimeToFire = 0f;
     private WeaponSwitcher IsReloadingScript;
 
     public Vector3 AimDownSight;
     public Vector3 HipFire;
 
     public GameObject Crosshair;
     public GameObject WeaponHolder;
 
     public Animator anime;
     public Animator popUpTextAnime;
 
     private void OnEnable()
     {
         transform.localPosition = HipFire;
         Crosshair.SetActive(true);
         AmmoText.text = "Ammo " + Ammo.ToString();
         CylinderText.text = "Cylinder " + clip.ToString();
         fpsCamP1.fieldOfView = 66;
 
     }
 
     private void Start()
     {
         IsReloadingScript = WeaponHolder.GetComponent<WeaponSwitcher>();
         AmmoText.text = "Ammo " + Ammo.ToString();
         CylinderText.text = "Cylinder " + clip.ToString();
         transform.localPosition = HipFire;
     }
     // Update is called once per frame
     void Update () {
 
         
 
         //aiming
         if (Input.GetButtonDown("Fire2") && IsReloadingScript.IsReloading == false)
         {
             Crosshair.SetActive(false);
             transform.localPosition = AimDownSight;
             fpsCamP1.fieldOfView = AimZoom;
             
         }
         if (Input.GetButtonUp("Fire2"))
         {
             Crosshair.SetActive(true);
             transform.localPosition = HipFire;
             fpsCamP1.fieldOfView = 66;
         }
 
         //fire
         if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire && clip != 0 && IsReloadingScript.IsReloading == false)
         {
             audioSource.PlayOneShot(RevolverShoot, volume);
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
             CylinderText.text = "Cylinder " + clip.ToString();
 
 
         }
 
         //reload
         if (Input.GetKeyDown(KeyCode.R) && Ammo != 0 && Ammo > 0 && ReloadAmount != clip && IsReloadingScript.IsReloading == false)
         {
 
             Reloading();
 
     
 
         }
     }
 
 
  
 
 
   
 
     void Shoot()
     {
         muzzleFlash.Play();
         clip -= 1f;
 
         //Sends out an invisable ray and sees if it hits somtehing, then stores that in Hit.
 
         RaycastHit hit;
         if (Physics.Raycast(fpsCamP1.transform.position, fpsCamP1.transform.forward, out hit, range))
         {
            
             //test if the ray hits an object with the target code
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 target.TakeDamage(damage);
                 GameObject DamageTextGameObject = Instantiate(DamagePopUp, hit.transform.position, fpsCamP1.transform.rotation);
                 Destroy(DamageTextGameObject, 1f);
              
             }
 
             //adds knockback to the object
             if (hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
 
             }
 
             //Tests if hits stone. Shows stone hole effect
             if (hit.transform.tag.Contains("Stone"))
             {
                 GameObject impactGameObject = Instantiate(impactEffectStone, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(impactGameObject, 2f);
 
             }
             //Tests if dirt/sand. shows dirt/sand hole effect
             if (hit.transform.tag.Contains("Dirt") || hit.transform.tag.Contains("Sand"))
             {
 
                 GameObject impactGameObject = Instantiate(impactEffectDirt, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(impactGameObject, 2f);
 
             }
             //Tests if human. shows blood effect
             if (hit.transform.tag.Contains("Fleshy"))
             {
 
                 GameObject impactGameObject = Instantiate(impactEffectBlood, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(impactGameObject, 2f);
 
             }
 
             //Tests if wood, shows wood effect
             if (hit.transform.tag.Contains("Wood"))
             {
 
                 GameObject impactGameObject = Instantiate(impactEffectWood, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(impactGameObject, 2f);
 
             }
 
 
             
 
         }
 
    
 
     }
 
 
     private void Reloading()
     {
         StartCoroutine(Reload);
 
     }
 
     private IEnumerator Reload
     {
         get
         {
 
             for (int i = 0; clip < ReloadAmount; i++)
             {
                 IsReloadingScript.IsReloading = true;
                 
                 Ammo -= 1f;
                 clip += 1f;
                 anime.Play(ReloadAnimationName);
                 AmmoText.text = "Ammo " + Ammo.ToString();
                 CylinderText.text = "Cylinder " + clip.ToString();
                 yield return new WaitForSeconds(ReloadingTime);
 
             }
 
             IsReloadingScript.IsReloading = false;
 
 
 
         }
         
 
     }
     
 }
 






Comment
Add comment · Show 1
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 Syntax_3rr0r · Aug 08, 2020 at 04:47 PM 0
Share

So I currently have a temporary fix which is the prevention of changing weapons while aiming. I'm going to keep the question open tho as this is very strange why it won't apply the transform

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Move an object to Input.Touch location 0 Answers

Vector3.Distance is acting erratically 2 Answers

How to maintain player chosen camera height? 0 Answers

Vector3.Lerp making my object invisible. 2 Answers

Object keeps moving when using Vector3.Lerp on transform.position 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges