Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 stanwijnen99 · Jan 26, 2021 at 04:36 PM · beginnermovement script

I need help with my script for my squirrel

Hey Unity!

I am currently working on my first little game where the player is a squirrel. And I have gotten stuck with my player movement script.

The goal of my script is: - The squirrel needs to be aligned with the ground/object it is climbing. (see the CastRay()) - The squirrel needs to be able to climb in trees (and maybe eventually the bad guys) and hang upside down from branches.

My current problems are: - I want the forward of the squirrel gameobject based on the walking direction in relation to the camera and the slope it is walking on. So walking backwards (towards the camera) turns the squirrel gameobject 180 degrees. Even on sloped surfaces. - I am able to let the squirrel walk around a horizontal cylinder, but when it reaches a 90 degree forward it gets stuck. Walking backwards works fine.

If anything is unclear or you maybe have a suggestion, please let me know!

Here is my full code:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     Animator animator;
     CharacterController controller;
     [SerializeField] Transform Cam;
     [SerializeField] Transform surfaceCheck;
     [SerializeField] Transform render;
     Transform activeCamera;
 
     [SerializeField] LayerMask surfaceMask;
 
     [SerializeField] float turnSpeed = 0.1f;
     [SerializeField] float walkSpeed = 5f;
     [SerializeField] float gravity = -9.81f;
     [SerializeField] float RaycastLength = 0.7f;
     [SerializeField, Range(0.5f, 1f)] float RaycastSpread = 0.7f;
 
     Quaternion rot;
 
     float surfaceDistance = 0.05f;
     float turnSmoothVelocity;
 
     [SerializeField] bool applyGravity = true;
     bool isGrounded;
 
     Vector3 gVelocity;
     Vector3 rotationDir;
     Vector3 normal;
 
     void Awake()
     {
         animator = GetComponent<Animator>();
         controller = GetComponent<CharacterController>();
 
         activeCamera = Cam;
 
 
         rotationDir = transform.eulerAngles;
         rot = Quaternion.Euler(rotationDir.x, rotationDir.y, rotationDir.z);
     }
 
     private void Update()
     {
         //Get input
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
         Vector3 dir = new Vector3(horizontal, 0f, vertical).normalized;
 
         bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
         bool hasVerticalInput = !Mathf.Approximately(vertical, 0f);
         bool isWalking = hasHorizontalInput || hasVerticalInput;
 
         Vector3 _rotationDir = rotationDir.normalized;
         //transform.RotateAround(transform.position, transform.up, 50 * Time.deltaTime);
         if (isWalking)
         {
             //transform.rotation = Quaternion.Euler(rotationDir.x, transform.rotation.y, rotationDir.z);
             //Rotate the moveDir to the same plane as rotationDir
             Vector3 _dir = rot * dir;
 
             float targetAngle = Mathf.Atan2(_dir.x, _dir.z) * Mathf.Rad2Deg;
             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSpeed);
             //render.transform.rotation = Quaternion.Euler(0f, angle, 0f);
 
             //Vector3 moveDir = (Quaternion.Euler(0f, targetAngle, 0f));
             controller.Move(_dir.normalized * walkSpeed * Time.deltaTime);
 
         }
         CastRay(this.transform);
         Gravity(applyGravity);
 
         animator.SetBool("isWalking", isWalking);
     }
     private void OnDrawGizmos()
     {
         Gizmos.DrawSphere(surfaceCheck.position, surfaceDistance);
     }
     private void Gravity(bool applyGrav)
     {
         if (!applyGrav)
             return;
 
         isGrounded = Physics.CheckSphere(surfaceCheck.position, surfaceDistance, surfaceMask);
 
         if (isGrounded && gVelocity.y < 0)
         {
             gVelocity.y = -0.01f;
         }
         else
         {
             gVelocity.y += gravity * Time.deltaTime;
         }
         Vector3 gravDir = rot * gVelocity;
         controller.Move(gravDir * Time.deltaTime);
     }
 
     void CastRay(Transform castpoint)
     {
         LayerMask maskPlayer = LayerMask.GetMask("Player");
         maskPlayer = ~maskPlayer;
 
         RaycastHit hit;
         for (float castAngle = 0; castAngle > -RaycastSpread; castAngle -= 0.1f)
         {
             Vector3 castDir = rot * (new Vector3(0f, castAngle, 1f));
             Debug.DrawRay(castpoint.transform.position, castDir * RaycastLength, Color.cyan);
 
             if (Physics.Raycast(castpoint.transform.position, castDir, out hit, RaycastLength, maskPlayer))
             {
                 if (normal != hit.normal)
                 {
                     normal = hit.normal;
 
                     transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
                 }
                 //Debug.Log("hit");
                 Debug.DrawRay(castpoint.transform.position, castDir * hit.distance, Color.yellow);
                 Debug.DrawRay(hit.point, normal * 2f, Color.red);
 
 
             }
         }
     }
 }








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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sab_Rango · Jan 27, 2021 at 12:23 PM

Hey ! I think

the line :: float targetAngle = Mathf.Atan2(_dir.x, _dir.z) * Mathf.Rad2Deg;

need to be changed into :::::::::::::::: ::float targetAngle = Mathf.Atan2(_dir.x, _dir.z) * Mathf.Rad2Deg+camera.eulerAngles.y ;

Although, I don't know the main script issue, the problem is properly rotating the Character controller.

I also made several mistakes my 1st Char. cont, even watching Brackeys char. cont. tutorial !!

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

129 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

Related Questions

How to move the other object without moving the first object? 1 Answer

Issues with player movement 1 Answer

How to move the object to where the object is already pointing to? 1 Answer

Canvas Movement Code Being Disabled 2 Answers

Movement Script Results In Character Spinning Around 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