Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 urnandp · Mar 10 at 09:56 AM · 3dall

Anyone know how to code it so the target will respawn instead of carrying moving

I am making an aim trainer and i am making a moving targets task, the targets are hit because the score goes up but the target does not stop moving and will only respawn while being hit when the target is not moving. This is the code using System.Collections; using System.Collections.Generic; using UnityEngine;

public class targetshooter : MonoBehaviour {

 public float clicks = 0;
 public float shots_hit = 0;
 

 public Camera fpsCam;
 

 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }
 }
 
 
 void Shoot()
 {
     clicks = clicks + 1;
     RaycastHit hit;
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
     {
         Debug.Log(hit.transform.name);

         target target = hit.collider.gameObject.GetComponent<target>();

         if (target != null)
         {
             target.Hit();
             shots_hit = shots_hit + 1; 
         }

             
     }
     
 }

} this is for the gun using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class target : MonoBehaviour { static float scores = 0;

 [SerializeField] Text scoreText;
 public void Hit()
 {
     transform.position = targetbounds.Instance.GetRandomPosition();
    
     scores = scores + 1;

    
     
     
 }

 void Update()
 {
     scoreText.text = scores.ToString();
 }

} this is for the target when hit using System.Collections; using System.Collections.Generic; using UnityEngine;

public class targetbounds : MonoBehaviour { public static targetbounds Instance;

 void Awake()
 {
     Instance = this;
 }

 [SerializeField] BoxCollider col;

 public Vector3 GetRandomPosition()
 {
     Vector3 center = col.center + transform.position;

     float minX = center.x - col.size.x / 2f;
     float maxX = center.x + col.size.x / 2f;

     float minY = center.y - col.size.y / 2f;
     float maxY = center.y + col.size.y / 2f;

     float minZ = center.z - col.size.z / 2f;
     float maxZ = center.z + col.size.z / 2f;

     float randomX = Random.Range(minX, maxX);
     float randomY = Random.Range(minY, maxY);
     float randomZ = Random.Range(minZ, maxZ);

     Vector3 randomPosition = new Vector3(randomX, randomY, randomZ);

     return randomPosition;
 }

} this get a random spawn for the target using System.Collections; using System.Collections.Generic; using UnityEngine;

public class moverandom : MonoBehaviour { private float movementDuration = 2.0f; private float waitBeforeMoving = 0.2f; private bool hasarrived = false; public bool Active = true;

 void Update()
 {
     
     
     if (!hasarrived)
     {
         hasarrived = true;
         float randX = Random.Range(-5.0f, 5.0f);
         float randY = Random.Range(4.0f, 8.0f);
         float randZ = Random.Range(-5.0f, 5.0f);
         StartCoroutine(movetopoint(new Vector3(randX, randY, randZ)));
         
         
     }
     
     
 }

 private IEnumerator movetopoint(Vector3 targetpos)
 {
     float timer = 0.0f;
     Vector3 startpos = transform.position;

     while (timer < movementDuration)
     {
        
         timer += Time.deltaTime;
         float t = timer / movementDuration;
         t = t * t * t * (t * (6f * t - 15f) + 10f);
         transform.position = Vector3.Lerp(startpos, targetpos, t);

         yield return null;
     }
     

     yield return new WaitForSeconds(waitBeforeMoving);
     hasarrived = false;
 }

} this is the code to move the target by getting a random location to move to. anyone know how to respawn the target instead of it moving. The movement script and hit script are a component on the object target.

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

132 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

Related Questions

All imported 3d models share the same color? 1 Answer

3d Models, How to turn invisible 3 Answers

Adding force to a bullet 3 Answers

Creating 3d planets with terrain and atmosphere 3 Answers

Sprites: Project Sprite to Mesh, allow for bending of sprites for 3d environments 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