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 /
  • Help Room /
This question was closed Nov 17, 2016 at 11:38 PM by hexagonius.
avatar image
0
Question by NandoDine · Nov 17, 2016 at 07:18 AM · aim

Help with a script (JavaScript) :D

I have a problem, the ENEMY is following the movements of the camera of the player, I wanted to make the enemy look directly at the player / camera of the player, which would be the "soldierCamera" but he is not targeting the player, Player camera moves

That is, if the player looks at the side the enemy also looks in the same direction, the intention is to make him look at the player

Someone there to give me an idea?

 var soldierCamera : Transform;
 var headRotationFix : Vector3 = Vector3(180,90,90);
 var crosshair : Transform;
 
 
 private var target : Vector3;
 private var targetTarget : Vector3; //The target for Mathf.Lerp to the target.
 private var rightUpperArm : Transform;
 private var rightClavicle : Transform;
 private var leftUpperArm : Transform;
 private var leftClavicle : Transform;
 private var leftForearm : Transform;
 private var leftFinger : Transform;
 private var spine1 : Transform;
 private var spine2 : Transform;
 private var head : Transform;
 private var neck : Transform;
 private var cameraYRotation : float;
 private var cameraPitch : float;
 private var torsoOffsetAngle : float;
 private var torsoOffsetPitch : float;
 private var leanHead : float = 0.0;
 private var soldierAnimationScript : soldierAnimation;
 private var leftArmIkScript : MonoBehaviour;
 private var ikArm : Transform;
 private    var ikUpperArm : Transform;
 private    var ikForearm : Transform;
 private var transition : float;
 private var transitionTarget : float; // 0 means normal animation, 1 means complete aim.
 private var transition2 : float;
 private var transition2Target : float; // 0 means normal animation, 1 means complete aim. For head and torso.
 
 function Start(){
     spine1 =  transform.Find("mech_bot/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1");
     spine2 =  transform.Find("mech_bot/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1/Bip01 Spine2");
     neck = spine2.Find("Bip01 Neck");
     rightClavicle = transform.Find("mech_bot/Bip01/Bip01 Pelvis/Bip01 Spine/Bip01 Spine1/Bip01 Spine2");
     rightUpperArm = rightClavicle.Find("Bip01 Neck");
     leftClavicle = neck.Find("Bip01 L Clavicle");
     leftUpperArm = leftClavicle.Find("Bip01 L UpperArm");
     leftForearm = leftUpperArm.Find("Bip01 L Forearm");
     leftFinger = leftForearm.Find("Bip01 L Hand/Bip01 L Finger2/Bip01 L Finger21");
     head = neck.Find("Bip01 Head");
     soldierAnimationScript = GetComponent("soldierAnimation");
     var upperArmLength : float = Vector3.Distance(leftUpperArm.position, leftForearm.position);
     var forearmLength : float = Vector3.Distance(leftForearm.position, leftFinger.position);    
     
 }
 
 function LateUpdate(){
     //Target.
     var targetRay : Ray = soldierCamera.GetComponent.<Camera>().ViewportPointToRay(crosshair.position);
     var targetRayDistance : float = 300.0;
     var targetHit : RaycastHit;
     targetTarget = targetRay.origin + targetRay.direction * targetRayDistance;
 
     target = Vector3.Lerp(target,targetTarget,Time.deltaTime * 10.0);
     var aimAid : Transform = new GameObject("aidAim").transform; //This will be used to aid the body parts aim in the rigth direction.
     //Transition 1(arms).
     transitionTarget = 1.0;
     //Store pre-rotations.
     var rightUpperArmLocalRotation : Quaternion = rightUpperArm.localRotation;
     var leftUpperArmLocalRotation : Quaternion = leftUpperArm.localRotation;
     var leftForearmLocalRotation : Quaternion = leftForearm.localRotation;
     var headLocalRotation : Quaternion = head.localRotation;
     var spine1LocalRotation : Quaternion = spine1.localRotation;
     var spine2LocalRotation : Quaternion = spine2.localRotation;
     //Aiming.
     var characterYRotation : float = transform.rotation.eulerAngles.y;
     var spineYRotation : float = spine1.rotation.eulerAngles.y + 60;
     if (soldierCamera != null){
         cameraYRotation = soldierCamera.rotation.eulerAngles.y;
         cameraPitch = soldierCamera.localRotation.eulerAngles.x;
     }
     var deltaTorsoAngle : float = Mathf.DeltaAngle(spineYRotation, cameraYRotation);
     torsoOffsetAngle = Mathf.Lerp(torsoOffsetAngle, deltaTorsoAngle, Time.deltaTime * 15.0);
     var deltaTorsoPitch : float = Mathf.DeltaAngle(0, cameraPitch);
     torsoOffsetPitch = Mathf.Lerp(torsoOffsetPitch, deltaTorsoPitch, Time.deltaTime * 15.0);
     spine1.localRotation.eulerAngles.x -= torsoOffsetAngle * .5;
     spine2.localRotation.eulerAngles.x -= torsoOffsetAngle * .5;
     spine1.localRotation.eulerAngles.z -= torsoOffsetPitch * .5;
     spine2.localRotation.eulerAngles.z -= torsoOffsetPitch * .5;
     aimAid.position = rightUpperArm.position;//Right arm.
     var gunAim : Transform = rightUpperArm.Find("Bip01 R Forearm/Bip01 R Hand/gun/gunAim");
     aimAid.LookAt(gunAim);
     rightUpperArm.parent = aimAid;
     aimAid.LookAt(target);
     rightUpperArm.parent = rightClavicle;
     aimAid.position = leftUpperArm.position;//Upper arm.
     aimAid.LookAt(leftForearm);
     leftUpperArm.parent = aimAid;
     var gunGrab : Transform = rightUpperArm.Find("Bip01 R Forearm/Bip01 R Hand/gun/gunGrab");
     aimAid.LookAt(gunGrab);
     leftUpperArm.parent = leftClavicle;
     //IK. (Left arm).
     var leftElbowTarget : Transform = spine2.Find("leftElbowTarget");
     Destroy(aimAid.gameObject);
     if (soldierCamera != null){ //Head.
         head.LookAt(target);
         head.Rotate(headRotationFix);
         //Lean head  so it doesn't intersects with the gun.
         var minHeadLeanAngle : float = 0.0;
         var maxHeadLeanAngle : float = 120.0;
         var fullLeanAngle : float = 20;
         var leanHeadTarget : float =  0.0;
         if (head.localRotation.eulerAngles.x > minHeadLeanAngle && head.localRotation.eulerAngles.x < maxHeadLeanAngle){
             leanHeadTarget = head.localRotation.eulerAngles.x - minHeadLeanAngle; //Angle for leaning the head.
             leanHeadTarget /= maxHeadLeanAngle;
             leanHeadTarget *= fullLeanAngle;
         }
         leanHead = Mathf.Lerp(leanHead, leanHeadTarget, Time.deltaTime * 5.0);
         head.Rotate(0,leanHead,0);
     }
     //Transition 1.
     transition = Mathf.Lerp(transition, transitionTarget, Time.deltaTime * 5.0);
     if(transition < 1.0){ //Only Lerp if transitionTarget is between 0 and 1.
         rightUpperArm.localRotation = Quaternion.Lerp(rightUpperArmLocalRotation, rightUpperArm.localRotation, transition);
         leftUpperArm.localRotation = Quaternion.Lerp(leftUpperArmLocalRotation, leftUpperArm.localRotation, transition);
         leftForearm.localRotation = Quaternion.Lerp(leftForearmLocalRotation, leftForearm.localRotation, transition);
         
     }
     //Transition 2.
     transition2 = Mathf.Lerp(transition2, transition2Target, Time.deltaTime * 5.0);
     if(transition2 < 1.0){ //Only Lerp if transitionTarget is between 0 and 1.
         head.localRotation = Quaternion.Lerp(headLocalRotation, head.localRotation, transition2);
         spine1.localRotation = Quaternion.Lerp(spine1LocalRotation, spine1.localRotation, transition2);
         spine2.localRotation = Quaternion.Lerp(spine2LocalRotation, spine2.localRotation, transition2);
     }
 }


Thanks in advance!

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

  • Sort: 

Follow this Question

Answers Answers and Comments

77 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

Related Questions

Target lead (aim ahead) problem 1 Answer

instantiate projectiles towards player 0 Answers

3D, Rotate gun towards mouse 0 Answers

Rotation Transformation From Hell 0 Answers

Controlling player with a mouse 2 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