After making an object follow the cursor on the screen and make the camera tilt according to the cursor left-right
(the way I control the object) (I can't put a picture... So I put it as a link) Pictures of the objects in the event
***Player***
= Game forward object
Animation Camera
= The object used for rotation, moves from the parent position. and use it as a camera animation
Main Camera
= Ah yes, it's a camera
now this is my code But here, in order to make it easier to read So I excluded the irrelevant part
This code I added to ***Player***
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class move : MonoBehaviour
{
float speed;
public float SetSpeed = 10.00f;
public bool game = false;
public bool CentrifugalForce = true;
public string GameMode = "Pxy";
Vector3 PointOfPlayer = new Vector3(0, 0, 0);
Vector3 PlayerPosition = new Vector3(0, 0, 0);
Vector3 movement = new Vector3(0, 0, 0);
Vector2 PointOfMouseOnGame = new Vector3(0, 0);
Vector2 Move;
Vector2 Mouse;
Vector2 ScreenSet;
void Start()
{
ScreenSet = new Vector2(Screen.width / 2,
Screen.height / 2);
PlayerPosition = new Vector3(0, 0, 0);
}
void Update()
{
if (game == true)
{
Mouse = new Vector2(Input.mousePosition.x,
Input.mousePosition.y);
Move = new Vector2(Mouse.x - ScreenSet.x,
Mouse.y - ScreenSet.y);
PointOfPlayer = transform.position; //current position----------
PointOfMouseOnGame = new Vector2(Move.x, Move.y);
speed = SetSpeed;
movement = Vector3.forward * Time.deltaTime;
PlayerPosition = movement * speed; //Player's position in the next frame----------
if (GameMode == "Pxy")
{
float x = (PointOfPlayer.x - PointOfMouseOnGame.x) / 2;
float y = (PointOfPlayer.y - PointOfMouseOnGame.y) / 2;
PlayerPosition = new Vector3(x / 100, y / 100);
}
if (CentrifugalForce == true)
{
transform.localRotation = Quaternion.Euler(Input.mousePosition.y / 100, transform.position.x * -2, transform.position.x * -2.5f) ;
}
//----- Final Procress -----//
transform.position = new Vector3(PlayerPosition.x,
PlayerPosition.y,
PlayerPosition.z);
//----- Final Procress -----//////////
}
}
}
Sorry for my poor organization...
In fact, I've tried doing smooth mouse movements on Scratch
and it worked. So I tried to do it on Unity but it moved normally. or is it ineffective
Maybe it's because Scratch's framerate is only 32 frames...
Example Code image from Scratch
thanks in advance
CodePudding user response:
Quaternion.Slerp
is a great way of interpolating between rotations. It gets a point between a
and b
by t
.
t is basically where inbetween the rotations it should output. We can use Time.deltaTime, so it gets slightly ahead of a, to b every frame. a will change to the current rotation, so it doesn't return the same point every time. You can add a speed, so it smooths it auicker or slower.
Here is an example code:
public float smoothSpeed = 1f;
...
if (CentrifugalForce == true)
{
Quaternion a = transform.localRotation;
Quaternion b = Quaternion.Euler(Input.mousePosition.y / 100, transform.position.x * -2, transform.position.x * -2.5f);
float t = Time.deltaTime * smoothSpeed;
Quaternion smooth = Quaternion.Slerp(a, b, t);
transform.localRotation = smooth;
}
Let me know in coments if there are any problems! :)