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 /
  • Help Room /
avatar image
0
Question by SAONCE · Mar 20, 2021 at 05:42 PM · playercharactercharacter.move

deviation as the character speeds up,I have a character, it gets faster as time passes. However, after a while, the character begins to tremble, will you help

My character is misguiding in the game as time passes. Can you help me?

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerMotor : MonoBehaviour
 {
     private const float LANE_DISTANCE = 2.3f;
     private const float TURN_SPEED = 0.05f;
     // Func
     private bool isRunning = false;
     // Falling 
 
     // Animation
     private Animator anim;
 
     //Movement
     private CharacterController controller;
     private readonly float jumpForce = 4.70f;
     private readonly float gravity = 12.0f;
     private float verticalVeloCity;
     private int desiredLane = 1; //0=left,1=middle,2=right
 
     // Speed Modifier
     private readonly float originalSpeed = 7.0f;
     private float speed;
     private float speedIncreaseLastTick;
     private readonly float speedIncreaseTime = 2.5f;
     private readonly float speedIncreaseAmaount = 0.10f;
     private void Start()
     {
         speed = originalSpeed;
         controller = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
     private void Update()
     {
         if (!isRunning)
             return;
 
         if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
         {
             speedIncreaseLastTick = Time.time;
             speed += speedIncreaseAmaount;
             // Change Modifer Text
             GameManager.Instance.UpdateModifer(speed - originalSpeed);
 
         }
         // Gather the inputs on whic lane we should be 
         if (MobileInput.Instance.SwipeLeft)
             MoveLane(false);
         if (MobileInput.Instance.SwipeRight)
             MoveLane(true);
 
         // Calculate where we should be in the future 
         Vector3 targetPosition = transform.position.z * Vector3.forward;
         if (desiredLane == 0)
             targetPosition += Vector3.left * LANE_DISTANCE;
 
         else if (desiredLane == 2)
             targetPosition += Vector3.right * LANE_DISTANCE;
 
 
         // Let's calcu our move delta 
 
         Vector3 moveVector = Vector3.zero;
         moveVector.x = (targetPosition - transform.position).normalized.x * speed;
         bool isGrounded = IsGrounded();
         anim.SetBool("Grounded", isGrounded);
 
         // Calculate Y 
 
         if (isGrounded)// İf Grounded
         {
             verticalVeloCity = -0.1f;
             if (MobileInput.Instance.SwipeUp)
             {
                 //JUMP
                 anim.SetTrigger("jump");
                 verticalVeloCity = jumpForce;
             }
             else if (MobileInput.Instance.SwipeDown)
             {
                 // Slide
                 StarSliding();
                 Invoke(nameof(StopSliding), 1.0f);
             }
 
 
         }
         else
         {
             verticalVeloCity -= (gravity * Time.deltaTime);
             // Fast falling mechaning 
             if (MobileInput.Instance.SwipeDown)
             {
                 verticalVeloCity = -jumpForce;
             }
         }
         moveVector.y = verticalVeloCity;
         moveVector.z = speed;
         // Move the Robot
         controller.Move(moveVector * Time.deltaTime);
 
         // Rotate the rabbit to where is going 
 
         Vector3 dir = controller.velocity;
         if (dir != Vector3.zero)
         {
             dir.y = 0;
             transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
         }
 
 
     }
     private void StarSliding()
     {
         anim.SetBool("Sliding", true);
         controller.height /= 2;
         controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
     }
     private void StopSliding()
     {
         anim.SetBool("Sliding", false);
         controller.height *= 2;
         controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
     }
     private void MoveLane(bool goingRight)
     {
         desiredLane += (goingRight) ? 1 : -1;
         desiredLane = Mathf.Clamp(desiredLane, 0, 2);
 
     }
     private bool IsGrounded()
     {
         Ray groundRay = new Ray(new Vector3(controller.bounds.center.x, (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f, controller.bounds.center.z), Vector3.down);
         Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
 
 
         return Physics.Raycast(groundRay, 0.2f + 0.1f);
 
     }
     public void StartRunning()
     {
         isRunning = true;
 
         anim.SetTrigger("StartRunning");
     }
     private void Crash()
     {
         anim.SetTrigger("Death");
         isRunning = false;
         GameManager.Instance.OnDeath();
     }
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         switch (hit.gameObject.tag)
         {
             case "Obstacle":
                 Crash();
                 break;
 
         }
         switch (hit.gameObject.tag)
         {
             case "Falling":
                 Falling();
                 break;
         }
 
 
     }
 
 
     private void Falling()
     {
         anim.SetTrigger("Falling");
 
     }
 
 }
 


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 SAONCE · Mar 20, 2021 at 05:47 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Cameramotor : MonoBehaviour
 {
     public Transform lookAt; // Our Charcter// object we're looking at 
     public Vector3 offset = new Vector3(0, 5.0f, -10.0f);
     public Vector3 rotation = new Vector3(35, 0, 0);
     public bool IsMoving { set; get; }
 
     private void LateUpdate()
     {
         if (!IsMoving)
             return;
         Vector3 desiredPosition = lookAt.position + offset;
         desiredPosition.x = 0;
         transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(rotation), 0.1f);
     }
 }

There is another problem, the camera character cannot follow after a while, he falls behind, the character is too far ahead.

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

215 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Select character using click and work on same scrip 0 Answers

Top Down Character - Face direction of movement? 0 Answers

Character goes crazy when pressing play 1 Answer

Making camera follow the player the right way 0 Answers

How do i ajust the player height acording to the height of the roof in a first person game? 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