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 Masrura · Apr 10, 2020 at 06:13 AM · speed issuespunch

The speed of Ai and player becomes zero on punch

Hello! I was following a game tutorial on udemy but the course is deleted. The game consists of a platform with 4 Ai and 1 player where the player and Ai punches each other to fight and get their respective enemies out of the platform to gain score. My problem is, when the Ai punches another Ai or the main player, the speed of Ai or main player becomes zero and they don't move. Can anyone please help me with this?

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 azerty0220pl · Apr 10, 2020 at 08:44 AM 0
Share

Could you please upload some code to give us more information?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Masrura · Apr 10, 2020 at 09:17 AM

using System.Collections; using System.Collections.Generic; using UnityEngine; using CnControls; using DG.Tweening; using TMPro; using UnityEngine.AI;

public class easyplayer : MonoBehaviour { public bool active; public bool isAi;

 public Transform[] bones;
 public bool keepBonesStraight;
 public float speed;
 public float turnSpeed;
 public bool isInZone;
 public int score;
 public Transform scoreFx;
 public Transform zoneEasy;
 public Transform target;
 public float outsideOfZoneRange;
 public ParticleSystem easytrailFx;

 public bool canPunch = true;
 public float punchForce;
 public GameObject punchFx;
 public bool stunned;
 public Transform puncher;


 public Transform scoreCard;
 public TextMeshPro playerName;
 public GameObject crown;


 private Rigidbody rb;
 private gamemanager gamemanagerScript;
 private NavMeshAgent nav;
 private zoneEasy zoneEasyScript;
 private float defaultSpeed;
 private Animator easyanim;


 public AudioSource punchSfx;



 void Start()
 {
     rb = GetComponent<Rigidbody>();
     gamemanagerScript = GameObject.FindWithTag("easymanager").GetComponent<gamemanager>();
     easyanim = GetComponent<Animator>();
     InvokeRepeating("addZoneScore", 1, 1);

     if (isAi)
     {
         nav = GetComponent<NavMeshAgent>();
         zoneEasyScript = zoneEasy.GetComponent<zoneEasy>();
     }
     else
     {
         defaultSpeed = speed;
     }
 }
 private void OnEnable()
 {
     if (active)
     {
         canPunch = true;
         stunned = false;

         ParticleSystem.EmissionModule em = easytrailFx.emission;
         em.enabled = true;
         easyanim.SetBool("eRun", true);
         rb.velocity = Vector3.zero;

         if (isAi)
             nav.enabled = true;

         else
         {
            // camFollowScript.enabled = true;
             speed = defaultSpeed;
         }

        // if (isBomber)
            // unstable = false;

         Vector3 dir = new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(zoneEasy.position.x, 0, zoneEasy.position.z);

         transform.rotation = Quaternion.LookRotation(-dir);

     }

 }



 void Update()
 {

     if (Input.GetKeyDown(KeyCode.Escape))
     {
         Application.LoadLevel("Title");
     }

    if (gamemanagerScript.gameStarted && !active)
     {
         active = true;
         easyanim.SetBool("eRun", true);
         ParticleSystem.EmissionModule em = easytrailFx.emission;
         em.enabled = true;

         if (isAi)
             nav.enabled = true;
     }


     if (active)
     {
         if (isAi)
         {
             if (!stunned)
             {

                 if (gamemanagerScript.playersInZone.Contains(transform))
                 {
                     target = getClosestEnemyInZone(gamemanagerScript.playersInZone);

                     if (target == null)
                         target = zoneEasyScript.wayPoints[Random.Range(0, zoneEasyScript.wayPoints.Length)];


                 }
                 else
                 {
                     target = getClosestEnemy(gamemanagerScript.players);

                     if (Vector3.Distance(transform.position, target.position) > outsideOfZoneRange)

                         target = zoneEasy;
                 }
                 nav.SetDestination(target.position);
             }
         }


         else
         {

             if (Input.GetMouseButton(0))
             {
                 Vector3 touchMagnitude = new Vector3(CnInputManager.GetAxis("Horizontal"), CnInputManager.GetAxis("Vertical"), 0);
                 Vector3 touchPos = transform.position + touchMagnitude.normalized;
                 Vector3 touchDir = touchPos - transform.position;
                 float angle = Mathf.Atan2(touchDir.y, touchDir.x) * Mathf.Rad2Deg;
                 angle -= 90;
                 Quaternion rot = Quaternion.AngleAxis(angle, Vector3.down);
                 transform.rotation = Quaternion.Lerp(transform.rotation, rot, turnSpeed * Mathf.Min(Time.deltaTime, .04f));


             }
         }

        /* if (transform.position.y < -1 && !stunned)
         {
             ParticleSystem.EmissionModule em = easytrailFx.emission;
             em.enabled = false;
             stunned = true;
             speed = 0;
             StartCoroutine(death());
         }*/
     }
 }

