I have a ball which we can throw here and there in my game. I want my cinemachine vcam to move only in the x direction with the ball. And just look up to the ball while its in air. I don't want the camera to move in y direction along it too.
I have set the look at and follow fields to the ball only. All i want it the camera to not follow the ball in y. (Basically, follow the ball in x, and keep looking at it in the air)
How can I achieve this is the most trivial and beautiful way possible? (open to scripts, but the lesser code the better)
as i've just tested, You can write a script that attaches to your camera, where you create a new vector3 with the x of your ball, and the frozen Y values that you want here's an exemple:
public class CameraController : MonoBehaviour
{
//Assign your ball in the inspector
public Transform target;
// Update is called once per frame
void Update()
{
//Here i assumed that you want to change the X
Vector3 newCamPosition = new Vector3(target.position.x, yourValue, yourValue);
gameObject.transform.position = newCamPosition;
}
}
Hope It helped <3
Related
I have a script that faces my player (cube) to its moving direction. It also has a grappling gun, but when he turns to its moving direction, gun turns too and it can't use grappling gun when i try to grapple and object staying on the opposite site of my player.
This is how i turn my player:
private void Flip()
{
if (isFacingRight && HorizontalInput() < 0f || !isFacingRight && HorizontalInput() > 0f)
{
Vector3 localScale = transform.localScale;
isFacingRight = !isFacingRight;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
If I can fix the localScale of my gun, so that it won't multiplied with -1 when i multiply the whole body.
Any idea? Thanks a lot.
As I understand the question: you want to flip the player but not flip the gun (which is a child of the player).
One way of doing this is to organize the Player object like this:
-Player (prefeb, invisible)
--PlayerGraphicChild (Cube, flip this one only)
--PlayerGunChild (don't flip this one).
Another way is to add a new prefeb called Gun, and set the position of that object to one of the children of Player. Organize the hierarchy like this:
-Player (flip this one)
--PlayerGunDolly (flips with parent)
and create a new prefeb:
-Gun (has a component that sets the transform.position to transform of PlayerGunDolly. Does not flip.)
I'm trying to do a little game on mobile using Unity and I've got a problem with the rotation of a maze.
To add context :
When your moving your finger on the screen, the maze is rotating on himself. There is a ball in it and you need to make it go on a cell to win the level.
When the maze is rotating too fast, the ball falls down and go through the ground and I don't know how to fix it.
I tried to play with the gravity, colliders...
This is the same when the ball is jumping (when the maze is going up and down quickly).
For the moment I just reset the ball position when you're falling.
{
ball.transform.position = new Vector3(0, 2, 0);
maze.transform.position = Vector3.zero;
maze.transform.rotation = Quaternion.identity;
}
Do you guys have some ideas ? Thanks
I had a similar problem in a tilt maze mini-game I worked on. Ideally implementing jkimishere's solution will work but I assume the maze is moving too fast for the collisions to register properly. You'll need to smooth the maze's rotation with a Lerp. In our case we had pressure plates with a tilt value, so it doesn't directly translate to your mobile use but perhaps give you a nudge in the right direction. We used:
public GameObject maze;
private float _lerpTime;
private bool _isRotating;
private Quaternion _startingRot, _desiredRot;
private void Awake()
{
_startingRot = maze.transform.localRotation;
}
private void Update()
{
//Don't want to move the maze if we don't ask for it
if(!_isRotating)
return;
//Lerp the maze's rotation
_lerpTime = Mathf.Clamp(_lerpTime + Time.deltaTime * 0.5f, 0f, 1f);
maze.transform.localRotation = Quaternion.Lerp(_startingRot, _desiredRot, _lerpTime);
//Once the maze gets where it needs to be, stop moving it
if(affectedObject.transform.localRotation.Equals(_desiredRot)
_isRotating = false;
}
private void ActivateTilt()
{
//Set the new starting point of the rotation.
_startingRot = maze.transform.localRotation;
//However you want to calculate the desired rotation here
//Reset our lerp and start rotating again
_lerpTime = 0f;
_isRotating = true;
}
This will ease the rotation of your maze over time. So that the ball can adapt to the new collider positions.
In the rigidbody(for the ball), make the collision detection to continuous, and in the rigidbody for the maze(if you have one) set the collision detection to continuous dynamic. Hope this helps!
I think that is unavoidable if you allow the player to move the platform freely. I suggest you restrain the speed at wich the player can tilt the platform. This way, the ball will have more frames to adapt to the new slope
I'm trying to learn Unity by myself. I'm recreating pong in 3d with Unity objects. I started a few minutes ago and every time I throw any input into the pad its y coordinate shifts to 2.6, I have no idea why. Can someone help?
public class PadMovement : MonoBehaviour {
private CharacterController pad;
private Vector3 direction;
private Vector3 movement;
[SerializeField] private float speed = 50.0f;
// Use this for initialization
void Start() {
pad = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update() {
direction = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
movement = direction * speed;
pad.Move(movement * Time.deltaTime);
}
}
SOLVED: there was a capsule collider out of place!
Afternoon, I recently copied your code into a 'Unity3d', ".cs", File and actually I created a cube and placed my game into a 2d mode, After this I named my file "PadMovement", after that I dragged it onto my newly created cube, Once I had done that I tried to click play and noticed that my cube didn't have a "CharacterController", attached to my cube, Once I had done that and clicked play I was eligible to have my "paddle", smoothly move around the screen # 50.0f along the X axis.
Knowingly, My Input came from the "Character Controller", My Speed came from the Serial field you had for speed!
Do you by any chance have a CapsuleCollider Component or some other collider on the same GameObject that you have this PadMovement Component on? That sounds like a height where it might just be trying to pop the object out of ground collision.
It should be harmless if that's all it is. If you really want an object to be at y of 0 you can attach it to a parent object and have that parent stay at 0.
I am very new to Unity and just got done yesterday following the Roller Ball example on the learn page here at Unity3d.
To practice what I have learned I wanted to try and recreate something similar using my own art and making the game different. I have been playing around with Voxel Art and I am using MagicaVoxel to create my assests. I created the walls, the ground etc.. and all is well.
Then came the player object, the sphere. I created one as close to a sphere as possible with magicaVoxel and it rolls fine. However, when using a script to have the camera follow the object it runs into issues.
If I don't constrain the Y axis then I will get bouncing and as far as the x and z axis I get kind of a Flat Tire effect. Basically the camera doesn't follow smoothly it bounces around, stop go etc...
I have tried making the collider larger then the sphere and even using the position of the collider vs the object itself. I have also tried putting the code in Update / FixedUpdate / LateUpdate. What is the proper way to fix or address something like this? Here is my scripts below:
Camera Controller:
public class CamController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
// Get the distance between the player ball and camera.
offset = this.transform.position - player.transform.position;
}
void LateUpdate ()
{
this.transform.position = player.transform.position + offset;
}
}
Player Controller:
public class PlayerController : MonoBehaviour {
public float _speed;
void FixedUpdate()
{
// Get input from keyboard.
float _hoz = Input.GetAxis("Horizontal");
float _ver = Input.GetAxis("Vertical");
// Create a vector3 based on input from keyboard.
Vector3 _move = new Vector3(_hoz, 0.0f, _ver);
// Apply force to the voxel ball
this.GetComponent<Rigidbody>().AddForce(_move * _speed);
}
}
Thanks for any help in advance.
You can use the SmoothFollow Script of Unity it self for getting smooth follow of camera.
Here are the steps how you can get the script:
1) Assets->Import Package->Scripts.
2) At the dialog that appears select all the scripts, or just the smooth follow one and hit Import button.
3) Now this script is in your project, and you can attach it to the camera.
Hope this will help you...
Best,
Hardik.
How do we achieve a control similar to this game?
https://play.google.com/store/apps/details?id=com.fridgecat.android.atiltlite&hl=en
You can do this with builtin physics:
create a level from some simple scaled cubes (don't forget the ground).
add the ball - a sphere, then and add a RigidBody to it. Set a constraint on the rigidbody - check freeze position y (or it will be able to jump out of the level if you put the device upside down).
add this script anywhere on the scene (for example to the camera):
using UnityEngine;
public class GravityFromAccelerometer : MonoBehaviour {
// gravity constant
public float g=9.8f;
void Update() {
// normalize axis
Physics.gravity=new Vector3(
Input.acceleration.x,
Input.acceleration.z,
Input.acceleration.y
)*g;
}
}
or if you want the physics to just affect this one object, add this script to the object and turn off it's rigidbody's affected by gravity:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class ForceFromAccelerometer : MonoBehaviour {
// gravity constant
public float g=9.8f;
void FixedUpdate() {
// normalize axis
var gravity = new Vector3 (
Input.acceleration.x,
Input.acceleration.z,
Input.acceleration.y
) * g;
GetComponent<Rigidbody>().AddForce (gravity, ForceMode.Acceleration);
}
}
And now you have working ball physics. To make the ball behave as you'd like, try to play with the rigidbody properties. For example, change the drag to something like 0.1.