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
1
Question by IceMan- · Mar 17, 2017 at 09:55 PM · c#raycastquaternionlookrotationrotatetowards

Quaternion LookRotation problems while moving up hill.

UPDATE: I have made a list of what I need help with. When I get one of these to work it will break the other. I need the things on this list to work together at the same time.

  1. I need the turret to stay attached to the tank properly while going up / down hill.

  2. I need the turret and gun to follow the camera's raycast and line up accurately while going up / down hill.

  3. I need to be able to control the rotation speed of the turret.

Hello, I am having a few problems with a LookRotation that follows a camera's raycast. I am controlling a tank's turret and gun with a Raycast from the center of a camera, so the turret and gun look where the camera looks. When I am on flat ground the turret pivot and gun pivot look at the camera's raycast correctly, but the first problem I encountered was driving up a angled surface. The turret and gun axis would break. I fixed this issue by adding a line of code which 0s out the euler angles for the rotation on the x and z axis (As I only care about Y on the turret). After doing that it messed up the accuracy of the Quaternion.LookRotation. Now when I drive up a hill the rotation of the turret pivot and gun pivot is not lining up which is causing the accuracy to be off. I have added my code and some screen shots of my problems. I need help to stop the turret from flipping off its axis while keeping the accuracy of the lookrotation tracking the camera's movement when going up hill. Thank you.

alt text

alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraRaycast : MonoBehaviour
 {
     public Transform TurretPivot;
     public Transform GunPivot;
     public float TurretRotationSpeed;
     public float GunRotationSpeed;
     private RaycastHit Hit;
  
     void Start ()
     {
         TurretPivot = TurretPivot.GetComponent<Transform>();
         GunPivot = GunPivot.GetComponent<Transform>();
 
     }
     
     void Update ()
     {
         TurretFollowCameraRay();
         GunFollowCameraRay();
     }
 
     void TurretFollowCameraRay()
     {
 
         if (Physics.Raycast(transform.position, transform.forward, out Hit))
         {
             Vector3 CameraRayDirection = Hit.point - TurretPivot.transform.position;
             Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
             TurretPivot.transform.rotation = Quaternion.RotateTowards(TurretPivot.transform.rotation, RotateToPosition, TurretRotationSpeed * Time.deltaTime);
 
             // This line of code fixes the turret's pivot axis, but breaks the accuracy of the LookRotation.
             TurretPivot.localEulerAngles = new Vector3(0,TurretPivot.localEulerAngles.y, 0);
         }
     }
 
     void GunFollowCameraRay()
     {
         if (Physics.Raycast(transform.position, transform.forward, out Hit))
         {
             Vector3 CameraRayDirection = Hit.point - GunPivot.transform.position;
             Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
             GunPivot.transform.rotation = Quaternion.RotateTowards(GunPivot.transform.rotation, RotateToPosition, GunRotationSpeed * Time.deltaTime);
 
             // This line of code fixes the gun's pivot axis, but breaks the accuracy of the LookRotation.
             GunPivot.localEulerAngles = new Vector3(GunPivot.localEulerAngles.x, 0, 0);
         }
     }
 }
 







image2.jpg (460.4 kB)
image3.jpg (457.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jackishere · Mar 18, 2017 at 01:44 PM

Your code really rely on raycast so there's chance that your raycast might not hit anything when aiming up,

you can try add invisible collider on the roof or pass a hit point if the raycast did not hit anything

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 IceMan- · Mar 19, 2017 at 08:19 AM 0
Share

I am having these problems even when the raycast is hitting objects.

avatar image jackishere IceMan- · Mar 19, 2017 at 03:14 PM 0
Share

Hey is there's anyway i can get the $$anonymous$$imal sample of the project so i can help you better?

avatar image
0

Answer by IgorAherne · Mar 18, 2017 at 01:15 AM

TurretPivot.transform.rotation redundand code. You already declared TurretPivot as Transform. just use TurretPivot.rotation instead. Otherwise it's like this.gameObject.transform.transform.rotation

Now to the problem:

just use:

 //specify rotation, where second argument is what should be the "upwards dir of the object"
 Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection, TurretPivot.up);

if you are using turret.FBX, try .right instead


for Gun use GunPivot.right, because it rotates around its local right axis

And then dispose of the y-angles code and the x-angles code you were using.

Comment
Add comment · Show 3 · 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 OzzyDE · Mar 18, 2017 at 01:58 AM 0
Share

IgnorAherne by default if you do not specify the 2nd argument in LookRotation it is classed as Vector3.up so unless he got the wrong axis that is ok coding.

The fact it works on ground but not on the angle is confusing to me as it seems the code is using is correct to zero out both axis.

avatar image IgorAherne OzzyDE · Mar 18, 2017 at 06:40 PM 0
Share

yes, but it's Vector3.up by default, which is different to transform.up (the local up of an object). For example, if we treat Earth's north as the Vector3.up, my up direction in U$$anonymous$$ is different to the Vector3.up, and is different to the up direction to someone on the opposite side of Earth.

avatar image IceMan- · Mar 19, 2017 at 07:07 PM 1
Share

I tried the code you suggested and it did not fix the problem. The turret still has the problem of not staying attached to the tank. I need help with three things.

  1. I need the turret to stay properly attached to the tank when going up/down hill.

  2. I need the turret and gun to accurately follow the camera's raycast and line up properly.

  3. I need to be able to control the rotation speed of the gun and turret.

I need to have those three things work together. I have only been able to get them to work one at a time and then they break each other.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Child transforms screwed up by parent's rotations 1 Answer

Rotate object towards another object without using the local Y axis 0 Answers

to rotate along z axis 1 Answer

Quaternion.Slerp with Quaternion.LookRotation causes unexpected results 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