Why does my Player in Unity fall down when I start PlayMode? - c#

Video explanation
My cube in Unity is falling down randomly.It is on a cube named "Ground" and it has a movement script attached below. Above is the video showing what happens, and I'll attach an image too. Image
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
controller.Move(direction * speed * Time.deltaTime);
}
}
}

Make sure the colider covers the entire cube, because now it’s only of size 1 on all axis.

Related

Why is my script for a sprite applying to a tilemap? (Unity2D)

I'm trying to make a simple top-down 2d movement demo as a personal project, and I've run into a very weird roadblock. I was using code from this website to move the sprite, and it worked fine. I then drew in a simple structure using tilemap to test movement and collision of the sprite, but now when I use the arrow keys it moves the structure I painted in with the tilemap in an inverted control scheme (up arrow is down, left arrow is right, etc.). I have the script I wrote for the test sprite attached below. Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerController : MonoBehaviour
{
Rigidbody2D body;
float horizontal;
float vertical;
Vector2 move;
public float runSpeed = 2.5f;
void Start()
{
body = GetComponent<Rigidbody2D>();
}
void Update ()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
//move.x = horizontal;
//move.y = vertical;
}
private void FixedUpdate()
{
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
I know it was much worse but could you try? :)
body.velocity = new Vector2(-horizontal * runSpeed, -vertical * runSpeed);

Moving Camera shake in Unity

I'm working on a downhill racer and I want to have the camera shake to various degrees to convey that the player is getting faster. Right now I have a GameObject called "CameraHolder" which is a parent to the Main Camera.
I have a script attached to the Holder that follows the player, and another script attached to the camera that is meant to shake it within that holder.
CameraHolder Follow Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Camera cam;
public Transform player;
public Vector3 offset;
public float speed = 2f;
public Rigidbody rb;
public float startingFieldOfView = 60f;
void FixedUpdate()
{
float interpolation = speed * Time.deltaTime;
Vector3 cameraPosition = new Vector3(Mathf.Lerp(transform.position.x, player.position.x, interpolation), player.position.y + player.localScale.y + offset.y, player.position.z + -player.localScale.z + offset.z);
// Change of camera FOV depending on the speed of the rigidbody
transform.position = cameraPosition;
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, startingFieldOfView + (rb.velocity.magnitude / 3), .1f);
CameraShake Script attached to child Main Camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public Rigidbody player;
public float shakeThreshold;
public float shakeMagnitude;
private void Update()
{
float x = Random.Range(-shakeMagnitude, shakeMagnitude);
float y = Random.Range(-shakeMagnitude, shakeMagnitude);
if (player.velocity.magnitude >= shakeThreshold)
{
transform.localPosition = new Vector3(x, y, transform.position.z);
}
}
}
I think maybe I misunderstand how localPosition works. I thought that it would move the camera the random amount while moving within the holder, but it does nothing. Am I applying this wrong or is the logic off?
I think using cinemachine will really help you.
To remplace your CameraFollow script you can use a virtual camera with the property Follow, for more information you can go there : https://docs.unity3d.com/Packages/com.unity.cinemachine#2.6/manual/CinemachineSetUpVCam.html
To replace your Camera shake you can check the Noise Property there : https://docs.unity3d.com/Packages/com.unity.cinemachine#2.6/manual/CinemachineVirtualCameraNoise.html
For more information on cinemachine go check the doc here : https://docs.unity3d.com/Packages/com.unity.cinemachine#2.6/manual/CinemachineUsing.html

trying to make a mouse rotation script in unity, but it does nothing

Im making a 3d platformer In unity as my first game, and I'm trying to add some mouse rotation, but don't know what to do. the problem is it literally will not rotate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PCM : MonoBehaviour
{
public float speed = 10.0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float translation = Input.GetAxis("MouseX") * speed;
float straffe = Input.GetAxis("MouseY") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
}
}
all player propeties
I assume, since you said "platform" that you're doing 2D. You had
transform.Translate(straffe, 0, translation);
Usually, 2D works on X- (left/right) and Y- (up/down) axes (and Z is "in/out"). Try
transform.Translate(straffe, translation, 0);

Rigid Body fps controller issues

I'm trying to make my own Rigid body character controller using Unity, I have movement but it's a little weird. When I press A/D it moves my Character along the x-axis, then when I press W/D it moves it along the y-axis. Also right now I have an fps camera (Brakeys) but the Rigid body has no code telling it to go in that direction.
Here is the code...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 5f;
public float jumpForce = 5f;
public bool isGrounded;
public Rigidbody rb;
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
rb.AddForce(move * speed);
}
}
Use FixedUpdate when dealing with physics.
And you're assigning the vertical input value to the Y-axis. My guess is you want it on the Z-axis.
void FixedUpdate()
{
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
rb.AddForce(move * speed);
}

How to rotate camera around player in unity c#

I need to rotate the camera around my player gameobject while holding the left mouse button. How would I approach this?
Also, I've read a bit on Vector 3, but I don't have a full understanding of it. Anybody who could explain it would be greatly appreciated.
I've looked at youtube videos and this one is exactly the concept I was looking for. I was just having trouble applying it to my code.
I'm on a bit of a time crunch, exams are nearing and my teacher hasn't explained most things that are explained in the video.
// This is my code inside the camera which follows the ball/player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptBallCam : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
//End of code inside camera
//Code inside of player/ball
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptBall : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
// end code
Results I'm expecting are exactly shown at 1:22 in
https://www.youtube.com/watch?v=xcn7hz7J7sI
Try this. The script goes on your camera.
Basically this script works by first getting the direction your mouse has moved in. In this case the X axis Mouse X (left/right direction). Then we take our rotation speed turnSpeed, and use it to rotate around the player by that amount of degrees using Quaternion.AngleAxis. Finally we make sure that the camera is always looking at the player by using transform.LookAt
using UnityEngine;
using System.Collections;
public class OrbitPlayer : MonoBehaviour {
public float turnSpeed = 5.0f;
public GameObject player;
private Transform playerTransform;
private Vector3 offset;
private float yOffset = 10.0f;
private float zOffset = 10.0f;
void Start () {
playerTransform = player.transform;
offset = new Vector3(playerTransform.position.x, playerTransform.position.y + yOffset, playerTransform.position.z + zOffset);
}
void FixedUpdate()
{
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = playerTransform.position + offset;
transform.LookAt(playerTransform.position);
}
}
There is a lot of information on this topic over here:
https://answers.unity.com/questions/600577/camera-rotation-around-player-while-following.html

Categories

Resources