Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SrBilyon · Jan 26, 2013 at 02:33 PM · targetcircletargetingstrafing

Circle strafing/Z-Targeting

This was something that I always wanted to do but could never manage to emulate.

alt text

Pretty much from what I can see, the character's movement vector is modified to orbit around the target, while assuring that the camera should have the target at all times, but I'm not sure how to actual modify the movement vector to do so. From what I researched, it would require the character to move in a diagonal like fashion when locked on to a target.

This is all I have so far in my Late Update. At this point, the character faces the target, but the camera doesn't stay directly behind the player.

 void  LateUpdate ()
 {
     if (target) 
     {
         xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
         ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
  
         ClampAngle(ySmooth, yMinLimit, yMaxLimit);
 
         Quaternion rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
  
        // posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
         posSmooth = (target.position + player.position)/2; // no follow smoothing
 
         var playerLook = new Vector3(target.position.x, player.position.y, target.position.z);
         player.LookAt(playerLook);
 
 
         transform.rotation = rotation;
 
         transform.position = rotation * new Vector3(0.0f, 0.0f, -distance) + posSmooth;
         transform.LookAt((target.position + player.position)/2 + new Vector3(0,0,3));
     }
 }

Any help would be appreciated.

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 Harvizl · Mar 17, 2019 at 02:33 PM 0
Share

Hey, I have the Camera Slerping into a Vector3 position fixed behind the Player and Baked the Root Transform Rotation in the Animation Source so the Player GameObject doesn't rotate during the Attack Animation (causes the Vector3 position to make the Camera jitter).

// Smoothly move Camera Position into lockOnPosition child object of Player as per smoothSpeed cameraTransform.position = Vector3.Slerp(cameraTransform.position, lockOnPosition.position, smoothSpeed * Time.deltaTime);

I have the issue where I can't get the rolling movement to work (where it orbits around the locked on enemy). Would you $$anonymous$$d sharing yours with me? :)

1 Reply

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

Answer by SrBilyon · Jan 27, 2013 at 03:15 AM

I managed to make a similar camera by merging and modifying some of the basic camera scripts provided by Unity:

 using UnityEngine;
 using System.Collections;
 
 public class Orbit : MonoBehaviour
 {
     public Transform target, player;
     float distance = 10.0f;
 
     float xSpeed = 1;
     float ySpeed = 1;
 
     float yMinLimit = -20;
     float yMaxLimit = 80;
 
     private float x = 0.0f;
     private float y = 0.0f;
 
 
     float smoothTime = 0.3f;
 
     private float xSmooth = 0.0f;
     private float ySmooth = 0.0f;
     private float xVelocity = 0.0f;
     private float yVelocity = 0.0f;
 
     private Vector3 posSmooth = Vector3.zero;
     private Vector3 posVelocity = Vector3.zero;
 
     public float zOffset = 3;
 
 
     //@script AddComponentMenu("Camera-Control/Mouse Orbit smoothed")
 
     void Start()
     {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
     // The distance in the x-z plane to the target
     // the height we want the camera to be above the target
     public float height = 5.0f;
     // How much we 
     public float heightDamping = 2.0f;
     public float rotationDamping = 3.0f;
 
 
     void LateUpdate()
     {
         if (target)
         {
             xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
             ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
 
             ClampAngle(ySmooth, yMinLimit, yMaxLimit);
 
             posSmooth = (target.position + player.position) / 2; // no follow smoothing
 
             var playerLook = new Vector3(target.position.x, player.position.y, target.position.z);
             player.LookAt(playerLook);
 
             transform.LookAt(player.position);
 
             // Calculate the current rotation angles
             var wantedRotationAngle = player.eulerAngles.y;
             var wantedHeight = player.position.y + 2;
 
             var currentRotationAngle = transform.eulerAngles.y;
             var currentHeight = transform.position.y;
 
             // Damp the rotation around the y-axis
             currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
             // Damp the height
             currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
             // Convert the angle into a rotation
             var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
 
             // Set the position of the camera on the x-z plane to:
             // distance meters behind the target
             transform.position = player.position;
             transform.position -= currentRotation * Vector3.forward * distance;
 
             // Set the height of the camera
             transform.position = new Vector3(transform.position.x, height, transform.position.z);
 
             // Always look at the target
             transform.LookAt(player);
 
         }
     }
 
     static void ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         //return Mathf.Clamp (angle, min, max);
     }
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

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

10 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

Related Questions

When moving camera I like my circle Plane look at cam. 0 Answers

Rocket launcher script help 1 Answer

Camera re-target 2 Answers

Changing AI target, after destroying gameObject 1 Answer

revise enemy code to player code 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