 private void FixedUpdate()
 {
     if (active && !isAi)
         rb.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);



 }

 private void LateUpdate()
 {
     if (keepBonesStraight)
         foreach (Transform t in bones)
             t.eulerAngles = new Vector3(0, t.eulerAngles.y, t.eulerAngles.z);
 }
 private void addZoneScore()
 {
     if (isInZone)
     {
         if (isAi)
         {
             score++;
         }
         else
         {
             score++;
             Transform t = Instantiate(scoreFx, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);

             TextMeshPro txt = t.GetComponent<TextMeshPro>();
             txt.DOFade(0, .5f).SetDelay(.5f);
             t.DOMoveY(t.position.y + 2, 1);
             Destroy(t.gameObject, 2);

         }
     }
 }

 public void addKnockOutScore()
 {
     if (isInZone)
     {
         if (isAi)
         {
             score += 4;
         }
         else
         {
             score += 4;

             Transform t = Instantiate(scoreFx, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);
             TextMeshPro txt = t.GetComponent<TextMeshPro>();
             txt.text = "+4";
             txt.color = Color.yellow;
             txt.DOFade(0, .5f).SetDelay(.5f);
             t.DOMoveY(t.position.y + 2, 1);
             t.DOPunchScale(new Vector3(.5f, .5f, .5f), .8f);
             Destroy(t.gameObject, 2);
         }
     }
 }

 private Transform getClosestEnemyInZone(List<Transform> enemies)
 {
     Transform bestTarget = null;
     float smallestDistance = Mathf.Infinity;
     Vector3 currentPosition = transform.position;
     foreach (Transform potientialTarget in enemies)
     {
         if (potientialTarget != transform)
         {
             Vector3 directionToTarget = potientialTarget.position - currentPosition;
             float distance = directionToTarget.sqrMagnitude;

             if (distance < smallestDistance)
             {
                 smallestDistance = distance;
                 bestTarget = potientialTarget;
             }
         }
     }

     return bestTarget;
 }

 private Transform getClosestEnemy(easyplayer[] enemies)
 {
     Transform bestTarget = null;
     float smallestDistance = Mathf.Infinity;
     Vector3 currentPosition = transform.position;

     foreach (easyplayer potientialTarget in enemies)
     {
         if (potientialTarget.transform != transform)
         {
             Vector3 directionToTarget = potientialTarget.transform.position - currentPosition;
             float distance = directionToTarget.sqrMagnitude;

             if (distance < smallestDistance)
             {
                 smallestDistance = distance;
                 bestTarget = potientialTarget.transform;
             }
         }
     }

     return bestTarget;
 }
 public void punch(Transform other)
 {
     if (canPunch)
     {
         canPunch = false;
         easyanim.SetBool("eAttack", true);
         StartCoroutine(resetPunch());

         easyplayer tempeasyplayerScript = other.GetComponent<easyplayer>();
        tempeasyplayerScript.puncher = transform;
          tempeasyplayerScript.StartCoroutine(tempeasyplayerScript.stun());


         Rigidbody _rb = other.GetComponent<Rigidbody>();
         _rb.velocity = transform.forward * punchForce;
     }
 }

 private IEnumerator resetPunch()
 {
     yield return new WaitForSeconds(.1f);
     keepBonesStraight = false;
     punchFx.SetActive(true);

     yield return new WaitForSeconds(.25f);
     keepBonesStraight = true;
     punchFx.SetActive(false);
     canPunch = true;
     easyanim.SetBool("eAttack", false);
 }

 public IEnumerator stun()
 {
     stunned = true;
     ParticleSystem.EmissionModule em = easytrailFx.emission;
     em.enabled = false;

     if (isAi)
     {
         canPunch = false;
         nav.enabled = false;
         punchSfx.Play();
     }
     else
     {
         speed = 0;
     }

     yield return new WaitForSeconds(.25f);

     if (Vector3.Distance(Vector3.zero, new Vector3(transform.position.x, 0, transform.position.z)) < 23f)
     {

         rb.velocity = Vector3.zero;
         stunned = false;
         em.enabled = true;

         if (isAi)
         {
             canPunch = true;
             nav.enabled = true;
         }

         else
         {
             speed = defaultSpeed;
         }

     }

     else
     {
         if (puncher)
         {
             puncher.GetComponent<easyplayer>().addKnockOutScore();
             puncher = null;
         }

         StartCoroutine(death());
     }
 }

 private IEnumerator death()
 {
     yield return null;
 }

 public void stop(bool won)
 {
     active = false;
     ParticleSystem.EmissionModule em = easytrailFx.emission;
     em.enabled = false;

     easyanim.SetBool("eRun", false);

     if (isAi)
         nav.enabled = false;
     else
         speed = 0;


     if (won)
     {
         easyanim.SetBool("eWon", true);
     }
     else
     {
         easyanim.SetBool("eLost", true);
     }

 }

}

Comment
Add comment · Show 2 · 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 azerty0220pl · Apr 10, 2020 at 09:44 AM 0
Share

I do not know what is the problem. However, according to Unity documentation, it would be better not to modify Rigidbody.velocity directly. They recommend using AddForce.

avatar image Masrura azerty0220pl · Apr 10, 2020 at 02:12 PM 0
Share

Thanks for your response.

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

124 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

Related Questions

Punch animation problem 0 Answers

De-Activating object removes rigidbody velocity 1 Answer

How to make hand stop at the wall when punching? (Weird fast stretching/rotations) 0 Answers

How can I let my player speed go up and down smoothly? 1 Answer

Game behaves differently in build version 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